How find a number ina list

Discussion in 'C++' started by nick048, Nov 2, 2006.

  1. nick048

    nick048 Guest

    Hi to all,
    If in input I enter an int, I need to create a function that verify if
    my int is in a list of this value:
    1 4 7 10 13 16 19 22 25 28 31 34.
    How can resolve this question.
    I hope in Your help.
    Best Regards
    Gaetano
     
    nick048, Nov 2, 2006
    #1
    1. Advertising

  2. nick048

    benben Guest

    nick048 wrote:
    > Hi to all,
    > If in input I enter an int, I need to create a function that verify if
    > my int is in a list of this value:
    > 1 4 7 10 13 16 19 22 25 28 31 34.
    > How can resolve this question.
    > I hope in Your help.
    > Best Regards
    > Gaetano
    >


    Store the input number, then iterate over the whole list and compare
    every individual item in the list with the number input. If an equality
    occurred then you know the number is in the list; otherwise if you
    iterated through the whole list and no equality occurred, then the
    number is not in the list.

    Ben
     
    benben, Nov 2, 2006
    #2
    1. Advertising

  3. nick048

    Jim Langston Guest

    "nick048" <> wrote in message
    news:...
    > Hi to all,
    > If in input I enter an int, I need to create a function that verify if
    > my int is in a list of this value:
    > 1 4 7 10 13 16 19 22 25 28 31 34.
    > How can resolve this question.
    > I hope in Your help.
    > Best Regards
    > Gaetano


    Simple way, put in some list, iterate through the list and see if it exists.
    Other ways, put the numbers in some keyed container (map, I think set) and
    do .find()
     
    Jim Langston, Nov 2, 2006
    #3
  4. nick048

    filox Guest

    "nick048" <> wrote in message
    news:...
    > Hi to all,
    > If in input I enter an int, I need to create a function that verify if
    > my int is in a list of this value:
    > 1 4 7 10 13 16 19 22 25 28 31 34.
    > How can resolve this question.
    > I hope in Your help.
    > Best Regards
    > Gaetano


    or just do this
    bool f(int a)
    {
    if (a % 3 == 1)
    return true;
    else return false;
    }
     
    filox, Nov 2, 2006
    #4
  5. nick048

    Daniel T. Guest

    "nick048" <> wrote:

    > Hi to all,
    > If in input I enter an int, I need to create a function that verify if
    > my int is in a list of this value:
    > 1 4 7 10 13 16 19 22 25 28 31 34.
    > How can resolve this question.
    > I hope in Your help.


    Try writing the program to work with a single number (rather than a
    whole list of numbers) first. Then ask yourself, how to change it so you
    can check against two numbers, then three. Once you get there, look for
    duplication in your code and see how an array and a loop can be used to
    remove that duplication.

    If at any point you are stuck, post the code you have written and ask
    for help.

    --
    To send me email, put "sheltie" in the subject.
     
    Daniel T., Nov 2, 2006
    #5
  6. nick048

    peter koch Guest

    nick048 wrote:
    > Hi to all,
    > If in input I enter an int, I need to create a function that verify if
    > my int is in a list of this value:
    > 1 4 7 10 13 16 19 22 25 28 31 34.
    > How can resolve this question.
    > I hope in Your help.
    > Best Regards
    > Gaetano


    We do not make your homework for you, and your question is not a C++
    question. So you better work on a solution and present it to us: then
    we'll be happy to help you if you get stuck.

    /Peter
     
    peter koch, Nov 2, 2006
    #6
  7. nick048:

    > If in input I enter an int, I need to create a function that verify if
    > my int is in a list of this value:
    > 1 4 7 10 13 16 19 22 25 28 31 34.


    bool IsPresent(int const i)
    {
    return i>=1 && i<=34 && 1==i%3;
    }

    Make an effort next time.

    --

    Frederick Gotham
     
    Frederick Gotham, Nov 2, 2006
    #7
  8. nick048

    nick048 Guest

    Ok, Ok.
    Sorry! Be patient with me. I am a newby in C++.
    This is my Function:
    void initColumn(int n) {
    int counter;
    int firstColumn[12] = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34};
    int secondColumn[12] = {2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35};
    int thirdColumn[12] = {3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36};
    for (counter=0; counter < 12; counter++) {
    if (firstColumn[counter] == n) {
    columnArray[0]++;
    break;
    }
    if (secondColumn[counter] == n) {
    columnArray[1]++;
    break;
    }
    if (thirdColumn[counter] == n) {
    columnArray[2]++;
    }
    }
    }

    first/second/thirdColumn are my list (arithmetic progression) of 12
    elements (step 3).
    I need to find if a number is in first/second/thirdColumn.
    If yes, add 1 to the relative counter in columArray and exit.

    My question is:
    it is possible to write better this function (see i.e. the 3 IF)?

    Best Regards
    Gaetano
     
    nick048, Nov 2, 2006
    #8
  9. nick048:

    > My question is:
    > it is possible to write better this function (see i.e. the 3 IF)?


    It really depends how much freedom you want with the algorithm. Something
    like the following would give you a lot of freedom:

    #include <cassert>

    void IncCounters(unsigned *const *ppcounters,
    int const val,
    int const *const *ppcols,
    unsigned const quant_cols,
    unsigned const num_per_col)
    {
    assert(ppcounters);
    assert(ppcols);
    assert(quant_cols);
    assert(num_per_col);

    int const *const *const ppcols_over = ppcols + quant_cols;

    do
    {
    unsigned *const pcounter = *ppcounters++; assert(pcounter);

    int const *pelem = *ppcols++; assert(pelem);

    int const *const pelem_over = pelem + num_per_col;

    do if (val == *pelem++) ++*pcounter;
    while (pelem_over != pelem);
    } while (ppcols_over != ppcols);
    }

    int main()
    {
    int const column1[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
    int const column2[12] = {1,2,4,8,16,32,65,128,256,512,1024,2048};
    int const column3[12] = {1,2,3,5,7,11,13,17,19,50,60,70};

    int const *const arr_pcols[3] = {column1,column2,column3};

    unsigned counter1=0, counter2=0, counter3=0;

    unsigned *const arr_pcounters[3] = {&counter1,&counter2,&counter3};

    IncCounters(arr_pcounters,2,arr_pcols,3,12);
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Nov 2, 2006
    #9
  10. nick048

    David Harmon Guest

    On 2 Nov 2006 07:09:06 -0800 in comp.lang.c++, "nick048"
    <> wrote,
    >first/second/thirdColumn are my list (arithmetic progression) of 12
    >elements (step 3).
    >I need to find if a number is in first/second/thirdColumn.
    >If yes, add 1 to the relative counter in columArray and exit.


    If your formula is going to be regular as "step 3" then you should use
    one of the "n % 3" type of solutions that others have suggested, using
    the C++ % remainder operator. The remainder when dividing by three is
    directly the index to the correct counter!
    columnArray[n % 3]++;
     
    David Harmon, Nov 2, 2006
    #10
    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. John Antypas

    Using JavaMail API ina proxy server?

    John Antypas, Aug 24, 2003, in forum: Java
    Replies:
    1
    Views:
    4,081
    GaryM
    Aug 26, 2003
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Replies:
    1
    Views:
    500
    Lee Fesperman
    Nov 21, 2003
  3. Marc H.
    Replies:
    2
    Views:
    339
    Martin Franklin
    Mar 13, 2005
  4. Vasu
    Replies:
    0
    Views:
    480
  5. apondu
    Replies:
    1
    Views:
    386
    Patrice
    Apr 3, 2007
Loading...

Share This Page