Whats wrong with my code?
Apr 20, 2014 at 5:26pm UTC
I cannot seem to figure the error in my code. Basically, its suppose to take 2 arrays and sort them then print them out. Any help will be greatly appreciated.
This is my header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include<iostream>
#include <iomanip>
#include <string>
using namespace std;
class SnowData
{
private :
int dates[7];
double inches[7];
string monthName;
int startdate;
public :
SnowData(string, int , double []);
double getAverage();
void print();
void sortByDate();
void sortByInches();
};
My .cpp file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include "SnowData.h"
double SnowData::getAverage()
{
int sum = 0;
int average = 0;
for (int i = 0; i < 7; ++i)
sum += inches[i];
average = sum / 7;
return average;
}
void SnowData::print()
{
cout << "Snow Report for the month of " << monthName << endl;
cout << "Date Snow" << endl;
cout << "--------------------------------" << endl;
sortByDate();
cout << "Snow Report for the month of " << monthName << endl;
cout << "Date Snow" << endl;
cout << "--------------------------------" << endl;
sortByInches();
}
SnowData::SnowData(string _monthName, int _startdate, double _inches[])
{
for (int i = 0; i < 7; i++)
inches[i] = _inches[i];
for (int i = 0; i < 7; i++)
dates[i] = _startdate + i;
}
void sortByDate(int dates[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = dates[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (dates[index] < minValue)
{
minValue = dates[index];
minIndex = index;
}
}
dates[minIndex] = dates[startScan];
dates[startScan] = minValue;
}
}
void sortByInches(double inches[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = inches[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (inches[index] < minValue)
{
minValue = inches[index];
minIndex = index;
}
}
inches[minIndex] = inches[startScan];
inches[startScan] = minValue;
}
}
And my tester .cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include<iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "SnowData.h"
int main()
{
double snow_inches[7] = { 5.6, 7.7, 2.4, 1.9, 1.5, 4.5, 3.4 };
SnowData snow_data("January" , 12, snow_inches);
snow_data.sortByDate();
cout << "---------- Sorted by Date -------\n" ;
snow_data.print();
snow_data.sortByInches();
cout << "--------- Sorted by Inches ---------\n" ;
snow_data.print();
cout << "Average snowfall = " << setprecision(2)
<< snow_data.getAverage() << " inches\n" ;
return 0;
}
Last edited on Apr 20, 2014 at 5:30pm UTC
Topic archived. No new replies allowed.