Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need some help on why this program isn't working right

jeepman950jeepman950 OkinawaMember Posts: 3

This code compiles fine but the math does not work for the sum. Can you please help for why. The variables I am using are as followed:
y
1.2
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4

The C code is:

define NUMMONTHS 12

define NUMYEARS 5

include <stdio.h>

// function prototypes
void inputdata();
void printdata();

// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
int sum =0;
char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
char months[NUMMONTHS][12] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int main ()
{
char enterData = 'y';
printf("Do you want to input Precipatation data? (y for yes)\n");
scanf("%c",&enterData);
if (enterData == 'y') {
// Call Function to Input data
inputdata();

 // Call Function to display data
 printdata();

}
else {
printf("No data was input at this time\n");
}
printf("Please try the Precipitation program again. \n");
return 0;
}
// function to inputdata
void inputdata() {
/* variable definition: */
float Rain=1.0;
// Input Data
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf("Enter rain for %d, %d:\n", year+1, month+1);
scanf("%f",&Rain);
Raindata[year][month]=Rain;
}
}
}
// Function to printdata
void printdata(){
// Print data
printf ("year\t month\t rain\n");
for (int year=0;year < NUMYEARS; year++) {
sum=0;
for (int month=0; month< NUMMONTHS; month++) {
sum = sum + Raindata[NUMYEARS][NUMMONTHS];
printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);
}
printf("Total rain in the year is: %f\n", sum);
}
printf("Total rain in all years : %f\n", sum);
}

Thanks any help work greatly be apperciated

Sign In or Register to comment.