By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,132 Members | 1,948 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,132 IT Pros & Developers. It's quick & easy.

How to count some repetitions

P: n/a
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;
if (cin.eof())
break;
//update the counter
iNumber++;
}

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

Nov 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"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.
Nov 1 '06 #2

P: n/a
In article <11**********************@e64g2000cwd.googlegroups .com>,
"nick048" <ni*************@moonsoft.itwrote:
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;
if (cin.eof())
break;
//update the counter
iNumber++;
}

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.
You are braking the problem down the wrong way (or not breaking it down
enough as the case may be.) You should take a "verticle slice" of the
problem. This means instead of trying to do the "first step" to
completion, you should try to do all the steps, but with a limited data
set.

So for example, get your program working for just one input value. I.E.,
make a program that can accept a single number as input and print the
correct result. Then modify your program to accept two values, then
three, &c. By the time you get to three, you should start seeing where
you can use loops and arrays to remove duplication, and thus have a
general solution that can work with any number of inputs.

Here is a little something to get you started:

int main()
{
cout << "Enter a number from 0 to 36: ";
int number = -1;
cin >number;
assert( number >= 0 && number <= 36 ); // need real error checking?
cout << "The most numerous in the sequence is: " << number;
cout << "It occurs " << 1 << "time(s).";
}

Now modify the above so it will work with more than one input where all
the values entered are the same. When you get that working post your
result back here and we'll help you with the next step.

--
To send me email, put "sheltie" in the subject.
Nov 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.