Need some help with searching arrays- C programming.

1) 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.


Attempt 1:
1
2
3
4
isAMember = false;
for(k=0; k<nMembers; k++)
if(currentMembers[k] == memberID)
isAMember = true;

Attempt 2:
1
2
3
4
5
6
7
8
9
10
11
12
for ( k=0 ; k<currentMembers.length ; k++ )
{
if (memberID==currentMembers[k])
{
isAMember = true ;
break ;
}
else
{
isAMember = false ;
}
}


2) You are given an int variable k, an int array zipcodeList that has been declared and initialized , an int variable nZips that contains the number of elements in zipcodeList, and an int variable duplicates.

Write some code that assigns 1 to duplicates if there are two adjacent elements in the array that have the same value , and that assigns 0 to duplicates otherwise. Use only k, zipcodeList, nZips, and duplicates.

First attempt:
1
2
3
4
5
6
7
duplicates=false;
for (k=0; k<nZips-1; k++){
    if (zipcodeList[k]==zipcodeList[k+1]){
        duplicates=true;
        break;
    }
}


Second attempt:
1
2
3
4
5
for(k=0;k<nZips - 1;k++)
{
if (zipcodeList[k]==zipcodeList[k+1])
duplicates=1;
}


3) You are given two int variables j and k, an int array zipcodeList that has been declared and initialized , an int variable nZips that contains the number of elements in zipcodeList, and an int variable duplicates.

Write some code that assigns 1 to duplicates if any two elements in the array have the same value , and that assigns 0 to duplicates otherwise. Use only j, k, zipcodeList, nZips, and duplicates.

My attempt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
duplicates = false;
for (k =0; k < nZips; k++)
{

for(j=k+1 ;j<nZips;j++)
{

if (zipcodeList[k] == zipcodeList[j])
{
duplicates = true;
break;

}

}


4)You are given an array x of c-strings along with an int variable n that contains the number of elements in the array . You are also given an array of chars representing a string named mode that has been declared . Assign to mode the mode value of the array . (Assume there are no "ties".)

NOTE: The mode is the value that occurs most frequently.

EXAMPLE: Given "msft" "appl" "msft" "csco" "ibm" "csco" "msft", the mode is "msft" because it appears the most number of times in the list.

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char* modeList[n] = {"msft", "appl", "msft", "csco", "ibm", "csco", "msft"};
std::string x[n];
int mode1count = 0;
int mode2count = 0;
int mode3count = 0;
int mode4count = 0;

int i;

for(i = 0; i < n; ++i)
  x[i] = modeList[i];

for(i = 0; i < n; ++i)
{
  if(x[i] == "msft")
    ++mode1count;
  else if(x[i] == "appl")
    ++mode2count;
  else if(x[i] == "csco")
    ++mode3count;
  else if(x[i] == "imb")
    ++mode4count;
}
Last edited on
What is the question?
Topic archived. No new replies allowed.