You must Sign In to post a response.
  • Category: .NET

    Need to help for below program correct it..

    Hi Friends

    Please find below logic and i wrote coding those logic. if its not correct means help to correct it.

    Question :

    1) Input is 5 similar size tennis balls, 4 weigh exactly the same and the last one is heavier than other 4.
    2) Find the heaviest ball in 3 iteration


    Code :

    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    int i,min,max,Weight[5];
    clrscr();
    printf("enter 5 Size Balls:");
    for(i=0;i<5;i++)
    {
    scanf("%d",&arr[i]);
    }
    min=arr[0];
    max=arr[0];
    for(i=0;i<=3;i=i+2)
    {
    if(i==0)
    {
    if(Weight[i] > Weight[i+1];
    }
    }
    printf("\n%d is Heviest Size",max);
    getch();
    }

    thanks
    Sriram.R
  • #758201
    Hi Sriram,
    change your code as commented just right

    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    int i,min,max,Weight[5];
    clrscr();
    printf("enter 5 Size Balls:");
    for(i=0;i<5;i++)
    {
    scanf("%d",&arr[i]);
    }
    min=arr[0]; //no need of this
    max=arr[0]; //no need of this
    for(i=0;i<=5;i=i+2) // here ,i<=5 if as you start with 0 and increment with 2 so value of i will be 0,2,4
    {
    //change here no need of these removed line
    if(i==4) //in this case means last is heaviest
    {
    printf("\n%d is Heviest Size",Weight[i]);
    }
    else if(Weight[i] > Weight[i+1])
    {
    printf("\n%d is Heviest Size",Weight[i]);
    break;
    }

    }

    Regards & thanks
    Arvind kumar
    Visit--blog.akumars.esoftera.in

  • #758203
    Hi aravind .

    Please find below code first time am getting Heivest values. second time apply not getting correct value.

    please find the code

    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    int i,Weight[5];
    clrscr();
    printf("enter 5 Size Balls:");
    for(i=0;i<5;i++)
    {
    scanf("%d",&arr[i]);
    }

    for(i=0;i<=5;i=i+2)
    {

    if(i==4)
    {
    printf("\n%d is Heviest Size",Weight[i]);
    }
    else if(Weight[i] > Weight[i+1])
    {
    printf("\n%d is Heviest Size",Weight[i]);
    break;
    }

    }

    thanks
    Srriam.R

  • #758205
    Hi Aravind,

    its correct any anything to change if i run the program first time am getting Heiviest value ,next time not getting.but i cant able to run 3 Iterations it for single Iteration only running.

    if anything i missed..

    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    int i,Weight[5];
    clrscr();
    printf("enter 5 Size Balls:");
    for(i=0;i<5;i++)
    {
    scanf("%d",&Weight[i]);
    }

    for(i=0;i<=5;i=i+2)
    {

    if(i==4)
    {
    printf("\n%d is Heviest Size",Weight[i]);
    }
    else if(Weight[i] > Weight[i+1])
    {
    printf("\n%d is Heviest Size",Weight[i]);
    getch();
    break;
    }


    }}

  • #758272
    Hello sriram,

    a simpler way can also be applied like following:-
    1> divide balls in 2 groups of 2 and compare weight.
    2> If group A weights more then divide it them into 2 i.e 1 each and weight it again. Similarly if group B weights more.
    3> if both groups are equal then definately 5th ball weights more.

    ex:-
    5 balls viz. a,b,c,d,e.
    divide it into a-b and c-d group.
    if a-b is more then compare a-b.
    if c-d is more then compare c-d.
    if both equal e is the answer.


    int a = 2;
    int b = 1;
    int c = 1;
    int d = 1;
    int e = 1;
    int sum1 = 0;
    int sum2 = 0;
    sum1 = a + b;
    sum2 = c + d;
    if (sum1 > sum2)
    {
    if (a > b)
    {
    Console.WriteLine(a + "is heaviest");
    }
    else
    {
    Console.WriteLine(b + "is heaviest");
    }
    }
    else if (sum2 > sum1)
    {
    if (c > d)
    {
    Console.WriteLine(c + "is heaviest");
    }
    else
    {
    Console.WriteLine(d + "is heaviest");
    }
    }
    else if (sum1 == sum2)
    {
    Console.WriteLine(e + "is heaviest");
    }
    Console.ReadLine();


    simple demo.

    Regards,
    Shakil Sama


  • Sign In to post your comments