I'm doing a project for school and I'm having difficulty getting the average of two averages, using arrays. I have to get the average of high temperatures for a year, the average for low temperatures for a year, and then find the average of those average. We used 366 days a year, which is 732 added for both low and high. Here's the code I have now:

int DailyTemps::get_avg()
  double avg_temp_hi, avg_temp_lo, total_avg;

    for(int i = 1; i < num_days; i++)
        avg_temp_hi = temps_hi[i] + avg_temp_hi;
    for(int i = 1; i < num_days; i++)
        avg_temp_lo = temps_lo[i] + avg_temp_lo;
    total_avg = (avg_temp_hi + avg_temp_lo) / (732);

The first time I run the program it outputs

the  avg  high daily temperature last year was -2147483648

When the menu repeats and I select the option for average, it outputs:

the  avg  high daily temperature last year was 50

What is wrong with this code?

6 Years
Discussion Span
Last Post by s_r_k

You are using variables without initializing them. You need to initial the value of avg_temp_hi and avg_temp_lo to 0 before you use them in the loops...

Edited by Taywin: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.