c - how do you store data from a for loop into a variable for other calculations? -


i'm trying store sums variable add percentage between years. right i'm stumped on how this. searched answers , tried few things ,but no luck yet. wondering if point me in right direction figure out.i keep getting memory location , not value. i'm college student please bear me. appreciated. here's code review.

#define years 4 #define months 12 int main(void) {     float percentage [4];     int = 0, j = 0, n = 0, sum = 0;     int time[] = {    2012,2013,2014,2015};     int *value[years];     const char* name[]= {"  jan ", "feb ", "mar ", "apr ", "may ", "jun ", "jul  ","aug ","sep ","oct ","nov ","dec "};     int range[years][months] = {          { 5626, 5629, 5626, 5606, 5622, 5633, 5647, 5656, 5673, 5682, 5728, 5728},         { 5741, 5793, 5814, 5811, 5831, 5854, 5857, 5874, 5900, 5923, 5954, 5939},         { 5999, 6020, 6062, 6103, 6115, 6128, 6169, 6194, 6219, 6233, 6256, 6301},         { 6351, 6378, 6371, 6409, 6426, 6426, 6437, 6441, 6451, 6484, 6549, 6597}     };      printf(" year  %s %s %s %s %s %s %s %s %s %s %s %s\n", name[0], name[1], name[2], name[3], name[4], name[5], name[6],name[7],name[8],name[9],name[10],name[11]);     /* for(n=0; n < name; n++)            printf("%s", name[n]); // code keeps crashing program     */     (i = 0; < years; i++) {         printf(" %i    ", time[i]);         (j = 0; j < months; j++)             printf("%2i ", range[i][j]);             printf("\n");      }      (i = 0; < years; i++) {         for(j = 0, sum = 0; j < months; j++)             sum += range[i][j];             printf("\n sum of months %i: %i", time[i], sum);       }     (i = 0; < years; i++) {         for(j = 0, sum = 0; j < months; j++)             value[years] = sum;             printf("\n%i", value);     }      return 0;  } 

change value array of ints. not make sense make value array of pointers.

int value[years]; // drop * 

you have following block compute sum each year.

for (i = 0; < years; i++) {     for(j = 0, sum = 0; j < months; j++)         sum += range[i][j];         printf("\n sum of months %i: %i", time[i], sum); } 

however, sum not stored. gets overwritten each year.

what need save sum in value. use:

for (i = 0; < years; i++) {    value[i] = 0;    for(j = 0, sum = 0; j < months; j++)    {       value[i] += range[i][j];    }    printf("\n sum of months %i: %i", time[i], value[i]); } 

after that, don't need last loop @ all.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -