GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Nov-2004, 20:13
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

How to search an array problem


Hi 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:
for (k = 0; k < nMembers; k++)
   {
      if (currentMembers[k]=currentMembers[memberID])
         {
         currentMembers[isAMember] = 1;
         }
         else 
         {
         currentMembers[isAMember] = 0;
         }
  }
  #2  
Old 15-Nov-2004, 21:20
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
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:
for (k = 0; k < nMembers; k++)
   {
      if (currentMembers[k]==memberID)
         {
         isAMember = 1;
         }
         else 
         {
         isAMember = 0;
         }
  }
  #3  
Old 16-Nov-2004, 12:46
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Thanks,

This makes sense but how do I make it check both ends of the array? Right now it only checks one end?

Quote:
Originally Posted by nkhambal
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:
for (k = 0; k < nMembers; k++)
   {
      if (currentMembers[k]==memberID)
         {
         isAMember = 1;
         }
         else 
         {
         isAMember = 0;
         }
  }
  #4  
Old 16-Nov-2004, 14:26
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
I didn't get it. Could you pls elaborate a bit . If possible with some example.

Thanks,
  #5  
Old 17-Nov-2004, 01:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by jenmaz
Thanks,

This makes sense but how do I make it check both ends of the array? Right now it only checks one end?

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:
for (k = 0; k < nMembers; k++)
{
    if (currentMembers[k]==memberID)
    {
        isAMember = 1;
    }
    else 
    {
        isAMember = 0;
    }
}
I assume you want isAMember to be 1 if memberID is in the array somewhere. If so, try this:
CPP / C++ / C Code:
isAMember = 0;    // initialize to member not found
for (k = 0; k < nMembers; k++)
{
    if (currentMembers[k] == memberID)
    {
        isAMember = 1;    // member is found
    }
}
Now isAMember will be zero if memberID is not in the array, or 1 if it is.

Version 2:
CPP / C++ / C Code:
isAMember = -1;    // initialize to member not found
for (k = 0; k < nMembers; k++)
{
    if (currentMembers[k] == memberID)
    {
        isAMember = k;    // member is found
    }
}
Now isAMember will be -1 if memberID is not in the array, or > than -1 if the member is in the array. In fact, isAMember will be the index of the member in currentMembers so it will point to the actual member.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.