I have a problem with code I am writing. I am supposed to count the instances of each specific digit that the user inputs. I am supposed to output blank digit appears blank time(s). I figured out how to get the numbers to go to different indexes in the array but now I don't know how to count each specific digit. Here is my code so far...and if I am asking a dumb question please don't make fun of me I am new to this...:)

#include <iostream>
#include <iomanip>
using namespace std;

//Declare the array size and declare the array
const int ARRAY_SIZE = 101;
double myList[ARRAY_SIZE];
int main ()
{
	//Display A message Describing the Program
	cout << "This program reads numbers from 0 to 100." << endl;
	cout << "It will tell the number of occurences of each number in the list." << endl; 
	cout << "When prompted, enter any amount of numbers from 0 to 100, in any order." << endl;
	cout << "Numbers can be repeated.  Once zero is entered the program stops reading and counts the numbers." << endl;
	cout << endl;
	//Allocate memory for input numbers.
	int number; //input numbers.
	//Prompt the user to enter the integers needed for the program to run.
	cout << "Enter the integers between 0 and 100: " << endl;
	cin >> number;
	//Initialize the Array.
	for (int i = 0; i < ARRAY_SIZE; i++)
		myList[i] = 0;
	//Mark each number to its correspoding element.
	int placeholder = 0;
	while (number != 0)
	{
		myList[placeholder] = number;
		placeholder++;
		cin >> number;
	}
	//Prints the Array.
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		cout << myList[i] << " ";
	}
	return 0;
}

This is one solution:

for (int i = 0; i < ARRAY_SIZE; i++)
	{
		int n = 0;
		for(int j = 0; j < ARRAY_SIZE; j++){
			if(myList[j] == myList[i])
				n++;
		}
		
		if( myList[i] != 0 )
			cout << myList[i] << " appears"
				 << n << "times." << endl;
	}

Thanks. This helped a lot. The only thing that I need to change is that the output repeats itself. For example, if I put the numbers 2, 3, 5, and 2 into the input, the output will read: 2 appears 2 times. 3 appears 1 times. (and I want to find a way to make time singular in that sentence too.) 5 appears 1 times. and then it will repeat 2 appears 2 times. I do want to mention though...You are a genius. Thanks again.

Hi
Shall only whole numbers be counted as: myList[placeholder] = number; (if so, then change 0 into +=). Or shall each digit of an inputed number be counted, e.g. input 33 here 3 counts twice.

>> Andreas5: deleted, i got it, sorry
-- tesu

you mean change placeholder = 0 into placeholder += 0?

no, I meant the "=" in your statement: myList[placeholder] = number;

** deleted ** (i miss-understood you)

-- tesu

I made another one because it was fun :P

//Prints the Array.

	int alreadyPrinted[ARRAY_SIZE];
	int printed = 0;
	bool already = true;

	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		int n = 0;
		for(int j = 0; j < ARRAY_SIZE; j++){
			if(myList[j] == myList[i])
				n++;
		}

		already = true;
		for(int j = 0; j != printed; ++j)
			if(myList[i] == alreadyPrinted[j])
						already = false;


		if( myList[i] != 0 && already ) {
			cout << myList[i] << " appears " << n;
			if(n == 1) cout << " time." << endl;
				 else cout << " times." << endl;
			alreadyPrinted[printed++] = myList[i];
		}
	}

I just modified your while loop to perform two purpose :
1) To take input from user.
2) At the same time calculating frequency of occurrence;

//Mark each number to its correspoding element.
	int placeholder = 0;
	while (number != 0)
	{
		//myList[placeholder] = number;
		//placeholder++;
                 myList[number]++;
		cin >> number;
		
	}

Now your myList contain all the frequency corresponding to its index. Hope this will solve your problem:)

Thank you all. I was able to make the code work and I also learned several new ways to solve this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.