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!
Re: Beginner needing help with program
What have you done so far?
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?
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
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. ;)
Re: Beginner needing help with program
Quote:
Originally Posted by
Rigsby
Code:
while(num[i]<=tens){cout << “*”;}
I wouldn't bet on that this loop ever terminates... ;)
Re: Beginner needing help with program
Quote:
Originally Posted by
Eri523
I wouldn't bet on that this loop ever terminates... ;)
LOL :eek: Oh dear….. You’re right, but he would have learned a very important lesson!
It was pseudo-code typed without testing!:D
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! :thumb:
Re: Beginner needing help with program
Why are you doing this guy's homework for him?
Re: Beginner needing help with program
Quote:
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?
Re: Beginner needing help with program
Quote:
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.
Re: Beginner needing help with program
Quote:
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?
Re: Beginner needing help with program
Quote:
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?
Re: Beginner needing help with program
Quote:
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.
Re: Beginner needing help with program
Quote:
Originally Posted by
Rigsby
Why are you policing these threads?
Don't do other peoples homework for them. That helps no-one.
Re: Beginner needing help with program
Quote:
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. :D