Home Forums C Programming Two things in an IF statement

Viewing 2 reply threads
  • Author
    Posts
    • #2212
      GladysMccurry
      Participant

      I have this program I wrote for a user to enter 5 Integers.
      Now I would like to modify it and determine what position the highest number was entered in.
      Was it the 1st ,2nd, 3rd, 4th or 5th entry that was largest?

      I would like to do something like:
      int count=0;
      -During my statement
      if (number > highNum)
      highNum = number and count++;

      That way when I am done count will be raised to the matching number of the highest entry.
      e.g. if my numbers were 4, 17, 38, 99, 2
      highNum = 99 and count = 4

      I am lost and new to C, I would appreciate any help.
      Thanks


      #include
      #define MAX 5

      int main()

      {

      int i = 1;
      int number = 0, highNum = 0;

      printf("This program will choose the largest number entered.");
      printf("nWe are going to enter five numbers.nn");

      for (i = 1; i <= MAX; i++)
      {
      printf("Enter an integer: ");
      scanf("%d", &number);

      if (number > highNum)
      highNum = number;
      else {} /* nothing */
      }

      printf("nThe largest number entered was %dn", highNum);

      return 0;
      }

    • #3592
      GWILouisaxwzkla
      Participant

      could do something like this:


      include
      #define MAX 5

      int main()

      {

      int i = 1;
      int number = 0, highNum = 0;
      int highPosition = 0;

      printf("This program will choose the largest number entered.");
      printf("nWe are going to enter five numbers.nn");

      for (i = 1; i <= MAX; i++)
      {
      printf("Enter an integer: ");
      scanf("%d", &number);

      if (number > highNum)
      {
      highNum = number;
      highPosition = i;
      }
      }

      printf("nThe largest number entered was %dn", highNum);
      printf("nThe number was in position %dn", highPosition );
      return 0;
      }

    • #3593
      GladysMccurry
      Participant

      Thank you, that is exactly what I was looking for.
      I wish my textbook had an example like that for an If statement.

Viewing 2 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.