0

I am trying to create a code where given an ordered array with numbers between 1 and 10, the code returns all of the values missing.

My code is as follows:

int missingArray [] = {1, 3, 4, 5, 7, 8};

for (int i = 0; i < 11; i++) {
    if (missingArray[i] == i+1) {
        cout << "Continue. \n";
    }
    if (missingArray[i] != i+1) {
        cout << "The value of " << i+1 << " is missing. \n";
    }
}

I want the code to return Continue The value of 2 is missing Continue Continue Continue The value of 6 is missing Continue Continue The value of 9 is missing The value of 10 is missing

But instead, after I get the first "missing" element, it lists everything as missing. Anyone have any suggestions?

lottie3
  • 37
  • 2
  • 5
  • 1
    You may also want to see [Find missing number](http://stackoverflow.com/questions/3492302/easy-interview-question-got-harder-given-numbers-1-100-find-the-missing-numbe) thread on Stackoverflow – WhiZTiM Dec 26 '16 at 21:36
  • Step through the program in a debugger. You will notice something when i is 2. – Raymond Chen Dec 26 '16 at 21:39

7 Answers7

1

Your code leads to undefined behavior since missingArray[i] is not valid for values of i greater than 5.

You need to change your approach a little bit.

int missingArray [] = {1, 3, 4, 5, 7, 8};

int* start = missingArray;
int* end = start + sizeof(missingArray)/sizeof(*missingArray);

for (int i = 1; i < 11; i++)
{
   if ( std::find(start, end, i) == end )
   {
      cout << i << " is missing.\n";
   }

   // Optionally
   else
   {
      cout << "Found " << i << "\n";
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

What is REALLY going wrong is that your initial assumption - that the value (i+1) is expected at location i - becomes invalid once a missing value is detected. If you intend to detect ALL missing values, you need to decouple the array index from the value tracking. Consider the following code:

#define NMISSWING 6
int missingArray[NMISSING] = {1, 3, 4, 5, 7, 8};

int i = 0;
for (int n=1; n<=10; n++) {
    if (i >= NMISSING) break;   // all array entries checked

    if (missingArray[i] == n) {
        cout << "Continue.  \n";
        i += 1;  // Matched i'th, move on to next
    }
    else {
        cout << "The value of " << n << " is missing.  \n";
    }
}

note that I just use 'else' instead of performing essentially the same test twice. If someone is trying to teach you to to do otherwise, feel free to tell them that my opinion as a professional programmer is that that motif strikes me as academic pedantry which should be avoided

PMar
  • 26
  • 1
0

you check missingArray[i] == i+1

1 == 1

3 == 2

4 == 3

5 == 4

...

so after first condition 1==1 others are never equal.

int missingArray[] = { 1, 3, 4, 5, 7, 8 };
int k = 0;

for (int i = 0; i < 10; i++) {

    if (missingArray[k] == i + 1) {
        cout << "Continue. \n";
        k++;
    }
    else if (missingArray[k] != i + 1) {
        cout << "The value of " << i + 1 << " is missing. \n";
    }
}
0

My approach would be to select each element of the array in turn and then iterate between one greater than that value and the next element in the array.

Then to finish off iterate between the final value and the maximim value you are seeking (11 in this case).

    int missingArray [] = {1, 3, 4, 5, 7, 8};

    int j = 0;

    for(auto i = 0U; i < sizeof(missingArray)/sizeof(int) - 1; ++i)
        for(j = missingArray[i] + 1; j < missingArray[i + 1]; ++j)
            std::cout << "missing: " << j << '\n';

    for(++j; j < 11; ++j)
        std::cout << "missing: " << j << '\n';

Output:

missing: 2
missing: 6
missing: 9
missing: 10
Galik
  • 47,303
  • 4
  • 80
  • 117
0

As Pmar said, your initial assumption was not valid. I change the code a little bit. I hope this will help you.

 #include<stdio.h>
 #include <iostream>
 using namespace std;

 int main (){
 int missingArray [] = {1, 3, 4, 5, 7, 8};
 int numbers_mising = 0;

 for (int i = 0; i < 10; i++) {
     if (missingArray[i - numbers_mising] == i+1) {
        cout << "Continue. \n";
     }
     if (missingArray[i -  numbers_mising] != i+1) {
         cout << "The value of " << i+1 << " is missing. \n" << numbers_mising << "\n";
         numbers_mising++;
     }
   }
 }

In this example, also the number two is missing. You do not need to know in advance what numbers are missing with this solution. I use a variable to keep track of the numbers missing and changing the index of the array.

JBeloqui
  • 27
  • 5
0

you can go with this logic also this is very simplest logic for you.

Expected Output:
The value of 3 is missing.
The value of 7 is missing.

int missingArray[]={1,2,4,5,6,8};

    int n=sizeof(missingArray)/sizeof(missingArray[0]);

    int i=0,k=1;
    while (i<n)
    {
        if(missingArray[i]==k)
        {
            i++;
            k++;
        }
        else 
        {
           cout<<"The value of "<<k<<" is missing. \n";
           k++;
        }
        
        
    } 
-1
    int main()
{
    char array[10] = {1,2,3,4,5,6,7,7,9,10};
    char i;
    char i_2 = 1;
    char not_ok = 1;
    while(i_2 < 11){
        i = 0;
        while(i < 10){
            if(array[i] == i_2){
                not_ok = 0;
            }
            i++;
        }   
        if(not_ok){
            printf("Missing %d\n",i_2);
        }
        not_ok = 1;
        i_2++;
    }
    return 0;

}