C++ poll aggregation

Hey everybody. I had to define a function for my programming class that would aggregate political polls. My source code is posted below and the logic all seems to be correct. However, whenever I call the predictWinner function in my professors auto grader it only returns the first if statement that appears in the code. For example when this if statement is first in the code:

1
2
if(sumC>=sumA && sumC>=sumB)
    {return 'C';}


The function will return 'C' when it is suppose to return 'C' but it will also only return 'C' whenever it is suppose to return 'A' or 'B'. So I get the correct answer when the expected result is 'C' but not when it is expected to be 'A' or 'B'. Similarly, If I were to move the else if statement below it above the previous if statement and change it to an if statement like this:

1
2
if(sumA>=sumB && sumA>=sumC)
    {return 'A';}


Then the function will return 'A' when 'A' is expected to be returned and 'A' when 'C' and 'B' are expected to be returned.

I have no idea what's going wrong here so any help at all would be really appreciated!


Here's the code:


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
#include <iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>

using namespace std;

char predictWinner(const double a[],const double b[],const double c[],const double pollWeights[],char results[],int len)
{   
    int sumA=0,sumB=0,sumC=0;
    for(int i=0;i<len;i++)
    {
        if( (a[i]>b[i]) && (a[i]>c[i]) )
        {
        results[i]='A';
        sumA=sumA+(pollWeights[i]*a[i]);
        }
        else if((b[i]>a[i]) && (b[i]>c[i]))
        {
        results[i]='B';
        sumB=sumB+(pollWeights[i]*b[i]);
        }
        else if((c[i]>a[i]) && (c[i]>b[i]))
        {
        results[i]='C';
        sumC=sumC+(pollWeights[i]*c[i]);
        }
    }
    if(sumC>=sumA && sumC>=sumB)
    {return 'C';}
    
    
    
    else if(sumA>=sumB && sumA>=sumC)
    {return 'A';}
    
    
    
    else if(sumB>=sumA && sumB>=sumC)
    {return 'B';}
    

   
}


int main()
{   
    double a[500],b[500],c[500],pollwt[500];
    char r[500];
    int n,i,j,m,k;
    cout<<"Enter the length of all the four arrays:";
    cin>>n;
     cout<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<"Enter any value from 0 to 100 for Polling A:";
        cin>>a[i];
        
       
        
         cout<<"Enter any value from 0 to 100 for Polling B:";
        cin>>b[i];
        
      //  cout<<endl;
        
         cout<<"Enter any value from 0 to 100 for Polling C:";
        cin>>c[i];
        
     //   cout<<endl;
        
        cout<<"Enter the weight of Poll No "<<(i+1)<<": ";
        cin>>pollwt[i];
        
        
    }
    
    char res=predictWinner(a,b,c,pollwt,r,n);
    cout<<endl;
    cout<<"Overall Predicted Winner="<<res<<endl;
    

    return 0;
}


The code being in this format always returns 'C' when 'C' is the expected result as well as when 'A' and 'B' are the expected result.
Last edited on
Here are the warning I get when compiling your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ g++ -Wall proj.cpp
proj.cpp: In function ‘int main()’:
proj.cpp:51:11: warning: unused variable ‘i’ [-Wunused-variable]
     int n,i,j,m,k;
           ^
proj.cpp:51:13: warning: unused variable ‘j’ [-Wunused-variable]
     int n,i,j,m,k;
             ^
proj.cpp:51:15: warning: unused variable ‘m’ [-Wunused-variable]
     int n,i,j,m,k;
               ^
proj.cpp:51:17: warning: unused variable ‘k’ [-Wunused-variable]
     int n,i,j,m,k;
                 ^
proj.cpp: In function ‘char predictWinner(const double*, const double*, const double*, const double*, char*, int)’:
proj.cpp:44:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

The last one is a potential problem.
Putting return '?'; as an unconditional statement at the end would stop this, and make it obvious your logic is wrong if you ever see a ? as the answer.

Then I ran this, and got 'A'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ ./a.out 
Enter the length of all the four arrays:3

Enter any value from 0 to 100 for Polling A:22
Enter any value from 0 to 100 for Polling B:33
Enter any value from 0 to 100 for Polling C:44
Enter the weight of Poll No 1: 4
Enter any value from 0 to 100 for Polling A:55
Enter any value from 0 to 100 for Polling B:44
Enter any value from 0 to 100 for Polling C:33
Enter the weight of Poll No 2: 6
Enter any value from 0 to 100 for Polling A:23
Enter any value from 0 to 100 for Polling B:1
Enter any value from 0 to 100 for Polling C:0
Enter the weight of Poll No 3: 3

Overall Predicted Winner=A


You need to supply an actual test case which you say fails for more thoughts.
Should sumA, sumB and sumC be integers or floating point numbers?
I added a return '?' at the end and got the same results as before. I also forgot to mention that the auto grader ignores everything outside of the main function and only looks at the function definition. The auto grader will only call the predictWinner function and pass in certain arguments that should return the expected result. When running and testing the code in a compiler it gives the correct results each time but not when used in the auto grader. So, is it possible I've included something in the main function that should be inside of the predictWinner function?
Try adding some debugging code when running it with the autograder. I'd start by printing sumA, sumB and sumC at line 29.

If that doesn't help, print out a[i], b[i], c[i] and pollweights[i] inside the loop.
> So, is it possible I've included something in the main function that should be inside of the predictWinner function?
It could be anything.

Perhaps you should post your actual definition of what predictWinner is supposed to do. At least we could tell whether you've omitted or mis-interpreted a part of the specification.

> results[i]='A';
What's the purpose of doing this?
You fill an array with ABC's, but the result never gets used by your code.

Or does it get used?

Because if it is used, and the user (aka autochecker) is expecting there to be a \0 at the end of the string, then it's going to be disappointed.
Topic archived. No new replies allowed.