Technoed
Technoed

Reputation: 17

Cannot answer letters C,D and E - ARRAY

Hi I need help I already got the A and B but for C-E I cannot get it
It needs to get the >=50 percent and print the Candidate name, I posted earlier about the sum suddenly, it also needs to get the highest number of votes and if there's no >=50 it needs to print the 2 largest and will print as run-off

enter image description here

Here is the code

//Prints table                  
    printf("Precint\t\tCandidate A\tCandidate B\tCandidate C\tCandidate D\n");
    for(rows=0; rows<5; rows++){
        printf("%d\t\t",k++);
        for(columns=0; columns<4; columns++){
            printf("%d\t\t",arr[rows][columns]);
        }   
        printf("\n");
    }
printf("\n\n");

//Computes Sum per Row
int Sum[4];
TSum=0;
for(rows = 0; rows < 4; rows++)
{     
    Sum[rows]=0;      
    for(columns = 0; columns < 5; columns++)
    {
        Sum[rows] += arr[columns][rows];
    }
    TSum += Sum[rows]; // only one operation per rows
}  

//Computes Percent and prints Table
double l1[4];
double l2[4];
double temp,i;

for(rows = 0; rows < 4; rows++)
{     
    Percnt[rows] = Sum[rows]/TSum*100;
    l++;
    printf("Candidate #%d Total:\t %d\t Percentage: %.0f%%\n",l, Sum[rows],Percnt[rows]);   

//finding largest   
  
        l1[rows] = Percnt[rows];
        l2[rows] = Percnt[rows];
        if (l1[rows] < l2[rows])
        {
            temp = l1[rows];
            l1[rows] = l2[rows];
            l2[rows] = temp;
        }

        for (i = 2; i < 4; i++)
        {
            if (Percnt[rows] > l1[rows])
            {
                l2[rows] = l1[rows];
                l1[rows] = Percnt[rows];
            }
            else if (Percnt[rows] > l2[rows] && Percnt[rows] != l1[rows])
            {
                l2[rows] = Percnt[rows];
            }
        }  
//end
}

printf("\nSum: %.0f\n", TSum );
printf ("The FIRST LARGEST = %.0f\n", l1[rows]);
printf ("THE SECOND LARGEST = %.0f\n", l2[rows]);

enter image description here

Upvotes: 0

Views: 36

Answers (1)

Shobhit Kumar
Shobhit Kumar

Reputation: 666

The part of the code where the l1[rows] and l2[rows] are assigned values seem wrong, they will have the same value each time. in the for loop that follows. you haven't made use of the variable í. Check the logic again

Upvotes: 1

Related Questions