Count number!

This is a discussion on Count number! within the C++ Programming forums, part of the General Programming Boards category; Read a list of ints into a array.The output is two-column list,first column is a list of the different array ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    67

    Count number!

    Read a list of ints into a array.The output is two-column list,first column is a list of the different
    array elements ;second column is the count of that element occurs.We can assume that there are few than 50 ints.

    Ok,the only problem for me is how to count the ints.Compare to letters,I can set up a array called letters[26],then contain from a~z,and use get function to get the char and compare:

    like

    Code:
    while(cin.get(next))
    {
          for(int i=0;i<26;i++)
         {
               if(next=letters[i])
               count[i]++;
          }
    }
    so you can see in my count[26],I have the occurences.

    but how to deal with an arbitrary int?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    1. Read your numbers into the array.
    2. Sort the array.
    3. Start at the first element.
    4. Call the count STL function on that array for the current element we are looking at. Print the element and the count returned.
    5. Advance through the array the same number of positions as step 4 returned
    5A. If not at the end of the array return to step 4.
    5B. If we are at the end, then exit.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Do you have to use an array? It becomes pretty trivial with a std::map. In fact, it would be what you described with letters, except that it is still efficient when dealing with arbitrary ints.
    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

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    Yeah, I naturally thought that too, but he said "Read a list of ints into a array" so I worked with what I got.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prompting user for data
    By radical in forum C Programming
    Replies: 5
    Last Post: 07-28-2005, 11:45 AM
  2. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  3. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  4. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM
  5. Count the number of letter "a" on a sentence
    By imbecile in C in forum C Programming
    Replies: 6
    Last Post: 07-27-2003, 02:32 PM

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