473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to count some repetitions

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
2 2801

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: knoak | last post by:
Hi there, I have a small question: I have a table with lots of rows in it. Of course all have a different id, but each can be assigned to a certain category. Categories correspond with 1 - 10....
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
6
by: Geetha | last post by:
I searched in the Oracle documents what count (1) meant and I could not find an answer. Can some one explain what Oracle does internally when use count (1) VS count (*). Thank you very much in...
4
by: darin dimitrov | last post by:
Hello, I need help with an algoritm that given a set of "n" distinct numbers will generate all the possible permutations of fixed length "m" of these numbers WITH repetitions (a total of n^m...
1
by: JD | last post by:
Hi guys I'm trying to write a program that counts the occurrences of HTML tags in a text file. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h> ...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
4
by: Branka | last post by:
Hi I have 20 'for' loops to create more than 40 million variations with repetitions. More precisely: I have total of nine factors with three possible levels (3^9) and 11 factors with 2 possible...
5
by: coolindienc | last post by:
Plz help me to find out how to count numbers of repetitions of loop and do output of it in C++. Andy
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.