Ok this program takes rainfall data for every month of the year and displays the total and avg for each month. I need it to display each month in order of rainfall from highest to lowest as well.
Im having a hard time implementing showorder into my code. Can someone give me a hand? Thank you very much

#include <iostream.h>
#define MAXSIZE ]2
 
void fillArray(float [MAXSIZE]);

void checkArray(float [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;

}

Recommended Answers

All 8 Replies

What are the problems you are facing when sorting the array?

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.

Thank you sos. It was compileing fine for me tho.
I must make it display each month in order of rainfall highest to lowest.

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 ??

no im useing v C++ 6. I dont know where it add that in. ate the very end? sorry im really new to this

What to add at the end ?
What have you decided, to use inbuilt functions or to make your own function?
First specify.

I think im sposed to use the show order function. I was asking if i needed to add it to the end of my code.

Yeah just like your previous functions... declare the prototype at the start of the program and write the function defination at the end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.