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

Simple C++ program to implement 8-ball problem with 4 ball 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<4;i++)
    {
        side1=side1+a[i];
        side2=side2+a[i+4];
    }
    cout<<"putting each side 4 balls: "<<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 having 4 balls into group of 2 balls:"<<endl;
        side1=side2=0;
        for(int i=0;i<2;i++)
        {
            side1=side1+a[i];
            side2=side2+a[i+2];
        }
        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 single balls: "<<endl;
            side1=side2=0;
            side1=a[0];side2=a[1];
            cout<<endl<<"\t\tside 1\t\t\tside 2"<<endl;
            cout<<"\t\t"<<side1<<"\t\t\t"<<side2<<endl;
            if(side1>side2){cout<<"ball having index number 1 is having more weight"<<endl;}
            else{cout<<"ball having index number 2 has more weight"<<endl;}
        }
        else{
            cout<<"side 2 has more weight,so dividing side 1 into group of single balls: "<<endl;
            side1=side2=0;
            side1=a[2];side2=a[3];
            cout<<endl<<"\t\tside 1\t\t\tside 2"<<endl;
            cout<<"\t\t"<<side1<<"\t\t\t"<<side2<<endl;
            if(side1>side2){cout<<"ball having index number 3 is having more weight"<<endl;}
            else{cout<<"ball having index number 4 has more weight"<<endl;}
        }
        }

    else{

        cout<<"side 2 has more weight, so dividing side 2 having 4 balls into group of 2 balls:"<<endl;
        side1=side2=0;
        for(int i=4;i<6;i++)
        {
            side1=side1+a[i];
            side2=side2+a[i+2];
        }
        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 single balls: "<<endl;
            side1=side2=0;
            side1=a[4];side2=a[5];
            cout<<endl<<"\t\tside 1\t\t\tside 2"<<endl;
            cout<<"\t\t"<<side1<<"\t\t\t"<<side2<<endl;
            if(side1>side2){cout<<"ball having index number 5 is having more weight"<<endl;}
            else{cout<<"ball having index number 6 has more weight"<<endl;}
        }
        else{
            cout<<"side 2 has more weight,so dividing side 1 into group of single balls: "<<endl;
            side1=side2=0;
            side1=a[6];side2=a[7];
            cout<<endl<<"\t\tside 1\t\t\tside 2"<<endl;
            cout<<"\t\t"<<side1<<"\t\t\t"<<side2<<endl;
            if(side1>side2){cout<<"ball having index number 7 is having more weight"<<endl;}
            else{cout<<"ball having index number 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