![]() |
|
#1
|
|||
|
|||
How to search an array problemHi there,
I am working on some problems and I am learning to search arrays. Here is the scenerio or problem. Given*: an int*variable* k , an int*array* currentMembers that has been declared* and initialized*, an int*variable* nMembers that contains the number of elements* in the array*, an int*variable* memberID that has been initialized*, and an int*variable* isAMember , write code that assigns* 1 to isAMember if the value* of memberID can be found in currentMembers , and that assigns* 0 to isAMember otherwise. Use only k , currentMembers , nMembers , and isAMember . here is my answer...Can anyone tell me what I am doing wrong? CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
|
One obvious mistake is in if condition. For comparison you need to use "==" (is equal to) instead of "=" which means assignment.
Also, I don't think you have correct logic there in the loop. You need to compare "memberID" with current value of currentMembers[k]. The correct program would be. CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
|
Thanks,
This makes sense but how do I make it check both ends of the array? Right now it only checks one end? Quote:
|
|
#4
|
|||
|
|||
|
I didn't get it. Could you pls elaborate a bit
. If possible with some example.Thanks, |
|
#5
|
||||
|
||||
|
Quote:
Actually, it checks every character. It's just that the if statement resets isAMember each time thru the loop so at the end of the loop isAMember is a value based only on currentMembers[nMembers-1] CPP / C++ / C Code:
CPP / C++ / C Code:
Version 2: CPP / C++ / C Code:
__________________
Age is unimportant -- except in cheese |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem in array | Kay Chan | C Programming Language | 2 | 05-Oct-2004 21:16 |
| Linear search on a multidimensional array. | anignna | C++ Forum | 4 | 07-Mar-2004 20:07 |
| weird search problem!! | JUNK KED | Open Discussion Forum | 3 | 11-Oct-2003 00:48 |
| Search Engine Positioning 101 and 201 "How To" Tips... | 000 | Search Engine Optimization Forum | 0 | 29-May-2003 10:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The