Velocity Reviews > C++ > How find a number ina list

# How find a number ina list

nick048
Guest
Posts: n/a

 11-02-2006
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.
Best Regards
Gaetano

benben
Guest
Posts: n/a

 11-02-2006
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

Jim Langston
Guest
Posts: n/a

 11-02-2006
"nick048" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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()

filox
Guest
Posts: n/a

 11-02-2006
"nick048" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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;
}

Daniel T.
Guest
Posts: n/a

 11-02-2006
"nick048" <(E-Mail Removed)> 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.

peter koch
Guest
Posts: n/a

 11-02-2006

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

/Peter

Frederick Gotham
Guest
Posts: n/a

 11-02-2006
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

nick048
Guest
Posts: n/a

 11-02-2006

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

Frederick Gotham
Guest
Posts: n/a

 11-02-2006
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

David Harmon
Guest
Posts: n/a

 11-02-2006
On 2 Nov 2006 07:09:06 -0800 in comp.lang.c++, "nick048"
<(E-Mail Removed)> 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]++;