Thread: Count number!

  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,817
    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
    28,413
    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.
    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

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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