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++ will facilitate #2 above, but I'm not familiar enough with it to demonstrate its use.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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;
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343