need help

This is my code for the rainfall array..
// Arrays Lab.cpp : Defines the entry point for the console application.
/*
Rainfall

Write a RainFall program that stores the total rainfall for each of 12 months into an array of doubles.

The program should have methods that return the following:

* The total rainfall for the year

* The average monthly rainfall

* The month with the most rain

* The month with the least rain

Input Validation: Do not accept negative numbers for monthly rainfall figures.
*/
#include <stdio.h>
#include "stdafx.h"
#define MAX_Months 12
void getAnnualrainfall(double Array[], int size);

void sortArray(double Arraymonths[]){
int temp;

for (int i = 0; i < MAX_Months - 1; i++) {
for (int j = i + 1; j < MAX_Months; j++){
if (Arraymonths[i] > Arraymonths[j]){
temp = Arraymonths[i];
Arraymonths[i] = Arraymonths[j];
Arraymonths[j] = temp;

}
}
} printf(" The rainfall array looks like this after being sorted :\n");
for (int i = 0; i < MAX_Months; i++){
printf(" %g ", Arraymonths[i]);
}

}

int getMax( double Arraymonths[]);
int getMin( double Arraymonths[]);

int _tmain(int argc, _TCHAR* argv[])

{//Variable declaration
char *Months[MAX_Months] = {"JAN", "FEB" , "MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC" };
double rainfallpermonth[12] = { 0 };
int Maxrainfall, Minrainfall;

//Input

for (int i = 0; i < 12; i++){
do {
printf(" the total rainfall for %s:", Months[i]);
scanf("%lf", &rainfallpermonth[i]);
} while (rainfallpermonth[i] < 0);
}

// Calculations
getAnnualrainfall (rainfallpermonth, MAX_Months);
sortArray(rainfallpermonth);
Maxrainfall = getMax(rainfallpermonth);
Minrainfall = getMin(rainfallpermonth);
printf ("\n The month with the highest rainfall is %s with %lf inches.\n", Months[Maxrainfall], rainfallpermonth[Maxrainfall]);
printf (" The month with the smallest rainfall is %s with %lf inches.\n ", Months[Minrainfall], rainfallpermonth[Minrainfall] );

return 0;
}

void getAnnualrainfall(double Array[], int size)
{
double Average;
double Total=0;
for (int count = 0; count < size; count++){
Total += Array[count];
}
Average = Total / size;
printf(" The total rainfall for the year is %.2lf inch\n ", Total);
printf(" The average monthly rainfall is %.2g inch\n", Average);
}

int getMax( double Arraymonths[]){
int Max= 0;
for( int i=0; i<MAX_Months; i++ ){
if( Arraymonths[i] > Arraymonths[Max])
Max = i;
}
return Max;
}
int getMin( double Arraymonths[] ){
int Min=0;
for( int i=0; i< MAX_Months; i++ ){
if( Arraymonths[i] < Arraymonths[Min])
Min = i;
}
return Min;
}
The problem is that when i execute it and type in all the number for rainfall and at the output for the month with highest and smallest rainfall, it always DEC and JAN even though its rainfall is not the highest or the smallest.. Plz help me with this as soon as possible guys since The deadline is 11:59 tonight . Thank you so much and i very appreciate that..
The problem is that you sort the array before calculating the highest and smallest rainfalls. In the sorted array the first element will always be the smallest and the last element the largest.
Topic archived. No new replies allowed.