Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C programming return value odd

Tags:

c

I tried to search this everywhere, but it's kind of difficult to word, it's most likely a simple fix. Basically when I go through my program that is supposed to compute the average rainfall for a year, it comes out with a very large number, however, I thought it may have been just that I was doing the arithmetic wrong or had a syntax error of some sort, but that was not the case, when I checked the value that the function returned it was the proper value.

#include <stdio.h>
#include <string.h>

void getData(float *, float *);

int main()
{
    char state[2], city[81];
    float rainFall[12], outputAverage, *pAverage;

    printf("Name Here\n");
    printf("Please enter the state using a two letter abreviation: ");
    gets(state);
    printf("Please enter the city : ");
    gets(city);
    pAverage = &outputAverage;
    (getData(rainFall, pAverage));
    printf("%.2f", outputAverage);


    return (0);
}

void getData(float *rainFall, float *pAverage)
{
    int i;
    float total;
    for (i=0; i<12; i++)
    {
        printf("Please enter the total rainfall in inches for month %d: ", i+1);
        scanf("%f", &rainFall[i]);
        total += rainFall[i];

    }
    *pAverage = total / 12;



}
like image 400
user2345434 Avatar asked Feb 09 '26 20:02

user2345434


2 Answers

you need to initialize total

float total = 0.0;
like image 89
Keith Nicholas Avatar answered Feb 12 '26 14:02

Keith Nicholas


  1. Initialize the total to 0
  2. Why you make it complicated? Why not just

    return total / 12 ?

    and called it like

    outputAverage = getData(rainfall)

like image 29
marcadian Avatar answered Feb 12 '26 15:02

marcadian