"nick048" <ni*************@moonsoft.itwrote in message
news:11**********************@e64g2000cwd.googlegr oups.com...
Hi,
I am a new user of C++ and I am learning this language.
I want to resolve this example:
1) I have an indefinite sequence of integer (from 0 to 36) as Input;
2) I want to know the maximum number of occurrences and its value.
eg Input: 0, 2, 5, 2, 23, 11, 11, 23, 15, 17, 25, 11, 23, 18, 23
(Ctrl+z)
Output: Value is 23 Occurences is 4
For the input I have written this code:
iNumber = 0; // Counter
while (1)
{ //read next
cout << "Enter number (Ctrl-z = Input End): ";
cin >myNumber;
Okay, you input myNumber, but you aren't doing anything with it. You
probably want to store it somewhere, prehaps a std::vector<int>. Code
fragments, since this looks suspiciously like homework, but you did show
some code, so I can show you some.
#include <vector>
....
std::vector<intData;
....
Data.push_back( myNumber );
if (cin.eof())
break;
//update the counter
iNumber++;
I really don't know what you plan on doing with iNumber, all it's doing is
counting how many numbres have been input. But if you use a std::vector you
can find out by using (I think) .size().
}
Okay, if you pushed the numbers into the vector, now you need to go through
them and do your second step. There are two ways to iterate through
vectors, using them like arrays [x] or using an iterator. I'll show you
code snippets of both. I prefer iterators myself.
for ( int i = 0; i < Data.size(); ++i )
{
// Do something with Data[i] here which will be one of the numbers.
}
for ( std::vector<int>::iterator it = Data.begin(); it != Data.end(); ++it )
{
// Do something with *it here which will be one of the numbers.
}
>
But I am in difficulty, when I try to resolve the second step. The
solution don't have to use the array.
I hope in Your help in order to resolve my problem without array or map
or struct.
Thank You and Best Regards
Nino
Look at what I read, read up on vectors, try to use them to solve your
problem. If you get stuck again, show your code and where you get stuck.