944,050 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4925
  • C++ RSS
Feb 7th, 2006
0

Number of times a number appears in an array

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xjim72 is offline Offline
4 posts
since Feb 2006
Feb 7th, 2006
0

Re: Number of times a number appears in an array

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xjim72 is offline Offline
4 posts
since Feb 2006
Feb 7th, 2006
0

Re: Number of times a number appears in an array

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Feb 9th, 2006
0

Re: Number of times a number appears in an array

Quote originally posted by Ancient Dragon ...
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[i] == 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;

}
}
Attached Files
File Type: cpp Ex11_Cabrera.cpp (1.8 KB, 35 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xjim72 is offline Offline
4 posts
since Feb 2006
Feb 9th, 2006
0

Re: Number of times a number appears in an array

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;
			
	}
}
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Feb 9th, 2006
0

Re: Number of times a number appears in an array

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);
             listSize--;
	cout <<endl;
	cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;
Quote ...
I get repeat info
Show the output.
Quote ...
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++ Syntax (Toggle Plain Text)
  1. C:\>count_numbers <numbers.txt
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Feb 10th, 2006
0

Re: Number of times a number appears in an array

Thanks guys. Your ideas worked.



Quote originally posted by dwks ...
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);
             listSize--;
	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++ Syntax (Toggle Plain Text)
  1. C:\>count_numbers <numbers.txt
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xjim72 is offline Offline
4 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: [urgent]Error
Next Thread in C++ Forum Timeline: making labels with c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC