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

Dani AI

Generated

A few focused points to make the program robust and match the spec in the thread.

First, decide whether 0 is a valid value or only a sentinel. The original prompt says "reads numbers from 0 to 100" but also uses 0 to stop input. That is a conflict: using 0 as sentinel means you will never count zero. If zero must be counted, use a different sentinel (for example -1) or stop on EOF.

Use integer storage for these values (not double) and validate each read is in range (0..100) before using it. That avoids floating‑point comparison issues and prevents out‑of‑bounds indexing.

Two reliable approaches:

  • Fixed-range frequency array: keep an int counts[101] (initialized to zero) and increment counts[value] as each valid input is read. This is O(n) time and constant memory for the 0–100 domain, and it avoids the duplicate-print problem entirely.
  • Order-preserving map: if you need to support arbitrary integers or print results in the order numbers first appeared, use a std::map<int,int> (or std::unordered_map) for counts plus a std::vector<int> to record the first-seen order. Increment the map entry on each input; push to the vector only when the count was previously zero.

Example (order-preserving sketch):

std::map<int,int> counts;
std::vector<int> order;
while (cin >> n && n != sentinel) {
  if (n < 0 || n > 100) continue;         // or report error
  if (counts[n] == 0) order.push_back(n);
  ++counts[n];
}
for (int v : order) {
  cout << v << " appears " << counts[v]
       << (counts[v] == 1 ? " time." : " times.") << '\n';
}

Practical checks and troubleshooting:

  • Protect against exceeding any fixed-size buffer when collecting raw inputs.
  • Check the return value of cin >> number to handle EOF or bad input.
  • Enforce input range to avoid corrupting the counts array.
  • Use the counts[x] == 1 ? "time" : "times" pattern to fix singular/plural output (as handled in his follow-up).
  • ’s suggestion to increment counts on input is the most efficient approach; combine that with the order-tracking idea above if you care about original input order.

Those changes will remove duplicate printed lines, handle grammar correctly, and make the program safe for out-of-range or malformed input.

Recommended Answers

All 8 Replies

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.