Whats wrong with this?

I need to print my counters in the main program and not in the function but I cant figure out why I the main program prints 0's and when I test it in the function the counters are correct.

I have posted my code and output below. The parts i'm having trouble with are bold and underlined. Thanks



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//assignment #5
#include<iostream>
#include<fstream>
using namespace std;
ifstream infile("data.dat");
const int VALUE=50;
void readdata(int, int[], int[]);
void printarray(int, int[]);
int smallest(int, int[]);
void construct(int , int [], int [], int []);
int whatshigher(int, int [], int [], int , int , int );
int main()
{
	int n, small, small_1, small_diff, score1[VALUE], score2[VALUE];
    int difference[VALUE], score1_count=0, score2_count=0, equal_count=0;
    infile>>n;
	
    readdata(n, score1, score2);
	
	cout<<"Score 1 array is printing"<<endl;
	printarray(n, score1);
	cout<<"Score 2 array is printing"<<endl;
    printarray(n, score2);
    
    small=smallest(n, score1);
	cout<<"smallest in array 1"<<endl;
    cout<<small<<endl;
    
    small_1=smallest(n, score2);
	cout<<"smallest in array 2"<<endl;
    cout<<small_1<<endl;
    
    construct(n, score1, score2, difference);
    cout<<"The smallest in difference: ";
    small_diff=smallest(n, difference);
    cout<<small_diff<<endl;
    
    whatshigher(n, score1, score2, score1_count, score2_count, equal_count); 
    cout<<"Array score1 was larger "<<score1_count<<"times"<<endl;
    cout<<"Array score2 was larger "<<score2_count<<"times"<<endl;
    cout<<"The two arrays were equal"<<equal_count<<"times"<<endl;
    
    
    
    
    
    
    
    
    infile.close();
	
system("pause");
return 0;
}

//readdata()
void readdata(int n, int nums1[], int nums2[]){

for(int i=0; i<n; i++){
     infile>>nums1[i];
     infile>>nums2[i];
}


}





//printarray()
void printarray(int lim, int arr[]){
for(int i=0; i<lim; i++)
cout<<arr[i]<<" ";

cout<<endl;



}
     


//smallest()
int smallest(int n, int nums[]){
//Process array
int min ;
for(int i=0; i<n; i++)
{ int num = nums[i];
if(num < min)
min = num;
}

return min;
}


//construct()
void construct(int k, int old1[], int old2[], int diff[]){
    
    cout<<"The difference array: ";
    for(int i=0; i<k; i++){
         diff[i]=old1[i]-old2[i];
         cout<<diff[i]<<" ";
         }
    cout<<endl;
}


//whatshigher()
int whatshigher(int n, int score1[], int score2[], int c1, int c2, int c3){
     
     
     
     for(int i=0; i<n; i++){
          if(score1[i]>score2[i]){
               cout<<"In position "<<i<<", score1 is larger:"
                   <<score1[i]<<" is greater than "<<score2[i]<<endl;
               c1++;
          }
          else if(score2[i]>score1[i]){
               c2++;
               cout<<"In position "<<i<<", score2 is larger:"
                   <<score2[i]<<" is greater than "<<score1[i]<<endl;
               
               }
          else if(score1[i]==score2[i]){
               cout<<"In position "<<i<<", the two arrays have the same value: "
                   <<score1[i]<<endl;
               c3++;
               }
     
     }
     
     return c1, c2, c3;
     
}
 



Here is my output:

Score 1 array is printing
12 1 51
Score 2 array is printing
45 15 60
smallest in array 1
1
smallest in array 2
15
The difference array: -33 -14 -9
The smallest in difference: -33
In position 0, score2 is larger:45 is greater than 12
In position 1, score2 is larger:15 is greater than 1
In position 2, score2 is larger:60 is greater than 51
Array score1 was larger 0times
Array score2 was larger 0times
The two arrays were equal 0times

Press any key to continue . . .
In function
 
int whatshigher(int n, int score1[], int score2[], int c1, int c2, int c3)


change it to

 
int whatshigher(int n, int score1[], int score2[], int& c1, int& c2, int& c3)


figure out the rest yourself
Without regard to what your program is doing or any other possible errors, your function whatshigher passes score1_count, score2_count and equal_count by value. Consequently inside the function you are working with copies of these variables and it is the copies that are modified within the function. Line 135 return c1, c2, c3;, although legal code does not return three values. A function can only return one value. The comma operator evaluates each expression from left to right with the result being the rightmost expression. The line return c1, c2, c3; evaluates c1 and discards it, evaluates c2 and discards it and finally evaluates c3 and returns it when the function ends. Notice that your call of the function on line 38 never uses the return value. To fix this problem change the return type of your function to void and pass score1_count, score2_count and equal_count by reference. Finally change line 135 to return nothing.

void whatshigher(int n, int score1[], int score2[], int& c1, int& c2, int& c3)

You will also need to change the function prototype on line 11 to match.
Thank you! Got it!
Topic archived. No new replies allowed.