Adding up arrays in a data

Hey all ! I am new to this forum and would need to get some help in my program !
I am trying to write a program that displays a menu for the user to choose which rainfall data to display from January to March. One of the options would be to display the total rainfall of the month, which is the adding of all the rainfall in MM for each day. Part of my code is as shown below, however i am unable to get the program to add up the data in my array correctly as it displays weird numbers and not the one that i need

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
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;
    float total;
	char systemPwd[]="abc123";	// declare system password as global string variable
	total=0.0;
	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; is :\n");
					for ( j=0;j<31;j++ ) 
					{
						total = total + rainfallJan[j];
						printf("%.2f", total);
					}
			               
					printf("\n\n");
					printf("View more rainfall data or quit from the following options :\n");
					printf("\n\n");

			     break;
	


The following code below is my data for the rainfall data of January in a seperate file
1
2
3

float rainfallJan[31]={0.0,0.0,0.0,18.4,31.2,0.0,0.0,2.0,0.0,0.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};


Can someone tell me what is wrong with the coding to display the adding of my January data?

Thanks in advance ! Cheers
%f specifier expects double, not float.
The following code below is my data for the rainfall data of January in a seperate file

I wonder if you are misinterpreting how rainfall data is supposed to be represented in a separate file. I doubt that the separate file is supposed to be some other code file that gets compiled into your executable. Instead, the file should probably have just the numbers in it, which you then read in yourself. Can you describe your assignment more? Were you given a sample file full of data to use?
@MiiNiPaa
Alrights , so if i am not supposed to use %f as specifier , then which specifier should i use ?

@booradley60
Uhm okays sorry if it wasn't clear what i meant by a seperate file is that the daily rainfall data will be fed to external sources ( external data files ) as arrays and i am supposed to :

Write a program to show a menu with options that allows the user to display
1. The total rainfall of a selected month
2. The maximum rainfalls of the selected month.
3. The month with the lowest average rainfall and the value of the lowest average rainfall
among all the months. (eg. Among three months that you have chosen).

> which specifier should i use ?

Use %f.

When a function with variadic arguments is called, float arguments are implicitly promoted to double (floating-point promotion).
Set total=0; just before line 38. Otherwise you'll get the right answer the first time you ask for it, but subsequent times will be adding to the previous total.

Swap lines 41 and 42. You want to print the final total, not the total after each day, right?

Thanks everyone for the help ! Appreciated a lot, finally managed to get the program to calculate the total rainfall in my arrays !

@dhayden swapping lines 41 and 42 and setting total=0; before line 38 sure did help ! thanks :)
Topic archived. No new replies allowed.