Number of times number repeats

Discussion in 'C++' started by viuxrluxvbbc@mailinator.com, Dec 6, 2005.

  1. Guest

    Hi im trying to write a program that will read in numbers and display
    them in ascending order along with a count of how many times it
    repeats. i got the numerical order portion done but cant figure out the
    other part. i keep on getting a wrong number
    please help
    #include <iostream>
    using namespace std;

    int main()
    {
    int numbers[4] = {8,3,8,5};
    int temp, counter, index, see, times, appears;
    for (counter = 0; counter < 4; counter++)
    {
    for (index = 0; index < 4 - counter; index++)
    {
    if (numbers[index] > numbers[index + 1])
    {
    temp = numbers[index];
    numbers[index] = numbers[index+1];
    numbers[index+1] = temp;
    }
    }
    }

    for (int times = 0; times < 4 ; times++)
    {
    if (numbers[times] !=8)
    appears++;
    else
    appears = 0;

    }
    cout << appears; //wrong

    for (see = 0; see < 4; see++)
    {
    cout << numbers[see] << " ";
    }
     
    , Dec 6, 2005
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hi im trying to write a program that will read in numbers and display
    > them in ascending order along with a count of how many times it
    > repeats. i got the numerical order portion done but cant figure out the
    > other part. i keep on getting a wrong number
    > please help
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int numbers[4] = {8,3,8,5};
    > int temp, counter, index, see, times, appears;
    > for (counter = 0; counter < 4; counter++)
    > {
    > for (index = 0; index < 4 - counter; index++)
    > {
    > if (numbers[index] > numbers[index + 1])
    > {
    > temp = numbers[index];
    > numbers[index] = numbers[index+1];
    > numbers[index+1] = temp;
    > }
    > }
    > }
    >
    > for (int times = 0; times < 4 ; times++)
    > {
    > if (numbers[times] !=8)
    > appears++;
    > else
    > appears = 0;
    >
    > }
    > cout << appears; //wrong
    >
    > for (see = 0; see < 4; see++)
    > {
    > cout << numbers[see] << " ";
    > }


    You could use a std::map<int,unsigned int>. It would associate a number
    (the key) with its count (the value). Consult your C++ text for more
    info, try it out, and ask for help if you get stuck.

    Cheers! --M
     
    mlimber, Dec 6, 2005
    #2
    1. Advertising

  3. BobR Guest

    wrote in message
    <>...
    >Hi im trying to write a program that will read in numbers and display
    >them in ascending order along with a count of how many times it
    >repeats. i got the numerical order portion done but cant figure out the
    >other part. i keep on getting a wrong number
    >please help
    >#include <iostream>
    >using namespace std;
    >
    >int main(){
    > int numbers[4] = {8,3,8,5};
    > int temp, counter, index, see, times, appears;
    > for(counter = 0; counter < 4; counter++){
    > for(index = 0; index < 4 - counter; index++){
    > if (numbers[index] > numbers[index + 1]){
    > temp = numbers[index];
    > numbers[index] = numbers[index+1];
    > numbers[index+1] = temp;
    > }
    > }
    > }
    > for(int times = 0; times < 4 ; times++){
    > if(numbers[times] !=8) appears++;
    > else appears = 0;
    > }
    > cout << appears; //wrong
    > for(see = 0; see < 4; see++){ cout << numbers[see] << " ";}
    >


    #include <iostream>
    #include <ostream>

    // int viuxrluxvbbc_main(std::eek:stream &cout){ // tested with
    int main(){
    using std::cout;
    cout <<"___ viuxrluxvbbc_main() ___"<<std::endl;
    int numbers[4] = {8,3,8,5};
    int temp, appears;
    for(int counter = 0; counter < 4; ++counter){
    for(int index = 0; index < 4 - counter; ++index){
    if(numbers[index] > numbers[index + 1]){ // swap
    temp = numbers[index];
    numbers[index] = numbers[index+1];
    numbers[index+1] = temp;
    }
    } //for(index)
    } //for(counter)
    // -- PROBLEM -- [ note the comments! ]
    for(int times = 0; times < 4 ; ++times){
    if( numbers[times] != 8 ) ++appears; // inc if not equal to 8
    else appears = 0; // otherwise, set 'appears' to zero.
    } //for(times)
    cout <<"appears="<< appears<<std::endl; //wrong
    // -- PROBLEM end --

    // -- PROBLEM fix --
    appears = 0;
    for(int times = 0; times < 4 ; ++times){
    if( numbers[times] == 8 ) ++appears; // inc if equal to 8
    } //for(times)
    cout <<"appears="<< appears<<std::endl; //wrong
    // -- PROBLEM fix end --

    for(int see = 0; see < 4; ++see){ cout << numbers[see] << " ";} //for(see)
    cout <<"___ viuxrluxvbbc_main() end ___"<<std::endl;
    return 0; // always return 0, EXIT_SUCCESS or EXIT_FAILURE.
    } //main() end
    // ---------------------
    // - output -
    ___ viuxrluxvbbc_main() ___
    appears=0
    appears=2
    3 5 8 8
    ___ viuxrluxvbbc_main() end ___


    --
    Bob R
    POVrookie
     
    BobR, Dec 6, 2005
    #3
  4. Guest

    well i got it to work. i need some help with the output tho.
    say if a number repeats, how can i get it to display only once but
    still display a correct repetition count
    right now its outputting something like this:

    Number Count
    8 3
    8 3
    8 3
    1 1

    but i wanna fix so it displays this:
    Number Count
    8 3
    1 1

    heres the code

    #include <iostream>
    using namespace std;

    const int size = 7;

    int main()
    {
    int numbers[size] = {1,6,8,5,4,5,6};
    int temp, counter, index, numorder, times, appears = 0, me;
    cout << "This program will read in numbers and display them in
    ascending order." << endl;
    cout << "It will also count the number of times a number is
    repeated." << endl;
    cout << endl;
    for (counter = 0; counter < 7; counter++)
    {
    for (index = 0; index < size - counter; index++)
    {
    if (numbers[index] > numbers[index + 1])
    {
    temp = numbers[index];
    numbers[index] = numbers[index+1];
    numbers[index+1] = temp;
    }
    }
    }

    for (numorder = 0; numorder < size; numorder++)
    {
    cout << numbers[numorder] << " ";
    }
    cout << endl;
    cout << "\nNumber Count" << endl;

    for (int times = 0; times < size ; times++)
    {
    appears = 0;
    for (int me = size - 1 ; me >= 0; me--)
    {
    if (numbers[times] == numbers [me] )
    {
    appears++;
    }

    }
    cout << numbers[times] << " " <<
    appears << endl;
    }

    cin.get();cin.get();
    return 0;
    }
     
    , Dec 7, 2005
    #4
  5. Neil Cerutti Guest

    On 2005-12-07, <> wrote:
    > well i got it to work. i need some help with the output tho.
    > say if a number repeats, how can i get it to display only once
    > but still display a correct repetition count right now its
    > outputting something like this:
    >
    > Number Count
    > 8 3
    > 8 3
    > 8 3
    > 1 1
    >
    > but i wanna fix so it displays this:
    > Number Count
    > 8 3
    > 1 1
    >
    > heres the code
    >
    > #include <iostream>
    > using namespace std;
    >
    > const int size = 7;
    >
    > int main()
    > {
    > int numbers[size] = {1,6,8,5,4,5,6};
    > int temp, counter, index, numorder, times, appears = 0, me;
    > cout << "This program will read in numbers and display them in
    > ascending order." << endl;
    > cout << "It will also count the number of times a number is
    > repeated." << endl;
    > cout << endl;
    > for (counter = 0; counter < 7; counter++)
    > {
    > for (index = 0; index < size - counter; index++)
    > {
    > if (numbers[index] > numbers[index + 1])
    > {
    > temp = numbers[index];
    > numbers[index] = numbers[index+1];
    > numbers[index+1] = temp;
    > }
    > }
    > }
    >
    > for (numorder = 0; numorder < size; numorder++)
    > {
    > cout << numbers[numorder] << " ";
    > }
    > cout << endl;
    > cout << "\nNumber Count" << endl;
    >
    > for (int times = 0; times < size ; times++)
    > {
    > appears = 0;
    > for (int me = size - 1 ; me >= 0; me--)
    > {
    > if (numbers[times] == numbers [me] )
    > {
    > appears++;
    > }
    >
    > }
    > cout << numbers[times] << " " <<
    > appears << endl;
    > }


    You are counting the total appearances every time a number
    appears. You only need to count once for each number. It will
    save time, and produce the index you need, to count from
    position [times] instead of from the back of the array.

    For example, assuming my list is 1, 1, 4, 5, 5:

    I see a one, so I count from there to the end of the ones. I
    print the output. 1, 2. I then start looking past the end of the
    ones, at the four. And so on.

    In psuedocode:

    set i to 0.
    loop1: if i is not less than size, break from loop1.
    set j to i.
    loop2: if j is not less than size, or
    numbers <> number[j], break from loop2.
    increment j by one.
    Loop.
    print numbers, (j-i+1).
    set i to j+1;

    It translates into two or three lines of C++ code.

    For fun, it can be solved with std::equal_range.

    std::pair<const int*, const int*> p(0, numbers);
    while (p.second != numbers+size) {
    p = std::equal_range(p.second, numbers+size, *p.second);
    std::cout << *p.first << ": " << p.second-p.first << '\n';
    }

    --
    Neil Cerutti
     
    Neil Cerutti, Dec 7, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kj12345

    Refresh repeats last action

    kj12345, Sep 28, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    504
    Eliyahu Goldin
    Sep 29, 2004
  2. Mick
    Replies:
    1
    Views:
    710
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 29, 2004
  3. Amelyan
    Replies:
    2
    Views:
    4,009
    clintonG
    Dec 19, 2005
  4. Thiago Jorge

    Tab repeats function 6 times

    Thiago Jorge, Oct 12, 2006, in forum: Javascript
    Replies:
    1
    Views:
    99
  5. cate
    Replies:
    1
    Views:
    208
    Evertjan.
    Jun 14, 2010
Loading...

Share This Page