Number of occurances

This is a discussion on Number of occurances within the C++ Programming forums, part of the General Programming Boards category; I am working on a program for class and could use a bit of guidance. The program is supposed to ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Number of occurances

    I am working on a program for class and could use a bit of guidance. The program is supposed to read in a number, declare and array of that size, have the user fill the array with numbers from 0-100. I have that part down just fine. Next it needs to order the numbers and display the number of times each number appeared. Now you can see I tried to do this by incrementing a[i]++ but i am not sure that will work, or how i would go about outputting that. any advice?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
            int sizeOfList=0;
            int a[101];
            int i = 0;
            int j = 0;
    
            cout << "Enter an integer: " << endl;
            cin >> sizeOfList;
            cout << "Enter an integer in the range 0-100:" << endl;
            while (j < sizeOfList) {
                    cin >> i;
                    a[i]++;
                    j++;
            }
    
            if (a[i] != 0){
                    cout << i << endl;
            }
    }

  2. #2
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    I think the first step would be to read something on dynamic arrays, in the current program if the size of list is greater then 101 then you will get a memory overflow.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    thanks for pointing that out. let me explain my thinking and maybe i can be pointed in the right direction. Since the user is allowed to input numbers between 0-100. I figured i could use the array to keep track of the occurrences. would this work? also i believe i would need to set all a[i] to 0 for all numbers first. If this is needed, how would i go about doing that?

  4. #4
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    I think you not quite understanding something here...

    Code:
    int a[101]
    this code sets HOW MANY numbers are avaliable, not the MAX VALUE of the number being input,

    This Code here...
    Code:
    while (j < sizeOfList) {
                    cin >> i;
                    a[i]++;
                    j++;
            }
    dosn't actually input any numbers into the array; it just selects the array and goes to the next one without doing anything, you would want something more like
    Code:
     while (j < sizeOfList) {
                    cin >> a[j];
                    j++;
            }
    or even better...
    Code:
    for(j =0; j < sizeOfList; j++)
    {
    cin >> a[j];
    }
    notice how i is redendent in both cases.

    and i'd recomend checking your output at the end of the program so you can see whats going on, add something like

    Code:
           for(j=0;j < sizeOfList; j++)
            {
            cout<< a[j] << std::endl ;
            }
    while(1){}
    just to debug and make sure whats going in is whats going out.

    in this case you dont need to preset anything to zero.
    Hopefully that will help.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    i understand that a[i] makes an array of size a. I think i am just being unclear of my approach. I was going to use the array a[i] to keep track of the occurrences, not store the input. ie, if the input is 50, 10, 30, 50... a[50]=2 a[10]=1 a[30]=1. Is this not a reasonable way to do that?

    thanks for you time matty

  6. #6
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    lol oops I miss read your first post and went running off in the wrong direction,
    so if iv'e got it right this time you want to find out how many times the value x occured in an array a[y]?

    sorta like this?
    Code:
    cin >> value;
    
    for(j = 0; j < sizeOfList; j++)
    {
    
    if(a[j] == value)
    {i ++;}
    
    }
    cout << "The value " << value << " occured " << i << " times."

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    haha i am failing at explaining this i guess.. i'll take it slowly. there is two inputs, the first is a int which declares the number of second inputs, the second input is a list of numbers that fall in the range 0-100
    ie. input 1 = 5... input 2 = 5,6,25,34,5
    the ouput is two columns, the first is the numbers that were entered in the second input and the second is the numbers of times each number appeared.
    ie
    5 2
    6 1
    25 1
    34 1

  8. #8
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    ok so basicly this is just slightly modifyed from the other code, hopefully iv'e got it this time lol, but this is how I would go about it...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
            int sizeOfList=0;
            int a[101];
            int i = 0;
            int j = 0;
            int count;
            int x;
                    
            cout << "Enter an integer: " << endl;
            cin >> sizeOfList;
            cout << "Enter an integer in the range 0-100:" << endl;
    
    
    for(x =0; x < sizeOfList; x++)
    {
    count = 0;
    cin >> i;
    
    for(j = 0; j < 100; j++)
    {
    if(a[j] == i)
    {count ++;}
    }
    
    cout << i << " ";
    cout << count << std::endl;
    
    }
    }

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    once again, thanks matty. There is one more aspect I guess i should have made clear. I need to be able to have the user input all the numbers at once(as in uninterupted) and have it perform all the ouput at once at the end. Also, the calculations are wrong.. but i think i can figure that part out with a bit of time...

    ie this codes ran
    Code:
    Enter an integer: 
    5
    Enter an integer in the range 0-100:
    5
    5was input 0times
    4
    4was input 1times
    3
    3was input 0times
    2
    2was input 0times
    1
    1was input 8times
    I need it to looks like this when run
    Code:
    Enter an integer: 
    5
    Enter an integer in the range 0-100:
    5
    4
    3
    2
    1
    5was input 0times
    4was input 1times
    3was input 0times
    2was input 0times
    1was input 8times

  10. #10
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    to do that i'd change i into and array and have a for loop with the cin part abouve the other loops

    like...
    Code:
    for(x = 0; x < sizeOfList;x++)
    {
    cin >> i[x];
    }
    ofcourse you would have to set i as a dynamic array after the first inputing section of the program,

    then chage 'i' into 'i[x]' on the rest of the program

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    thanks, i think i can take it from here. you have been of great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 09:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 10:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

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