Greetings, Everyone I\'m having trouble figuring out this programminglab exercis
ID: 3817960 • Letter: G
Question
Greetings, Everyone
I'm having trouble figuring out this programminglab exercise for C language. Please help and please it respond in "C".
You are given two int variables j and k, an int array zipcodeList that has been declared and initialized , an int variable nZipsthat 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.
Explanation / Answer
#include <stdio.h>
int main()
{
int j,k;
int zipcodeList[7]={101,101,103,104,105,106,107};//declare and initialize array
int nZips =sizeof(zipcodeList)/sizeof(zipcodeList[0]); //get size of array
int duplicates=0;//set duplicates to 0
for(j = 0; j < nZips; j++){
for(k = j+1; k < nZips; k++){
if(zipcodeList[j] == zipcodeList[k]){ //heck each element i array with next element if two are equal assign duplicates to 1
//printf(" %d ", zipcodeList[j]);
duplicates=1;
}
}
}//end of loop searching if no duplicates,the duplicates value will remain 0,else for loops executesand set it to 1
printf("The value of duplicates is %d ",duplicates);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.