This program lists down the temperatures of week1 and week2 and in the final row is supposed to average each week's temperatures. Everything about the output is right except the output of of week two's average which always comes out to 71.6 and I am not sure why. I double and triple checked and can't figure what is happening; the two functions must be conflicting somewhere I think. This is the first time I've used several functions within one program, any help or tips will be much appreciated. Thanks!
#include <cstdlib>
#include <iostream>
#include <string> //you should use this
struct daytemp
{
std::string name;
double temp;
};
double avgwval2(daytemp dt[][4]);
double avgwval(daytemp dt[][4]);
int main()
{
//Week1 and Week2 Avg
daytemp dt[6][4]; //Dimensions of array
dt[0][0].name = "mon";
dt[1][0].name = "tue";
dt[2][0].name = "wed";
dt[3][0].name = "thu";
dt[4][0].name = "fri";
dt[0][1].name = "mon";
dt[0][1].temp = 59;
dt[1][1].name = "tue";
dt[1][1].temp = 35;
dt[2][1].name = "wed";
dt[2][1].temp = 59;
dt[3][1].name = "thu";
dt[3][1].temp = 35;
dt[4][1].name = "fri";
dt[4][1].temp = 16;
dt[0][2].name = "mon";
dt[0][2].temp = 49;
dt[1][2].name = "tue";
dt[1][2].temp = 25;
dt[2][2].name = "wed";
dt[2][2].temp = 49;
dt[3][2].name = "thu";
dt[3][2].temp = 25;
dt[4][2].name = "fri";
dt[4][2].temp = 6;
//Declare Weekly averages
double a1, a2;
for(int i=0;i<=1; i++)
{
if(i==0)
{
a1=avgwval(dt);
}
else
{
a2=avgwval2(dt);
}
}
//Weekly Avg
dt[5][1].name = "AVG";
dt[5][1].temp = a1;
dt[5][2].name = "AVG";
dt[5][2].temp = a2;
std::cout<<"DAY TEMP-WK1 TEMP-WK2 AVG"<< '\n' ;
//Display pattern
for(int r=0;r<=5;++r)
{
for(int c=0;c<=2;++c)
{
if (c==0)
{
std::cout<< dt[r][c].name <<" ";
}
else
{
std::cout<< dt[r][c].temp <<" ";
}
}
std::cout<< '\n';
}
//no system(pause) that is a BAD this
return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------
//FUNCTIONS TO CALCULATE AVG TEMP OF WEEK
//WEEK 1
double avgwval(daytemp dt[][4]) //Function "Avgwval", needs to know struct array that's transferring over
{
double n = 0; //forgot to get it to 0
for(int i = 0; i < 5; i++)//declare iterators only in needed scope
{
n+=dt[i][1].temp; //Adds to itself every iteration
}
return (n/5);
}
//WEEK2
double avgwval2(daytemp dt[][4]) //Function "Avgwval", needs to know struct array that's transferring over
{
double n = 0; //you forgot to set it to 0
for(int i = 0; i < 5 ; i++)
{
n+=dt[i][2].temp; //Adds to itself every iteration
}
return ((n)/5);
}
i commented the few errors you made that were messing up your program. you should look back over scopes of variables, and you need to learn in which way variables are allocated.