Unsure whats wrong with program

These are the given instructions:
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 a bool  variable duplicates .


Write some code that assigns true to duplicates if there are two adjacent elements in the array that have the same value, and that assigns false to duplicates otherwise.

Use only k , zipcodeList , nZips , and duplicates .

This is what I have so far:
k=0;
duplicates=true;
if(zipcodeList==[k]nZips)
{
duplicates=true;
k++;
}
duplicates=false;
while(k != nZips && k!=duplicates);

If anyone can show me where I messed up or why its wrong comment please thank you
[k]nZips what's mean?
the while cycle no function boby.
maybe this:

k = 0;
duplicates = false;
while(k!=nZips)
{
if(zipcodeList[k] == zipcodeList[k+1])
{
duplicates = true;
k++;
}
else
duplicates = false;

}
sorry, the above is wrong

k = 0;
duplicates = false;
while(k!=nZips)
{
if(zipcodeList[k] == zipcodeList[k+1])
duplicates = true;
else
duplicates = false;
k++;
}
c++ does not have array boundary checks,
the while will ensure that k is less than nZips, when it is entered, however, if k==(nZips-1),
the comparison fails because you are trying to reference data at location beyond the last element.
1
2
3
4
5
6
7
8
9
10
11
12
k=0;
duplicates = false;
while((k+1)<nZips){//this ensures you dont go beyond the array bounds
if(zipcodeList[k]==zipcodeList[k+1]){
duplicates = true;
break; //you have to break here, or else the second loop, might set the duplicate to false
}
else{
duplicates = false;
}
k++;
}
Last edited on
Topic archived. No new replies allowed.