Home      Affiliated Colleges      Course content      First Sem     Second Sem     Third Sem     Fourth Sem     Fifth Sem     Sixth Sem     Seventh Sem     Eighth Sem     Lab report 4th sem     Contact    

Monday, August 9, 2010

Design Analysis And Algorithm Program 1

1.  You are given 12 balls of identical shape all of which, but one, are of same weight. You are also given a balance. Find the ball of the irregular weight and its type of irregularity (heavier or lighter), by using balance only 4 times.
Soln:
Program Code:
#include<stdio.h>
#include<conio.h>
 void main()
 {
      int p[12],s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,i;
      clrscr();
      printf("\n Enter the weights of 12 balls \n");
      for(i=0;i<12;i++)
              scanf("%d",&p[i]);

      for(i=0;i<6;i++)
              s1+=p[i];
      for(i=6;i<12;i++)
              s2+=p[i];

      s3=p[0]+p[1]+p[2];
      s4=p[3]+p[4]+p[5];
      s5=p[6]+p[7]+p[8];
      s6=p[9]+p[10]+p[11];

      if(s3==s4 && s3==s5)
      {
            if(s6>s3)
            {
                        if(p[9]==p[10])
                                    printf("\n Ball11 is heavier");

                        else if(p[9]==p[11])
                                    printf("\n Ball10 is heavier");

                        else
                                    printf("\n Ball9 is heavier");
            }
            else
            {
                        if(p[9]==p[10])
                                    printf("\n Ball11 is lighter");

                        else if(p[9]==p[11])
                                    printf("\n Ball10 is lighter");

                        else
                                    printf("\n Ball9 is lighter");
            }

      }

      if(s3==s4 && s3==s6)
      {
            if(s5>s3)
            {
                        if(p[7]==p[8])
                                    printf("\n Ball9 is heavier");

                        else if(p[7]==p[9])
                                    printf("\n Ball8 is heavier");

                        else
                                    printf("\n Ball7 is heavier");
            }
            else
            {
                        if(p[7]==p[8])
                                    printf("\n Ball9 is lighter");

                        else if(p[7]==p[9])
                                    printf("\n Ball8 is lighter");

                        else
                                    printf("\n Ball7 is lighter");
             }
      }

      if(s3==s5 && s3==s6)
      {
            if(s4>s3)
            {
                        if(p[4]==p[5])
                                    printf("\n Ball6 is heavier");

                        else if(p[4]==p[6])
                                    printf("\n Ball5 is heavier");

                        else
                                    printf("\n Ball4 is heavier");
            }
            else
            {
                        if(p[4]==p[5])
                                    printf("\n Ball6 is lighter");

                        else if(p[4]==p[6])
                                    printf("\n Ball5 is lighter");

                        else
                                    printf("\n Ball4 is lighter");
            }
      }

      if(s4==s5 && s4==s6)
      {
            if(s3>s6)
            {
                        if(p[1]==p[2])
                                    printf("\n Ball3 is heavier");

                        else if(p[1]==p[3])
                                    printf("\n Ball2 is heavier");

                        else
                                    printf("\n Ball1 is heavier");
            }
            else
            {
                        if(p[1]==p[2])
                                    printf("\n Ball3 is lighter");

                        else if(p[1]==p[3])
                                    printf("\n Ball2 is lighter");

                        else
                                    printf("\n Ball1 is lighter");
            }
      }

      getch();
 }




No comments:

Post a Comment

^ Scroll to Top Related Posts with Thumbnails ^ Go to Top