473,785 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1578
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.itwr ote in message
news:11******** *************@h 54g2000cwb.goog legroups.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.itwr ote in message
news:11******** *************@h 54g2000cwb.goog legroups.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.itwr ote:
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(uns igned *const *ppcounters,
int const val,
int const *const *ppcols,
unsigned const quant_cols,
unsigned const num_per_col)
{
assert(ppcounte rs);
assert(ppcols);
assert(quant_co ls);
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,1 3,17,19,50,60,7 0};

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

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

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

IncCounters(arr _pcounters,2,ar r_pcols,3,12);
}

--

Frederick Gotham
Nov 2 '06 #9
On 2 Nov 2006 07:09:06 -0800 in comp.lang.c++, "nick048"
<ni************ *@moonsoft.itwr ote,
>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
2411
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 possible to do this? Please let me know how to do it. Thanks.
8
5499
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 area code. When a customer calls, I currently use Ctrl F with the HomePhone field highlighted. Then I enter the last 4 digits and use the find next option. This is cumbersome, so I have tried several methods (including a macro) using comand...
10
13650
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 we say, inherited? -- a COBOL program with very little documentation that I've recompiled for debugging purposes. The compile/link/bind have all been done, but nothing in the output tells me what plan the program has been bound to, so I can't...
0
5088
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 TID : 1 PROC : db2stmm (WEBEDIDB) INSTANCE: db2inst1 NODE : 000 DB : WEBEDIDB APPHDL : 0-8 APPID: *LOCAL.db2inst1.080229022428 AUTHID : DB2INST1 FUNCTION: DB2 UDB, Self tuning memory...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.