show list

My program should allow the user to enter up to 100 Zip Codes and keep a count of how many times each one is entered. If a zip code has already been entered it should just add to the count, if it hasn't been entered then it it should add to a list. When the user is done it show a table of all zip codes entered and how many times it was entered. It should also displa how many zips were entered and what percent each of them is of the total number entered. Here is what i have so far....
Code:
// Marketing Department Geographical Distribution Chart
#include <iostream>
#include <new>
using namespace std;

int main ()
{
int i,n;
int * Zips;
cout << "How many Zip Codes would you like to enter? ";
cin >> i;
Zips= new (nothrow) int[i];
if (Zips == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter Zip Code: ";
cin >> Zips[n];
}
cout << "You have entered: " << i << " Zip Codes." << endl << endl;
cout << "The Zip Codes you have entered are: "<< endl;
for (n=0; n<i; n++)
cout << Zips[n] << ", ";
delete[] Zips;
}
return 0;
}
Looks good so far. An alternative to using a fixed number of Zips (say the user doesn't know how many zips he has on his list, but doesn't want to count them) you could use a vector of ints, instead of a pointer.

like this:

1
2
3
4
5
6
7
8
9
#include <vector> //the include file for vectors

.....

vector<int> Zips;
int x;

while(cin >> x) //This will allow the user to input forever until an invalid input or and EOF is given
    Zips.push_back(x); //the push_back function adds the integer x to Zips.  


Also if your program only allows up to 100 zips, make sure to put in some parameter checking. In your program just check to see if i is greater than 100.
to do so with vectors you can do something like this:

1
2
3
4
5
6
7
8
9
10
while(cin >> x)
{
     if(Zips.size()+1 == 100)
     {
          cout << "100 Zips have already been entered." << endl;
          break;
     }

     Zips.push_back(x);
}
Last edited on
Where would i place in the code i already have?? And will this help me to get a total?? I am really lost.
What do you mean 'where would i place the code i already have?'
ok cool now how do display the each zip entered and it's percent?
That's where google can become your best friend :P
Search about finding the mode of an array, and work from there. As for the percent, you can just divide the total number of zips from how many times the zip occured. So you entered 50 zips, and the particular one occured 10 times, 50/10 will give you the percentage, i believe.
Last edited on
Topic archived. No new replies allowed.