User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,711 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,400 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2452 | Replies: 6 | Solved
Reply
Join Date: Feb 2006
Posts: 4
Reputation: xjim72 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xjim72 xjim72 is offline Offline
Newbie Poster

Number of times a number appears in an array

  #1  
Feb 7th, 2006
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 4
Reputation: xjim72 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xjim72 xjim72 is offline Offline
Newbie Poster

Re: Number of times a number appears in an array

  #2  
Feb 7th, 2006
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Number of times a number appears in an array

  #3  
Feb 7th, 2006
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.
Reply With Quote  
Join Date: Feb 2006
Posts: 4
Reputation: xjim72 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xjim72 xjim72 is offline Offline
Newbie Poster

Re: Number of times a number appears in an array

  #4  
Feb 9th, 2006
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, 7 views)
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Number of times a number appears in an array

  #5  
Feb 9th, 2006
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;
			
	}
}
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 234
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Number of times a number appears in an array

  #6  
Feb 9th, 2006
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;
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
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Join Date: Feb 2006
Posts: 4
Reputation: xjim72 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xjim72 xjim72 is offline Offline
Newbie Poster

Re: Number of times a number appears in an array

  #7  
Feb 10th, 2006
Thanks guys. Your ideas worked.



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:\>count_numbers <numbers.txt
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC