By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

How find a number ina list

P: n/a
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

Nov 2 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Nov 2 '06 #2

P: n/a
"nick048" <ni*************@moonsoft.itwrote in message
news:11*********************@h54g2000cwb.googlegro 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()
Nov 2 '06 #3

P: n/a
"nick048" <ni*************@moonsoft.itwrote in message
news:11*********************@h54g2000cwb.googlegro 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;
}
Nov 2 '06 #4

P: n/a
"nick048" <ni*************@moonsoft.itwrote:
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.
Nov 2 '06 #5

P: n/a

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

Nov 2 '06 #6

P: n/a
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
Nov 2 '06 #7

P: n/a

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

Nov 2 '06 #8

P: n/a
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
Nov 2 '06 #9

P: n/a
On 2 Nov 2006 07:09:06 -0800 in comp.lang.c++, "nick048"
<ni*************@moonsoft.itwrote,
>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]++;

Nov 2 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.