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

Edited 6 Years Ago by tesuji: n/a

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

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

-- tesu

Edited 6 Years Ago by tesuji: n/a

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];
		}
	}

Edited 6 Years Ago by Andreas5: n/a

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.

This question has already been answered. Start a new discussion instead.