Multiple Functions in a program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <cstdlib>
#include <iostream>

using namespace std;

 struct daytemp
    {
    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;
	
    
    cout<<"DAY  TEMP-WK1  TEMP-WK2  AVG"<<endl;
    //Display pattern
    for(int r=0;r<=5;++r)
    {
            for(int c=0;c<=2;++c)
            {
                    if (c==0)
                    {
                    cout<<dt[r][c].name<<"  ";
                    }
                    else
                    {
                    cout<<dt[r][c].temp<<"        ";
                    }     
            }
            cout<<endl;
    }
   
    system("PAUSE");
    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
{
	int i;
	double n;
		for(i=0;i<=4;i++)
		{
		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
{
	int i2;
	double n2;
		for(i2=0;i2<=4;i2++)
		{
		n2+=dt[i2][2].temp;   //Adds to itself every iteration
		}
return ((n2)/5);
}


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!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#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.
Last edited on
Topic archived. No new replies allowed.