C Calculating annual rainfall totals with nested loops, & matching user inputs -
i having hardest time understanding why program won't execute correctly. need calculate total rainfall per year using loop, problem i'm having not excepting user input month of february , instead adds previous years total (i.e. 2011 total becomes feb. 2012 user input). appreciated.
#include <stdio.h> #include <stdio.h> #include <stdio.h> #define nummonths 12 #define numyears 5 // function prototypes void inputdata(); void printdata(); // global variables // these available functions float raindata[numyears][nummonths]; float sum = 0.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 want input precipatation data? (y yes)\n"); scanf("%c",&enterdata); if (enterdata == 'y') { // call function input data inputdata(); // call function display data printdata(); } else { printf("no data input @ time\n"); } printf("please try precipitation program again. \n"); return 0; } // function inputdata void inputdata() { /* variable definition: */ float rain=1.0; // input data (int year=0;year < numyears; year++) { (int month=0; month< nummonths; month++) { printf("enter rain %d, %d:\n", year+1, month+1); scanf("%f",&rain); raindata[year][month]=rain; } } } // function printdata void printdata() { // print data printf ("year\t month\t rain\n"); //function sum rainfall (int year=0; year< numyears; year++) { (int month=0; month< nummonths; month++) { printf("%s\t %s\t %5.2f\n", years[year],months[month],raindata[year][month]); raindata[year][nummonths+1] += raindata[year][month]; } printf("total amount of rain year %s: %5.2f\n", years[year], raindata[year][nummonths+1]); //prints total amount of rain every year } }
here output:
total amount of rain year 2011: 40.00 2012 jan 1.10 2012 feb 40.00 2012 mar 3.30
the main problem you're trying store total each year in variable doesn't exist - array element raindata[year][month+1]
. declared 5x12 array, , you're trying use 5x13. because of way array stored in memory, storing [month+1]
value in next year's data.
instead, i've added array (raintotal[numyears]
), , used store total each year.
#include <stdio.h> #define nummonths 12 #define numyears 5 // function prototypes void inputdata(void); void printdata(void); // global variables // these available functions float raindata[numyears][nummonths]; float raintotal[numyears]; float sum = 0.0; char years[numyears][5] = {"2011","2012","2013","2014","2015"}; char months[nummonths][4] = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"}; int main () { char enterdata = 'y'; printf("do want input precipatation data? (y yes)\n"); scanf("%c",&enterdata); if (enterdata == 'y') { // call function input data inputdata(); // call function display data printdata(); } else { printf("no data input @ time\n"); } printf("please try precipitation program again. \n"); return 0; } // function inputdata void inputdata() { // variable definition float rain=1.0; // input data (int year=0;year < numyears; year++) { (int month=0; month< nummonths; month++) { printf("enter rain %s, %s:\n", years[year], months[month]); scanf("%f",&rain); raindata[year][month]=rain; } } } // function printdata void printdata() { // print data printf ("year\t month\t rain\n"); //function sum rainfall (int year=0; year< numyears; year++) { (int month=0; month< nummonths; month++) { printf("%s\t %s\t %5.2f\n", years[year],months[month],raindata[year][month]); raintotal[year] += raindata[year][month]; } printf("total amount of rain year %s: %5.2f\n", years[year], raintotal[year]); //prints total amount of rain every year } }
i've made other changes:
you declared
years
,months
second array element seemingly equal number of years, , number of months. instead, should expected length ofchar
string, plus 1 (for null terminator).i changed printing statement show character strings declared,
enter rain 2011, jan:
instead ofenter rain 1, 1:
i fixed function prototypes (also pointed out user)
cleaned formatting - have @ popular coding standards - make code lot more readable when you're asking people :)
Comments
Post a Comment