Highest and lowest number in one structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mimiyuu
    New Member
    • Dec 2015
    • 2

    Highest and lowest number in one structure

    what I'm gonna do in my problem program? Can you help me in that one?
    Create a program that you have input 5 numbers and in the output will identify the highest and lowest number. Seletion structure in turbo c++
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First write a program the lets you enter five numbers and display them.

    Get that working then add the logic to find the smallest/largest number.

    Post some code so I can see hoe you are doing.

    Comment

    • Mimiyuu
      New Member
      • Dec 2015
      • 2

      #3
      Code:
      main ()
      
      {
      int a,b,c,d,e;
      clrscr();
      p("Enter a number: ");
      scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
      if ( a<b && a<c && a<d && a<e )
      p("Lowest number: %d\n",a);
      else if (a>b && b>c && c>d && d>e )
      p("Highest number: %d\n",a);
      else if ( b<a && b<c && b<d && d<e )
      p("Lowest number: %d\n",b);
      else if ( b>a && b>c && b>d && b>e )
      p("Highest number: %d\n",b);
      else if ( c<a && c<b && c<d && c<e )
      p("Lowest number: %d\n",c);
      else if (c>a && c>b && c>d && c>e )
      p("Highest number: %d\n",c);
      else if ( d<a && d<b && d<c && d<e )
      p("Lowest number: %d\n",d);
      else if ( d>a && d>b && d>c && d>e )
      p("Highest number: %d\n",d);
      else if ( e>a && e>b && e>c && e>d )
      p("Lowest number: %d\n",e);
      else if ( e<a && e<b && e<c && e<d );
      p("Highest number: %d\n",e);
      can you checked,what's wrong on my code?
      Last edited by zmbd; Dec 13 '15, 03:29 PM.

      Comment

      • jaseel97
        New Member
        • Feb 2015
        • 16

        #4
        Why dont you assume one value, say a, as highest and an other, maybe e, as the lowest and check each value(b,c,d,e and a,b,c,d) one by one against the assumed values. This logic should reduce the number of comparisons required. It'll look a lot cleaner too.

        Comment

        Working...