Thread: Show list

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Lightbulb 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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Now what's your question?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    6

    print table of entries

    how do make the program sow how many times a zip was entered and what percent that number is of the total entries?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Thoggle
    how do make the program sow how many times a zip was entered and what percent that number is of the total entries?
    Basically, you need to map the zip codes to counts. A simple way is to use a std::map<int, int>, but if you are not allowed to do so, then one approach is to use parallel arrays: you have another array of counts, for which zips[i] corresponds to counts[i]. You then search zips, and if the current zip code is not in it, you append it and set the count to 1.

    Incidentally, you should use std::vector<int> instead of manually managing memory, and then there seems to be little reason to use nothrow new when you can catch std::bad_alloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM