No Output?

I seem to be having trouble getting output from my weekAvg function which is just the inverse of my dayAvg function. When the code is compiled and run it just does nothing i feel i might be missing something here but it should work and it doesn't any help would be appreciated.
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
/* Author: Dylan Kavanaugh
 * Date: August 30, 2013
 * Purpose: To get familiar with the use of 2D static array, 
 * and passing arrays as function parameters.
 */
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;

void dayAvg(int randomTemp[][24], int rows, int col){
    int dayAvgArr[7], day;
    cout<<"Please Enter A Day(1-7): ";
    cin>>day;
    for(int i=0;i<rows;i++){
        dayAvgArr[i]=0;
        for(int j=0;j<col;j++){
            dayAvgArr[i]+=randomTemp[i][j];
        }
        dayAvgArr[i]/=col;
    }
    switch(day){
        case 1:
            cout<<"Sunday's Average Temperature Is: "<<dayAvgArr[day-1]<<endl;
            break;
        case 2:
            cout<<"Monday's Average Temperature Is: "<<dayAvgArr[day-1]<<endl;
            break;
        case 3:
            cout<<"Tuesday's Average Temperature Is: "<<dayAvgArr[day-1]<<endl;
            break;
        case 4:
            cout<<"Wednesday's Average Temperature Is: "<<dayAvgArr[day-1]<<endl;
            break;
        case 5:
            cout<<"Thursday's Average Temperature Is: "<<dayAvgArr[day-1]<<endl;
            break;
        case 6:
            cout<<"Friday's Average Temperature Is: "<<dayAvgArr[day-1]<<endl;
            break;
        case 7:
            cout<<"Saturday's Average Temperature Is: "<<dayAvgArr[day-1]<<endl;
            break;
        default:
            cout<<"Invalid Day Entered!\n";
            break;
    }
}

void weekAvg(int randomTemp2[][24], int rows2, int col2){
    int weekAvgArr[7], hour;
    cout<<"Please Enter A Time Of Day(0-23): ";
    cin>>hour;
    for(int j=0;j<col2;j++){
        weekAvgArr[j]=0;
        for(int i=0;i<rows2;i++){
            weekAvgArr[j]+=randomTemp2[i][j];
        }
        weekAvgArr[j]/=rows2;
    }
    cout<<"The Average Temperature For "<<hour<<"00 Is: "<<weekAvgArr[hour]<<endl;
}
int main(){
    int temp[7][24];
    int row=7, column=24;
    srand(time(NULL));
    for(int i=0;i<row;i++){
        for(int j=0;j<column;j++){
            temp[i][j]=rand()%121;
        }
    }
    dayAvg(temp,row,column);
    weekAvg(temp,row,column);
    //debug
    for(int i=0;i<row;i++){
        for(int j=0;j<column;j++){
            cout<<temp[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
Last edited on
The code in function weekAvg is incorrect. For example

void weekAvg(int randomTemp2[][24], int rows2, int col2){
int weekAvgArr[7], hour;
cout<<"Please Enter A Time Of Day(0-23): ";
cin>>hour;
for(int j=0;j<col2;j++){
weekAvgArr[j]=0;
for(int i=0;i<rows2;i++){
weekAvgArr[j]+=randomTemp2[i][j];
}
weekAvgArr[j]/=rows2;
}

array weekAvgArr is defined as having 7 elements. At the same time you are trying to initialize col2 elements of the array that is equal to 24.
Last edited on
Thank you for your swift response i changed the size from 7 to 24 to handle every hours average across 7 days. I missed the size of the array when i was copying code from my other function dayAvg(). Thank you for your help.
Topic archived. No new replies allowed.