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);
    return(total_avg);
}

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?

Recommended Answers

All 2 Replies

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...

Thank you. I knew it was a silly mistake

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.