Search code examples
carraysmultidimensional-arrayuser-defined-functionsfunction-prototypes

C Function to Group and Sum Column in Multi-D Array


I'm new to coding and C has been giving me some troubles. I have a function that uses scanf() to input values into an array. I need to group a column by an element and then sum the data. Here is what I have so far:

#define NUMMONTHS 12
#define NUMYEARS 5
#include <stdio.h>

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

// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
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();

    // Call Function to sum data
    sumdata();
}
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++) {
    for (int month = 0; month< NUMMONTHS; month++) {
        printf("%s\t %s\t %5.2f\n", years[year], months[month], Raindata[year][month]);
    }
}
}
// Function to sumdata
void sumdata() {
// Sum data
float sum = 0;
for (int year = 0; year < NUMYEARS; year++) {
    for (int month = 0; month < NUMMONTHS; month++) {
        sum += Raindata[year][month];
        printf("Sum of Rainfall of Year %s is %f \n", years, sum);
    }
}
}

Please excuse the indents - I am trying to get my sumdata() function to print the sum of yearly rainfall (grouping the rainfall data by year). Instead it is summing the entire Rain column. Please enlighten me with any suggestions.


Solution

  • You are declaring the sum = 0, before both the loops, instead declare the sum = 0 after the first loop and move the print statement to print after it adds all the months i.e,

    void sumdata() {
    // Sum data
    float sum;
    for (int year = 0; year < NUMYEARS; year++) {
        sum = 0.0;
        for (int month = 0; month < NUMMONTHS; month++) {
            sum += Raindata[year][month];
        }
        printf("Sum of Rainfall of Year %s is %f \n", years, sum);
    }
    

    Earlier, you initially declared sum as zero, and each time the rainfall was calculated, the value of sum was retained and was immediately printed for each iteration in months, now it is reset to zero for each year and prints only when traversed through all months, Hope this helps :)