C++ program to identify heaviest ball from 8-ball problem (3-3-2 method)

Easy C++ code for 8-ball problem with 3-3-2 method

Code:

#include <iostream>

using namespace std;

int main()
{
    int a[8],side1=0,side2=0;
    for(int i=0;i<8;i++)
    {
    cout<<"Enter the weight of ball no "<<i+1<<endl;
    cin>>a[i];
    }
    for(int i=0;i<3;i++)
    {
        side1=side1+a[i];
        side2=side2+a[i+4];
    }
    cout<<"putting each side 3 balls and remaining two will be out"<<endl;
    cout<<endl<<"\t\tside 1\t\t\tside 2"<<endl;
    cout<<"\t\t"<<side1<<"\t\t\t"<<side2<<endl;
    if(side1>side2)
    {
        cout<<"side 1 has more weight, so dividing side 1 into group of two and remaining onw ball would be out"<<endl;
        side1=side2=0;
        side1=a[0];side2=a[1];
        if(side1>side2){cout<<"ball having index 1 has more weight"<<endl;}
        else if(side1<side2){cout<<"ball having index  has more weight"<<endl;}
        else {cout<<"ball having index 3 has more weight"<<endl;}
    }
    else if(side1<side2)
    {
        cout<<"side 2 has more weight, so dividing side 1 into group of two and remaining onw ball would be out"<<endl;
        side1=side2=0;
        side1=a[3];side2=a[4];
        if(side1>side2){cout<<"ball having index 4 has more weight"<<endl;}
        else if(side1<side2){cout<<"ball having index 5 has more weight"<<endl;}
        else {cout<<"ball having index 6 has more weight"<<endl;}
    }
    else{
        cout<<"one of ball which is out of weighing machine is having more weight"<<endl;
        side1=side2=0;
        side1=a[6];side2=a[7];
        if(side1>side2){cout<<"ball having index 7 has more weight"<<endl;}
        else {cout<<"ball having index 8 has more weight"<<endl;}
        }
    return 0;
}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Java Program to Implement sorting algorithm using TCP on Server application

C++ program to perform data transformation Min-max and Z score Normalization