Show list

This is a discussion on Show list within the C++ Programming forums, part of the General Programming Boards category; My program should allow the user to enter up to 100 Zip Codes and keep a count of how many ...

  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
    22,269
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:05 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21