![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2007
Posts: 41
Rep Power: 0
![]() |
homeWork
#include "stdio.h"
int main (void)
{
/* Assigns variables and assigns them to zero */
int bigInt = 0;
int smallInt = 0;
int randomInt = 0;
int count = 0;
while(count <= 4)
{
printf("Enter a number :");
scanf("%d", &randomInt);
count = ++count;
if(randomInt >= smallInt)
{
smallInt = randomInt;
}
if(randomInt >= bigInt)
{
smallInt = bigInt;
bigInt = randomInt;
}
}
printf("The largest number is : %d\n", bigInt);
printf("The second largest number is : %d\n", smallInt);
return 0;
}The assignment was to take in 5 random numbers and output the largest two of them. When I was looking at other peoples algorithms they all had something in common(the people who used loops) They all checked if the number they entered was higher than the highest first; While I thought that if the number wasn't larger then the smallest they was no need to check further an you could just go get the next number. In my code I think even if it doesn't go true for randomInt >= smallInt it still goes and checks if randomInt >= bigInt. How do I get it to just stop if the first if statement fails and just return to go through the loop again? |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Re: homeWork
To just go to the next iteration of the loop, you can use
continue;, and to quit the loop you can use break;. However, I'd suggest the following change for your logic (see the comments): c Syntax (Toggle Plain Text)
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2007
Posts: 41
Rep Power: 0
![]() |
Re: homeWork
I think you kind of missed the point I was trying to get at. Thats easy enough. But if the new number isn't as big as the smallest number already they isn't a reason to continue with the loop. Ive already wrote it like that also. Sorry for the confusion. I want to drop out of the loop and immediatly go get another number if the first number isn't as large as the the one in the lowest place already.
I don't think continue or break will work fr what i'm trying to say. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2008
Location: teh interwebz
Posts: 22
Rep Power: 0
![]() |
Re: homeWork
i think that if you put an ELSE in front of your second IF it will do what you are trying to say... i could be confused though
and the way you two are going about it is exactly the same just with the IF statements flipped...
__________________
iEngage Last edited by iEngage; Jun 1st, 2008 at 8:36 PM. Reason: clarity |
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Oct 2007
Posts: 41
Rep Power: 0
![]() |
Re: homeWork
Quote:
Im not arugueing.. The second one needs to be checked if the first one comes back true. |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2008
Location: teh interwebz
Posts: 22
Rep Power: 0
![]() |
Re: homeWork
oh ok i see what your trying to say... sorry.
try it like this: c++ Syntax (Toggle Plain Text)
Although.. the way Jimbo has it is cleaner, and checking it one extra time really won't lose much processing time at all... you won't even notice a difference
__________________
iEngage |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Oct 2007
Posts: 41
Rep Power: 0
![]() |
Re: homeWork
ok thanks for the help. I actually hopes this class gets harder. They was a thousand ways I thought about how to do this it seems. They aren't suppose to be but about 10 "projects" from the begining till the end. You should of seen the flow chart the for the first way I had it. It was a five page print-out.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2008
Location: teh interwebz
Posts: 22
Rep Power: 0
![]() |
Re: homeWork
hehe sounds like fun
![]()
__________________
iEngage |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Oct 2007
Posts: 41
Rep Power: 0
![]() |
Re: homeWork
Yes, but sometimes I fall asleep in class. Its just like high school. I think I need some focus meds. It's just tech school I don't know how in depth it will go. I'm taking C over the summer with a web-design class. Two online classes but both of them are remedial. From the syllabus its only going to get to chapter 11 in the book that has 27 chapters. He says they use the same book for C++ so I guess it will be coming in the fall semester. I really don't know how he's going to teach object oriented programming from a C book, but I guess its possible.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| java homework.. please HELP!! | zxasqwedc | Java | 26 | Apr 27th, 2005 12:44 PM |
| HELP!!! Dialog box, inputoutput, and absolute value homework | crybabyland | Java | 1 | Feb 17th, 2005 12:30 PM |
| simple homework | rselge | Java | 3 | Feb 16th, 2005 4:45 PM |
| Need help with homework | evnp85 | Java | 2 | Feb 14th, 2005 3:05 PM |