|
-
March 8th, 2009, 02:03 PM
#1
Homework Help!
Long time lurker, first time poster.
I know helping with homework isn't this websites real purpose, but I am completely stuck.
This is my first semester with C++ and we learned about while loops, and my homework was to was to read a file and perform several task with the integers inside and write the results to a output file. The one that I'm stuck on is finding the smallest and largest integer and writing them to the output file.
I've tried if/else statements in a while loops, nest while loops, but I just cant figure out how to go about this.
My latest attempt was
while(Filein)
{
while(Num < Num2 || Num < Num3)
{
Num3 = Num;
Filein << Num;
}
Filein >> Num;
Filein >> Num2;
}
This was my attempt at finding the smallest integer and assigning it to Num3. But as I've noticed it does not work, and don't really know where to go from here. Please help.
-
March 9th, 2009, 12:44 AM
#2
Re: Homework Help!
We really need to know the exact problem that you've been assigned.
I'm having to make a ton of assumptions based on the code you supplied above, as well as the statements made in your post, so with that said, any source code I write for you now would likely need rewritten entirely...
For instance, the following questions arise, and can only be answered by you...
- Does this problem require Dynamic Memory Allocation(Array-Out-Of-Bounds Error Prevention in storing data from file - assuming further that it's even needed to be stored.)?
- What type of "package" are you using to read and write files? (I assume <ofstream> and <ifstream>?)
- What's the scope of your task results? (you said "find the smallest and largest int"... On the current line in the file, or... what? The entire file maybe?)
- How should the output file be formatted(comma seperated? white space? etc?)?
- There's more, but hopefully you get my point by now!

Typing the exact problem assigned may benefit you alot. I was bored, so I was going to post a code example for you, but didn't because it probly won't work for your situation as I have no clue how your teacher wants it(Or do I? lol yea that was dirty, i know =/). We need to know the exact problem to give you accurate advice, regardless of whether you wanted free source code from someone like me or not.
I'll send it to you in a PM if you want it for reference or to get an idea of the methodology I used, just let me know..
More info please! Post all of your code preferably... Hopefully that snippet you typed above isn't the extent of your code. If it is, I suggest that you research something called Pseudo Code.
And welcome to codeguru.
Last edited by dcyuri7; March 9th, 2009 at 12:51 AM.
-
March 9th, 2009, 05:31 AM
#3
Re: Homework Help!
I suggest the following structure
Code:
while(FILE_HAS_MORE_NUMBERS) {
READ_NEXT_NUMBER;
if(IS_FIRST_NUMBER || NUMBER<SMALLEST_NUMBER)
SMALLEST_NUMBER=NUMBER;
if(IS_FIRST_NUMBER || NUMBER>LARGEST_NUMBER)
LARGEST_NUMBER=NUMBER;
}
Nobody cares how it works as long as it works
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|