Hey guys, i would need help in one of my array programs , i am supposed to program a rainfall viewer program. The rainfall data is the amount of rainfall from January to March, everyday . However i am stuck on how do i get the program to display the maximum or meaning to say the highest rainfall amount that has been reached , say during January , from the data that is fed from external source ?
externfloat rainfallJan[];
externfloat rainfallFeb[];
externfloat rainfallMar[];
externvoid getPwd(char* pwd); // function prototype to store password input
externint chkPwd(char* pwd);
#include <stdio.h>
void SystemLogin(void);
void rainfallMenu(void);
void main(void)
{
char userinput;
int j;
int month;
int mostRainfall;
float total;
float totaldata;
char systemPwd[]="abc123"; // declare system password as global string variable
SystemLogin(); // Call in function for logging into the system
do
{
rainfallMenu();//call function to display menu
userinput=getchar(); // Record user input with getchar() function
fflush(stdin); //remove remaining input stream data
switch(userinput)
{
case'A': case'a':
printf("The raifall data for January from day 1 down to day 31 respectively is :\n");
totaldata=0;
for ( j=0;j<31;j++ )
{
printf(" %.2f\n", rainfallJan[j]);
totaldata = totaldata+rainfallJan[j];
}
printf("\n\n");
printf("With the data , the total amount of rainfall in MM is %.2f\n", totaldata);
printf("\n\n");
printf("\n\n");
printf("\n\n");
printf("View more rainfall data or quit from the following options :\n");
printf("\n\n");
break;
case'B': case'b':
printf("The rainfall data for Febuary from day 1 down to day 28 respectively is:\n");
for ( j=0; j<32; j++ )
{
printf(" %.2f\n", rainfallFeb[j]);
totaldata = totaldata+rainfallFeb[j];
}
printf("\n\n");
printf("With the data , the total amount of rainfall in MM is %.2f\n", totaldata);
printf("\n\n");
printf("\n\n");
printf("View more rainfall data or quit from the following options :\n");
printf("\n\n");
break;
case'C' : case'c':
printf("The rainfall data for March from day 1 to day 31 respectively is :\n");
for ( j=0; j<32; j++ )
{
printf(" %.2f\n", rainfallMar[j]);
totaldata = totaldata+rainfallMar[j];
}
printf("\n\n");
printf("With the data , the total amount of rainfall in MM is %.2f\n", totaldata);
printf("\n\n");
printf("\n\n");
printf("View more rainfall data or quit from the following options :\n");
printf("\n\n");
break;
case'D': case'd':
break;
Case D is the option to print to the user the maximum rainfall during January
Hey thanks for the help ! Managed to implement it into the program and its working now
1 2 3 4 5 6 7 8 9 10 11 12 13
case'D': case'd':
max = 0.0;
for(j=0;j<31;j++)
{
if (rainfallJan[j] > max)
{
max = rainfallJan[j];
}
}
printf("\n\n");
printf("The maximum rainfall for the month of January is %.2f\n", max);
printf("\n\n");
However could you please explain roughly how does this code enable it to find the highest value inside the array ?
- initialise max to a low number
- for each element in the array we are asking "are you greater than the current maximum value?"
- if it isn't, we do not re-assign a value to max, so (in your case), it remains zero.
- if it is, we do assign this current element value to be max as well.
So after we have iterated over the whole array, max will indeed be the biggest value found in the array.
Thank you all for the help ! Sorry the late reply , been busy with school and other projects , i've added in the extra codes and it is working well ! Thanks :D