adplus-dvertising

Print the 2 highest value in c++

Asked 3 months ago
Viewed 27 times
c++

How do you print the two highest value. heres an example I asked for 4 numbers how do i print the two highest value input?

#include <iosteam>
using namespace std;

int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;

return 0;
}

Heres my original code, I of the values of the ap bp cp and dp doesnt reach 50 it will supposed to print the two highest. Heres my original code, I of the values of the ap bp cp and dp doesnt reach 50 it will supposed to print the two highest.

#include <iostream>
#include <stdlib.h>
using namespace std; 


int main(){
    string candidate[4];
    int a[5], b[5], c[5], d[5];//precint
    float at,bt,ct,dt;
    float ap, bp, cp, dp;
    int total;
    int choice;
    char yesNo;
    int i;
    
    do{ 
    system("CLS");
    
    cout<<"[1] Enter candidates names and votes"<<endl;
    cout<<"[2] Show Tally"<<endl;
    cout<<"[3] Exit"<<endl;
    cout<<"Enter a choice: ";
    cin>>choice;
    switch(choice){
        case(1):
    
        for(int i=0; i<4; i++){
        cout<<"Enter candidate names: ";
        cin>>candidate[i];}
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[0]<<" precinct "<<i+1<<" votes: ";
        cin>>a[i];
        }
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[1]<<" precinct "<<i+1<<" votes: ";
        cin>>b[i];
        }
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[2]<<" precinct "<<i+1<<" votes: ";
        cin>>c[i];
        }
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[3]<<" precinct "<<i+1<<" votes: ";
        cin>>d[i];
        }
        break;
        case(2):
            cout<<"Precinct\t"<<candidate[0]<<"\t\t"<<candidate[1]<<"\t\t"<<candidate[2]<<"\t\t"<<candidate[3]<<endl;
            
            for(int i=0; i<5; i++){ //for displaying the tally
                cout<<i+1<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<"\t\t"<<c[i]<<"\t\t"<<d[i]<<endl;
            }
            cout<<endl;
            for(int i=0; i<5; i++){ //for the total votes
                at=at+a[i];
                bt=bt+b[i];
                ct=ct+c[i];
                dt=dt+d[i];
                                
            }
            total=at+bt+ct+dt;  //for the percent
            ap=(at/total)*100;
            bp=(bt/total)*100;
            cp=(ct/total)*100;
            dp=(dt/total)*100;
            
            cout<<"Candidate "<<candidate[0]<<" total votes and percentage: "<<at<<" votes. "<<ap<<"%"<<endl;
            cout<<"Candidate "<<candidate[1]<<" total votes and percentage: "<<bt<<" votes. "<<bp<<"%"<<endl;
            cout<<"Candidate "<<candidate[2]<<" total votes and percentage: "<<ct<<" votes. "<<cp<<"%"<<endl;
            cout<<"Candidate "<<candidate[3]<<" total votes and percentage: "<<dt<<" votes. "<<dp<<"%"<<endl;
            cout<<endl;
            if (ap>50){
                cout<<"Candidate "<<candidate[0]<<" is the WINNER!"<<endl;
            }else if (bp>50){
                cout<<"Candidate "<<candidate[1]<<" is the WINNER!"<<endl;
            }else if (cp>50){
                cout<<"Candidate "<<candidate[2]<<" is the WINNER!"<<endl;
            }else if (dp>50){
                cout<<"Candidate "<<candidate[3]<<" is the WINNER!"<<endl;
            }
            }
            cout<<endl;
            
        
        
        cout<<"Do you want to continue(Y/N): ";
        cin>>yesNo;
    }while (yesNo == 'y' || yesNo == 'Y');
        cout<<"Thank You!";
    
    

    
    
    return 0;
}

asked 3 months ago

Correct Answer

A simple solution is to put them all in an array then sort it.

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int v[] = {a, b, c, d};
    sort(v, v + 4);
    // largest = v[3];
    // second largest = v[2];
}
answered 3 months ago

Other Answer

I have created a sample array and find the two highest number. Time and memory complexity is O(n).

#include<iostream>
using namespace std;

int main(){
int a,b,c,d,max1 = 0, max2 = 0;
int arr[4] = {1, 5, 7, 3};

for(int i = 0; i < 4; i++){
    if(arr[i] > max1){
        max2 = max1;
        max1 = arr[i];
    }
    else if(arr[i] > max2){
        max2 = arr[i];
    }
}
cout << max1 << " " << max2 << endl;
return 0;
}
answered 3 months ago