-
March 25th, 2012, 07:57 AM
#1
Beginner needing help with program
Hi there,
I'm trying to write a console app in visual studio 2010 that will read a set of numbers and construct a histogram that shows how many numbers are between each two numbers...if that makes sense. To clear it up, i'm looking for an output like:
0 - 10 ***
11 - 20 ****
21 - 30 **
So in that example, 3 numbers (*) have been found in the read file between 0 and 10. 4 numbers (*) have been found in the read file between 11 and 20 and so on.
The read file looks like this:
1 2 24 13 11 9 20 28 16
So it's just a pretty basic txt file.
Any help on how to code this?
Thanks!
-
March 25th, 2012, 08:13 AM
#2
Re: Beginner needing help with program
What have you done so far?
-
March 25th, 2012, 08:27 AM
#3
Re: Beginner needing help with program
Nothing so far unfortunately :/
I'm not sure how I'd load the file but am I right in thinking that an if statement would be used for displaying that stars?
-
March 25th, 2012, 04:55 PM
#4
Re: Beginner needing help with program
Will your file be a continuous file with no returns or double spaces? Can you guarantee that? So just one continuous stream of “number space number space number space number” and so on?
If you can’t read a simple file into c++ the whole thing is a bit ambitious.
But what I would do is simply push each number into a vector as in: “vector <int> num;” and then iterate through num using 2 int variables:
Code:
void printNumHist(vector <int> num){
unsignedint strt=1, tens=10;
for (unsignedint i=0; i<num.size(); i++){
cout << "\n";
cout << strt" - "tens" = ";
while(num[i]<=tens){cout << “*”;}
tens=tens+10;
strt=tens-9;
}
}
This is non-tested pseudo code just typed into the box here…. Also, in order to create the vector I would need to have the answers to my first questions regarding the file format.
The vector also needs to be sorted….
Rigsby
-
March 25th, 2012, 05:10 PM
#5
Re: Beginner needing help with program
once I have your file format, I will explain how to read it into the vector and sort the vector.
-
March 25th, 2012, 08:16 PM
#6
Re: Beginner needing help with program
Originally Posted by Rigsby
Code:
while(num[i]<=tens){cout << “*”;}
I wouldn't bet on that this loop ever terminates...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
March 26th, 2012, 01:37 PM
#7
Re: Beginner needing help with program
Originally Posted by Eri523
I wouldn't bet on that this loop ever terminates...
LOL Oh dear….. You’re right, but he would have learned a very important lesson!
It was pseudo-code typed without testing!
Sorry dahrull! I have added the variable maxT as a quick and dirty solution, though I would probably look at another option to break;
Code:
void printNumHist(vector <int> num){
unsignedint strt=1, tens=10, maxT=num[num.size()-1];
for (unsignedint i=0; i<num.size(); i++){
cout << "\n";
cout << strt" - "tens" = ";
while(num[i]<=tens){cout << “*”;}
tens=tens+10;
strt=tens-9;
if(i>maxT){break;}
}
}
Well spotted Eri523! And thanks for letting me correct this myself!
-
March 26th, 2012, 01:56 PM
#8
Re: Beginner needing help with program
Why are you doing this guy's homework for him?
-
March 26th, 2012, 02:12 PM
#9
Re: Beginner needing help with program
Originally Posted by GCDEF
Why are you doing this guy's homework for him?
Oh GCDEF…. Seriously! Is this homework? Do you know that? This guy said he was a newbie! We all start somewhere! And I didn’t tell him how to read the file, which I suspect he was actually looking for ;-))
Also, a vector for this problem will work, but is a totally top-heavy solution for what he wants that any good teacher will jump on immediately, thus teaching him a further lesson! But if he is a genuine “newbie” the vector solution will serve him well, and teach him his first step into STL – so a win-win, don’t you think!
Why are you policing these threads?
-
March 26th, 2012, 02:26 PM
#10
Re: Beginner needing help with program
Originally Posted by Rigsby
Oh GCDEF…. Seriously! Is this homework? Do you know that? This guy said he was a newbie! We all start somewhere! And I didn’t tell him how to read the file, which I suspect he was actually looking for ;-))
Also, a vector for this problem will work, but is a totally top-heavy solution for what he wants that any good teacher will jump on immediately, thus teaching him a further lesson! But if he is a genuine “newbie” the vector solution will serve him well, and teach him his first step into STL – so a win-win, don’t you think!
Why are you policing these threads?
With 45 posts in 6 years, you're probably not familiar with how this forum works. We don't do homework for people, and that's very obviously homework.
And no, I don't think doing somebody's work for them is win/win.
I would advise you to come up with a consistent and meaningful style of indentation and alignment though.
-
March 26th, 2012, 02:31 PM
#11
Re: Beginner needing help with program
Originally Posted by GCDEF
With 45 posts in 6 years, you're probably not familiar with how this forum works. We don't do homework for people, and that's very obviously homework.
And no, I don't think doing somebody's work for them is win/win.
I would advise you to come up with a consistent and meaningful style of indentation and alignment though.
Why are you policing these threads?
-
March 26th, 2012, 02:34 PM
#12
Re: Beginner needing help with program
Originally Posted by GCDEF
With 45 posts in 6 years, you're probably not familiar with how this forum works. We don't do homework for people, and that's very obviously homework.
And no, I don't think doing somebody's work for them is win/win.
I would advise you to come up with a consistent and meaningful style of indentation and alignment though.
Why are you getting angry? And why are you „giving advice” on things other than code?
-
March 26th, 2012, 02:35 PM
#13
Re: Beginner needing help with program
Originally Posted by Rigsby
Why are you policing these threads?
I'm not going to engage in a pissing match with you. I'm just pointing out how the forum works. If I hadn't, somebody else would have.
-
March 26th, 2012, 02:40 PM
#14
Re: Beginner needing help with program
Originally Posted by Rigsby
Why are you policing these threads?
Don't do other peoples homework for them. That helps no-one.
-
March 26th, 2012, 02:41 PM
#15
Re: Beginner needing help with program
Originally Posted by GCDEF
I'm not going to engage in a pissing match with you. I'm just pointing out how the forum works. If I hadn't, somebody else would have.
I’m sorry, but I seriously do not understand what in God’s name you have for a bee in your bonnet about! If I do anything untoward then admin here will tell me and not you! And, words like p***ing are not very nice!
I am sorry if I embarrassed you over the int char thing! But that is not any reason to stalk my threads and hit me on “indentation” – I am on a Galaxy SII phone for Pete’s sake! Try indenting code on this thing.
Tags for this Thread
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
|