Thread: Occurence counter

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    Red face Occurence counter

    Hi,
    I am a new user of C++ and I am learning this language.
    I want to resolve this example:
    1) I have an indefinite sequence of integer (from 0 to 36) as Input;
    2) I want to know the maximum number of occurrences and its value.
    Sample
    Input: 0, 2, 5, 2, 23, 11, 11, 23, 15, 17, 25, 11, 23, 18, 23 (Ctrl+z)
    Output: Value is 23 Occurences is 4

    For the input I have written this code:
    Code:
     iNumber = 0; // Counter
          while (1)
             { //read next
                 cout << "Enter number (Ctrl-z = Input End): ";
                cin >> myNumber;
                if (cin.eof())
                   break;
                //update the counter
                iNumber++;
             }
    But I am in difficulty, when I try to resolve the second step. The
    solution don't have to use the array/map/struct.

    I hope in Your help in order to resolve my problem.

    Thank You and Best Regards
    Nino

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Err ... I really don't think this is possible to solve without either a simple array or lots of variables and a very ugly switch to simulate it. Oh, and the latter solution would have a quite horrible tree of ifelses to find the maximum.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Maybe the exact citate from the assignment would help? I suppose that only class implementations of vector, map etc are forbidden, but use of regular C-style arrays is possible...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I tested a solution that used the sort and equal_range and distance STL functions along with a regular integer array. Can you use those functions?
    "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

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Yes, I can use this.

    Thank You.
    Nino

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  3. Flowchart Question
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 04-08-2007, 11:33 AM
  4. Counter Heap Sort
    By Achillles in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 12:17 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 01:58 PM