Counting Occurrence of numbers

Hey guys,

Just got this new assignment and really don't know where to start. Any help would be great. I'm not a Computer Science major. I have a Bachelors in Graphic Design and am taking this as a pre-requisite for an HTML course over the summer I'm planning to take. Heres the assignemnt:

Write a Program that reads the integers between 1 and 100 and counts the occurrence of each number. Assume the input ends with 0.

**Note** if a number occurs more than one time, the plural word "times" is used in the output.


Thanks guys!
PS -- it has to written in C++
What is the source of the input, and how is it formatted?
Hey man,

all the codes start off like this...

1
2
#include <iostream>
using namespace std;
In the book, here is the sample run of the program:

Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0

2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
Ok, heres some code I came up with. I know it needs some hacking though cause its not working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <ctime>
#include "InsertionSort.h"
using namespace std;

int main()
{

	double list[100];
	double integers[100];
	int numberOfIntegers = 1;

	cout<<"Enter some integers between 1 and 100: "; //Prompts the user to enter integers between 1 and 100

	for (int i = 0; i < numberOfIntegers; i++)
	{
		cin>>list[i]; //gets integers from user
		numberOfIntegers++;

	for (int j = 0; j<100; j++)
	{
		if (list[i] = j)
		integers[j]++;
		else
			integers[j] = 0;
	
	for (int j = 0; j<100; j++)
	{
		// the following code displays the results
		if (integers[j] != 1 && integers[j] != 0){
			cout<< integers[i] << " occurs " << integers[j] << " times"<<endl;
			break;}
		else if (integers[j] == 1 && integers[j] != 0){
			cout<< integers[i] << " occurs 1 time"<<endl;
			break;}
		}
	}
	}
	int pause;
	cin>>pause;
	return 0;
}
It's a homework assignment, so I'm not going to show you code, but that said, I can point you in the right direction.

Try using a loop to parse all of the integers entered into a list, use an iterator to page through the list, and count the integers that are in there, then display any values that occur.
You're starting off on the right track. Here's some comments that might help you out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <ctime>
#include "InsertionSort.h"
using namespace std;

int main()
{

	double list[100]; // Don't use doubles to store int
	double integers[100]; // Don't use doubles to store int
	int numberOfIntegers = 1;

	cout<<"Enter some integers between 1 and 100: "; //Prompts the user to enter integers between 1 and 100

	for (int i = 0; i < numberOfIntegers; i++) // This loop will never, ever end.
	{
		cin>>list[i]; //gets integers from user
		numberOfIntegers++;

	for (int j = 0; j<100; j++)
	{
		if (list[i] = j)
		integers[j]++;
		else
			integers[j] = 0; // You just reset your count. The program will only return the very last number counted.
	
	for (int j = 0; j<100; j++)
	{
		// the following code displays the results
		if (integers[j] != 1 && integers[j] != 0){
			cout<< integers[i] << " occurs " << integers[j] << " times"<<endl;
			break;}
		else if (integers[j] == 1 && integers[j] != 0){ // By definition, if something == 1, then the same thing != 0. This is a redundancy.
			cout<< integers[i] << " occurs 1 time"<<endl;
			break;}
		}
	}
	}
	int pause;
	cin>>pause;// Use cin.get();
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main (void)
{
    string num = "";
    char search = '0';
    int i = 0;
    int count = 0;
    
    cout << endl << "Enter Number: ";
    cin >> num;

    cout << endl << "Enter Search Value: ";
    cin >> search;
    
    for (i = 0; i < num.length(); i++)
    {
        //cout << num[i] << endl;
        if ( num[i] == search )
        {
             count++;
        }
    }
    
    cout << endl << "Number of occurences: " << count << endl;
    
    system("PAUSE");
    return 0;
}


I know Im closer but should this work?
You are a bit closer, but you'll never get a for() loop to work right. Look up while() loops.
closed account (zb0S216C)
but you'll never get a for() loop to work right

Care to explain why?
He's trying to parse a single line, which means the end of the for loop has to be defined at runtime, and unless he's willing to take argc and argv[] under consideration, there's no way for the compiler (that I know of) to predefine a variable for the max iterations of a for loop.
@ ciphermagi: Do you mean at compile time? You are contridicting yourself right now. That's irrelevant though since a for loop will indeed allow you to use a variable that isn't defined until the program is executed (run time) for the end condition. I do this constantly to iterate through vectors like this for(int i = 0; i < MyVector.size(); i++).

EDIT: Also, as long as you keep in mind that you are comparing chars then his for loop could work.

@ OP: Do you know how to use structures yet? I just did something like this as a personal project and they help keep things straight.
Last edited on
@Computergeek01: Sure, he can use a vector to do it, but without a vector, there's not a way of terminating the for.
I know this is wrong but could someone show me where I went wrong:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

#define max_amount_of_numbers 100

#define biggest_allowed_number 100
#define lowest_allowed_number 1

#define end_of_input "0"

int main ()
{
    string num[max_amount_of_numbers];
    string search;

    cout << endl << "Enter Numbers: " << endl;

    int i = 0;
    while(cin>>num[i]){
      if(num[i]==end_of_input) break;

      if((atoi(num[i].c_str())>=lowest_allowed_number)&&(atoi(num[i].c_str())<=biggest_allowed_number))
        i++;
      else
        cout << "Imput out of range ("
             << lowest_allowed_number << " - " << biggest_allowed_number
             << "). Please try again"
             << endl;
    }

    cout << "Enter the search value: ";
    cin  >> search;
    cout << endl;

    int count = 0;
    while(i>-1){
      if( num[i].compare(search)==0 )count++;
      i--;
    }

    if(count==0)cout << "Given value did not occur.";
      else if(count==1)cout << "Given value occurred 1 time.";
        else cout << "Given value occurred: " << count << " times.";


    cin.get();
    return 0;
}
That looks identical to the code madredcake posted in http://www.cplusplus.com/forum/beginner/40000/#msg215958
...you should study up how to do it yourself. You might learn something.
@ nickd1085: Do you know how to use structures or not? This will help me show you how to do this in a way that won't drive you insane.
No, actually. Should I? My teacher is not the greatest for this class.

Thanks Computergeek01!
Topic archived. No new replies allowed.