Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 1st, 2008, 6:56 PM   #1
casesensitive
Programmer
 
Join Date: Oct 2007
Posts: 41
Rep Power: 0 casesensitive is on a distinguished road
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?
casesensitive is offline   Reply With Quote
Old Jun 1st, 2008, 8:10 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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)
  1. while(count <= 4)
  2. {
  3. printf("Enter a number :");
  4. scanf("%d", &randomInt);
  5. count = ++count;
  6. // first check if it's the biggest, as this case involves the most work
  7. if(randomInt >= bigInt)
  8. {
  9. smallInt = bigInt;
  10. bigInt = randomInt;
  11. }
  12. // if that didn't happen, check the other scenario
  13. else if(randomInt >= smallInt)
  14. {
  15. smallInt = randomInt;
  16. }
  17. // otherwise there's nothing to do, no need to add extra logic
  18. }
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Jun 1st, 2008, 8:14 PM   #3
casesensitive
Programmer
 
Join Date: Oct 2007
Posts: 41
Rep Power: 0 casesensitive is on a distinguished road
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.
casesensitive is offline   Reply With Quote
Old Jun 1st, 2008, 8:35 PM   #4
iEngage
Newbie
 
iEngage's Avatar
 
Join Date: May 2008
Location: teh interwebz
Posts: 22
Rep Power: 0 iEngage is on a distinguished road
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
iEngage is offline   Reply With Quote
Old Jun 1st, 2008, 8:38 PM   #5
casesensitive
Programmer
 
Join Date: Oct 2007
Posts: 41
Rep Power: 0 casesensitive is on a distinguished road
Re: homeWork

Quote:
Originally Posted by iEngage View Post
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...
No that wouldn't work. If I put an else in front the first statement may come back true and the second one wouldnt be evaluated at all.
Im not arugueing.. The second one needs to be checked if the first one comes back true.
casesensitive is offline   Reply With Quote
Old Jun 1st, 2008, 8:43 PM   #6
iEngage
Newbie
 
iEngage's Avatar
 
Join Date: May 2008
Location: teh interwebz
Posts: 22
Rep Power: 0 iEngage is on a distinguished road
Re: homeWork

oh ok i see what your trying to say... sorry.
try it like this:

c++ Syntax (Toggle Plain Text)
  1. while(count <= 4)
  2. {
  3. printf("Enter a number :");
  4. scanf("%d", &randomInt);
  5. count = ++count;
  6. if(randomInt >= smallInt)
  7. {
  8. smallInt = randomInt;
  9. }
  10. else
  11. {
  12. continue;
  13. }
  14. if(randomInt >= bigInt)
  15. {
  16. smallInt = bigInt;
  17. bigInt = randomInt;
  18. }
  19.  
  20. }

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
iEngage is offline   Reply With Quote
Old Jun 1st, 2008, 8:48 PM   #7
casesensitive
Programmer
 
Join Date: Oct 2007
Posts: 41
Rep Power: 0 casesensitive is on a distinguished road
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.
casesensitive is offline   Reply With Quote
Old Jun 1st, 2008, 9:12 PM   #8
iEngage
Newbie
 
iEngage's Avatar
 
Join Date: May 2008
Location: teh interwebz
Posts: 22
Rep Power: 0 iEngage is on a distinguished road
Re: homeWork

hehe sounds like fun
__________________
iEngage
iEngage is offline   Reply With Quote
Old Jun 1st, 2008, 10:19 PM   #9
casesensitive
Programmer
 
Join Date: Oct 2007
Posts: 41
Rep Power: 0 casesensitive is on a distinguished road
Re: homeWork

Quote:
Originally Posted by iEngage View Post
hehe sounds like fun
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.
casesensitive is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC