Home › Forums › C Programming › Two things in an IF statement
- This topic has 2 replies, 2 voices, and was last updated 15 years, 5 months ago by GladysMccurry.
- AuthorPosts
- July 15, 2009 at 7:37 pm #2212GladysMccurryParticipant
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 = 4I am lost and new to C, I would appreciate any help.
Thanks1234567891011121314151617181920212223242526272829<br />#include <stdio.h><br />#define MAX 5<br /><br />int main()<br /><br />{<br /><br />int i = 1;<br />int number = 0, highNum = 0;<br /><br />printf("This program will choose the largest number entered.");<br />printf("nWe are going to enter five numbers.nn");<br /><br />for (i = 1; i <= MAX; i++)<br />{<br />printf("Enter an integer: ");<br />scanf("%d", &number);<br /><br />if (number > highNum)<br />highNum = number;<br />else {} /* nothing */<br />}<br /><br />printf("nThe largest number entered was %dn", highNum);<br /><br />return 0;<br />}<br /> - July 16, 2009 at 3:59 pm #3592GWILouisaxwzklaParticipant
could do something like this:
123456789101112131415161718192021222324252627282930313233<br />include <stdio.h><br />#define MAX 5<br /><br />int main()<br /><br />{<br /><br />int i = 1;<br />int number = 0, highNum = 0;<br />int highPosition = 0;<br /><br />printf("This program will choose the largest number entered.");<br />printf("nWe are going to enter five numbers.nn");<br /><br />for (i = 1; i <= MAX; i++)<br />{<br />printf("Enter an integer: ");<br />scanf("%d", &number);<br /><br />if (number > highNum)<br />{<br />highNum = number;<br />highPosition = i;<br />}<br />}<br /><br />printf("nThe largest number entered was %dn", highNum);<br />printf("nThe number was in position %dn", highPosition );<br />return 0;<br />}<br /><br /> - July 16, 2009 at 10:53 pm #3593GladysMccurryParticipant
Thank you, that is exactly what I was looking for.
I wish my textbook had an example like that for an If statement.
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.