I am having trouble with an exercise of finding how many times a number appears in an array and display the value and the count of times it appears in the list. The array can be the size of 100.

Dani AI

Generated

Quick diagnosis (for ): two bugs explain the repeated output and the -999 showing up. The input loop in the original code stores the sentinel -999 in the array, so it becomes part of the data set. Also the inner counting loop compares the array name Numbers to temp instead of the element (e.g., Numbers[i]), which prevents correct element comparisons. Finally, scanning the whole array for every element will print the same number multiple times unless you either (a) aggregate counts as you go or (b) do one linear pass over a sorted list to report each run once.

A compact, robust alternative is to use the STL — std::map (keeps keys sorted) or std::unordered_map (faster if order is not required) to accumulate frequencies as values are read. This avoids storing the sentinel, handles duplicates naturally, and produces ascending output automatically with std::map. Example:

#include <iostream>
#include <iomanip>
#include <map>

int main() {
    std::map<int,int> freq;
    int x, total = 0;
    std::cout << "Enter up to 100 positive integers, end with -999\n";
    while (total < 100 && std::cin >> x && x != -999) {
        ++freq[x];
        ++total;
    }
    std::cout << std::setw(7) << "Number" << std::setw(9) << "Count" << '\n';
    for (const auto &p : freq)
        std::cout << std::setw(7) << p.first << std::setw(9) << p.second << '\n';
    return 0;
}

Notes and troubleshooting: prefer std::vector/STL to manual fixed arrays to avoid off-by-one and magic-constant bugs; check cin for EOF/bad input; if you must keep the sort+single-pass approach, ensure the sentinel is not stored and implement the “count runs” pattern (increment a counter while values equal previous, then print once when a new value appears). For easier testing, use file redirection as suggested.

Recommended Answers

All 6 Replies

Let me be more detailed. I have the program enter a list of numbers all integers and positive. The program then list the numbers in accending order and the number of times that number is in the list. Thanks.

1. put all the numbers in an array and short it.
2. create a 2d array, the first dimension contains the number and the second dimension contains a count of the number of times the number appears. The program will have to iterate through the original array (#1 above) and maintain each array in #2.

c++ <map> will facilitate #2 above, but I'm not familiar enough with it to demonstrate its use.

Well here is what I have so far but the results are not great.
I get repeat info and it prints out -999.
Any ideas?

// Description: Program that reads in a set of positive integers
//              and out puts how many times a number appears in the list.


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


//function prototypes
void initialize (int numbers[], int listSize);
void readNum (int numbers[], int& listSize);
void selectSort (int numbers[], int listSize);


int main()
{
int Numbers [100];
int count= 0;
int listsize = 100;
int temp;



initialize (Numbers, listsize);


cout <<"Enter a maximum of 100 positive integers ending with -999"<<endl;
readNum (Numbers, listsize);
cout <<endl;
cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;


selectSort(Numbers, listsize);


for (int t=0; t< listsize; t++)
{
temp = Numbers[t];
for (int i=0; i < listsize;i++)
{
if (Numbers == temp)
count ++;
}
cout <<setw(5)<<Numbers[t]<<setw(9)<<count<<endl;
count = 0;
}
return 0;
}


void initialize (int numbers[], int listSize)
{
int index;
int Num = 0;
for (index=0; index < 100; index++)
numbers[index] = 0;
}


void readNum (int numbers[], int&listSize)
{
int index =0;
int num =0;
listSize=0;


while (num != -999)
{
cin >>num;
numbers[index] = num;
index++;
listSize++;
}
}


void selectSort (int numbers[], int listSize)
{
int index, minIndex, smallestIndex, temp;


for (index=0; index < listSize - 1;index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < listSize; minIndex++)
if (numbers[minIndex] < numbers[smallestIndex])
smallestIndex = minIndex;


temp = numbers[smallestIndex];
numbers[smallestIndex] = numbers[index];
numbers[index] = temp;


}
}

suggestion: read the 100 numbers from a file instead of entering them from the keyboard.

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

	//function prototypes
void initialize (int numbers[], int listSize);
void readNum (int numbers[], int& listSize);
void selectSort (int numbers[], int listSize);

int main()
{
	int Numbers [100];
	int count= 0;
	int listsize = 100;
	int temp;
	

	initialize (Numbers, listsize);

	cout <<"Enter a maximum of 100 positive integers ending with -999"<<endl;
	readNum (Numbers, listsize);
	cout <<endl;
	cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;

	selectSort(Numbers, listsize);

        temp = Numbers[0];
        count = 1;
	for (int t=1; t< listsize; t++)
	{
               if(Numbers[t] == temp)
                  count++;
              else
              {
                   cout << "temp = " << temp << " total = " << count;
                  temp = Numbers[t];
                   count = 1;
              }
	}
        // display count of final number
        cout << "temp = " << temp << " total = " << count;

	return 0;
}

void initialize (int numbers[], int listSize)
{
	int index;
	int Num = 0;
	for (index=0; index < 100; index++)
use listSize in above loop
		numbers[index] = 0;
}

void readNum (int numbers[], int&listSize)
{
	int index =0;
	int num =0;
	listSize=0;
You don't need variable index.  use listSize instead
	while (num != -999)
	{
		cin >>num;
		numbers[index] = num;
		index++;
		listSize++;
	}
}

void selectSort (int numbers[], int listSize)
{
	int index, minIndex, smallestIndex, temp;

	for (index=0; index < listSize - 1;index++)
	{
		smallestIndex = index;
		for (minIndex = index + 1; minIndex < listSize; minIndex++)
			if (numbers[minIndex] < numbers[smallestIndex])
				smallestIndex = minIndex;
			
			temp = numbers[smallestIndex];
			numbers[smallestIndex] = numbers[index];
			numbers[index] = temp;
			
	}
}

You have to decrement listSize if you don't want it to print -999. eg

cout <<"Enter a maximum of 100 positive integers ending with -999"<<endl;
	readNum (Numbers, listsize);
             [b]listSize--;[/b]
	cout <<endl;
	cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;

I get repeat info

Show the output.

suggestion: read the 100 numbers from a file instead of entering them from the keyboard.

Or store the numbers in a file and use your OS's redirection capabilities:

C:\>count_numbers <numbers.txt

Thanks guys. Your ideas worked.


You have to decrement listSize if you don't want it to print -999. eg

cout <<"Enter a maximum of 100 positive integers ending with -999"<<endl;
	readNum (Numbers, listsize);
             [b]listSize--;[/b]
	cout <<endl;
	cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;

Show the output.

Or store the numbers in a file and use your OS's redirection capabilities:

C:\>count_numbers <numbers.txt
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.