473,412 Members | 3,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

How find a number ina list

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
9 1565
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
"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
"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
"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

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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: msnews | last post by:
Hi All, We have a requirement to display total number of current users using the site. We want to display only the total no other information. And this value needs to change dynamically. Is it...
8
by: jquest | last post by:
Hi Again; I have had help from this group before and want to thank everyone, especially PCDatasheet. My database includes a field called HomePhone, it uses the (xxx)xxx-xxx format to include...
10
by: OppThumb | last post by:
Hi, I've been searching this newsgroup for an answer to my question, and the closest I've come asks my question, but in reverse ("How to figure out the program from plan/package"). I've -- shall...
0
by: nimjerry | last post by:
i am using db2 udb V 9 on aix 5.3 and in db2diag.log alwas has this error occurr below is sample message 2008-03-03-09.45.34.366406+420 I306667A443 LEVEL: Warning PID : 835622 ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.