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 a bool variable duplicates. Write some code that assigns true to duplicates if any two elements in the array have the same value, and that assigns false to duplicates otherwise. Use only j, k, zipcodeList, nZips, and duplicates.

Respuesta :

Answer:

The c++ program for the scenario is given below.

#include <iostream>

using namespace std;

int main() {    

   int i,j, nZips, zipcodeList[nZips];

   bool duplicates;        

   cout<<"This program checks if duplicate values exist in the array." <<endl;    

   cout<<"Enter the number of integers to be inserted in the array."<<endl;

   cin>>nZips;    

   cout<<"Enter the integers to be inserted in the array."<<endl;    

   for(i=0; i<nZips; i++)

   {

       cin>>zipcodeList[i];

   }    

   for(i=0; i<nZips; i++)

   {

       for(j=0; j<nZips; j++)

       {

           if(i != j)

           {

               if(zipcodeList[i] == zipcodeList[j])

               {

                   duplicates = true;

                   cout<<"The value of duplicates variable is true meaning "<<duplicates<<endl;

                   break;

               }

               else

                   duplicates = false;

           }

       }

       

       if(duplicates == true)

               break;

}    

   if(duplicates == false)

       cout<<"The value of duplicates variable is false meaning "<<duplicates<<endl;    

   return 0;

}  

OUTPUT

This program checks if duplicate values exist in the array.

Enter the number of integers to be inserted in the array.

7

Enter the integers to be inserted in the array.

1234

2345

1234

5678

9087

6554

4560

3456

6789

The value of duplicates variable is true meaning 1  

Explanation:

The variables mentioned in the question are used as required. There is no logic applied to validate user input. It is assumed that user inputs only valid values.

This program is designed to check whether duplicate values are present in the array. The boolean variable duplicates is assigned value true or false depending on the presence of duplicate values.

The user is prompted the number of values to be put in the array along with those values.

Once the array is filled, the array is checked for identical values. If any identical value is present, the value of duplicates variable is assigned to true and the loop is discontinued.

If no identical values are present, the loop completes and duplicates variables is assigned the value of false.

In either case, the value of the duplicates variable is displayed.

Answer:

You cant use j =0… you have to use j = k+1, otherwise j =1 and k =1 will match up and be equal when really its comparing the exact same line to itself.

Explanation:

The solution will be as follows:

duplicates = false

for (k = 0; k < nZips; k++){

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

     if (zipcodeList [k] ==zipcodeList[j]){

          duplicates =true

           break;

              }

        }

}