What are the problems you are facing when sorting the array?
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Okay the thing is that your code doesnt even compile. I thought maybe you had "some problem" with the code ?
The mistakes were all silly ones os i have just changed them and the y are given in red...
// #include <iostream.h> depreceated header style use the one below
#include <iostream>
using namespace std ; // use the standard namespace
// #define MAXSIZE ]2 a closing square bracket ???
const long MAXSIZE = 12 ; // better
// void fillArray(float [MAXSIZE]);
// void checkArray(float [MAXSIZE]);
//will not compile, what were you trying to achieve here. this is not a
// prototype but errror.
void fillArray(float a[MAXSIZE]);
void checkArray(float a[MAXSIZE]);
int main(void)
{
float rain[MAXSIZE];
fillArray(rain);
checkArray(rain);
return 0;
}
void fillArray(float r[MAXSIZE])
{
int i;
for(i = 0; i < MAXSIZE; i++)
{
cout << "Enter rainfall for month " << i + 1 << " : ";
cin >> r[i];
if(r[i] < 0)
{
cout << "Rainfall cannot be negatige -- try again." << endl;
i--;
}
}
}
void checkArray(float a[MAXSIZE])
{
float max, min, tot, avg;
int i, maxmonth, minmonth;
max = min = a[0];
tot = avg = 0.0;
maxmonth = minmonth = 1;
for(i = 0; i < MAXSIZE; i++)
{
if(max < a[i])
{
max = a[i];
maxmonth = i + 1;
}
if(min > a[i])
{
min = a[i];
minmonth = i + 1;
}
tot = tot + a[i];
avg = tot / (i + 1);
}
cout.precision(2);
cout.setf(ios::showpoint | ios::fixed);
cout << "The largest rainfall was " << max << " in month " << maxmonth << "." << endl;
cout << "The smallest rainfall was " << min << " in month " << minmonth << "." << endl;
cout << "The total rainfall was " << tot << "." << endl;
cout << "The average rainfall was " << avg << "." << endl;
}
My output:Enter rainfall for month 1 : 100
Enter rainfall for month 2 : 50
Enter rainfall for month 3 : 200
Enter rainfall for month 4 : 300
Enter rainfall for month 5 : 23
Enter rainfall for month 6 : 5
Enter rainfall for month 7 : 46
Enter rainfall for month 8 : 23
Enter rainfall for month 9 : 5
Enter rainfall for month 10 : 2
Enter rainfall for month 11 : 55
Enter rainfall for month 12 : 3
The largest rainfall was 300.00 in month 4.
The smallest rainfall was 2.00 in month 10.
The total rainfall was 812.00.
The average rainfall was 67.67.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Hmm.. so you will obviously need a sorting algo..
If inbuilt algorithm will do, then look HERE
Otherwise you need to implement one of your own. A simple one you can get HERE
edit: Dont tell me you are usign the compiler of doomsday i.e. Turbo C ??
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
What to add at the end ?
What have you decided, to use inbuilt functions or to make your own function?
First specify.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Yeah just like your previous functions... declare the prototype at the start of the program and write the function defination at the end.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734