Finding highest data in an array

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 ?
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
85
86
87
88
89
90
91
92
93
  extern float rainfallJan[];   
extern float rainfallFeb[]; 
extern float rainfallMar[];
extern void getPwd(char* pwd);	// function prototype to store password input
extern int 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

 
float rainfallJan[30]={0.0,0.0,0.0,0.0,18.4,31.2,0.0,0.0,2.0,5.6,18.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};


The above code is the rainfall data of January stored in an external source.

Thanks in advance cheers :)
Last edited on
1
2
3
4
5
6
7
8
9
10
float max =-1.0;

for(int i=0;i< 31;i++)
{
    if (rainfallJan[i] > max)
    {
         max = rainfallJan[i];
    } 
}


that kinda thing.

edit. you can incorporate this into your for loop on line 41.
Last edited on
@mutexe

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.
Last edited on
closed account (48T7M4Gy)
As a small refinement you can initialise the max value to the first value of the array. Same for the minimum value ...
As a small refinement you can initialise the max value to the first value of the array. Same for the minimum value ...

OP, this is a much better suggestion than my suggestion of initialising to -1.0.
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
Topic archived. No new replies allowed.