Home > front end >  how can I make a loop print out missing elements in my int array?
how can I make a loop print out missing elements in my int array?

Time:09-26

I've been trying to solve an assignment where you:

  1. Enter the size N of an array of ints
  2. Enter the max value M of the ints that you want to populate the array with
  3. Enter N values that are of a value between 1 and M, into a second array.
  4. compare these two and print out the missing numbers...

Like this:

Size of array? 10        // => N = 10
Max value in array? 8    // => M = 8

Please enter 10 values between 1 and 8:
4 1 3 1 7 3 4 4 6 1

Missing values: 2 5 8

for some reason, my for loop just prints out all the numbers between 1 and M instead, no matter what I try... What am I missing??

code:

#include <stdio.h>

int main(void)
{
    int aSize, mValue;



    printf("Size of array? ");
    scanf(" %d", &aSize);
    printf("Max value in array: ");
    scanf(" %d", &mValue);

    int table[aSize];
    int values[mValue];

    for (int i = 0; i < aSize; i  )
    {
        table[i] = i 1;
        if ((i 1) > mValue)
        {
            table[i] = 0;
        }
    }

    printf("Please enter %d values between 1 and %d:\n", aSize, mValue);

    for (int i = 0; i < mValue; i  )
    {
        scanf(" %d", &values[i]);
    }

    for(int i = 0; i < aSize; i  )
    {
        for (int j = 0; j < mValue; j  )
        {
            if(table[i] != values[j] && table[i] != 0)
            {
                printf("%d ", table[i]);
                break;
            }
        }
    }
}

CodePudding user response:

#include <stdio.h>
   int main()
   {
    int aSize, mValue;
    printf("Size of array? ");
    scanf(" %d", &aSize);
    printf("Max value in array: ");
    scanf(" %d", &mValue);

    int table[aSize];
    int values[aSize]; // not 'mSize' because it is just max value not size of array

    for (int i = 0; i < aSize; i  )
    {
        table[i] = i 1;
        if ((i 1) > mValue)
        {
            table[i] = 0;
        }
    }

    printf("Please enter %d values between 1 and %d:\n", aSize, mValue);

    for (int i = 0; i < aSize; i  )
    {
        scanf(" %d", &values[i]);
    }

    for(int i = 0; i < aSize; i  )
    {
        int flag=0;
        for (int j = 0; j < aSize; j  )
        {
            if(table[i] == 0 || table[i] == values[j]) // numbers in common or zero
            {
                flag=1;
                break;
            }
        }
        if(flag == 0) printf("%d",table[i]); // missing numbers
    }
}

CodePudding user response:

In this code, I'm creating an extra array to store the occurance of the variable, then printing it. 1 means present 0 means not present.

#include <stdio.h>

int main(void) {
    int aSize, mValue;

    printf("Size of array? ");
    scanf(" %d", &aSize);
    printf("Max value in array: ");
    scanf(" %d", &mValue);

    int table[aSize];
    int values[mValue 1];
    for (int i = 0; i <= mValue; i  )
    {
        values[i]=0;
    }
    values[0]=1;

    printf("Please enter %d values between 1 and %d:\n", aSize, mValue);

    for (int i = 0; i < aSize; i  )
    {
        scanf(" %d", &table[i]);
        if(table[i]>mValue || table[i]<1){
            printf("Enter numbers in given range");
            return 0;
        }
    }

    for(int i = 0; i < aSize; i  )
    {
        values[table[i]]=1;
    }

    printf("Missing values:\n");
    for(int i = 1; i <= mValue; i  )
    {
        if(!values[i])
        printf("%d\n",i);
    }
    return 0;
}

This is better version of the above logic.

#include <stdio.h>

int main(void) {
    int aSize, mValue;

    printf("Size of array? ");
    scanf(" %d", &aSize);
    printf("Max value in array: ");
    scanf(" %d", &mValue);
    
    int temp;
    int values[mValue 1];
    values[0]=1;

    printf("Please enter %d values between 1 and %d:\n", aSize, mValue);

    for (int i = 0; i < aSize; i  )
    {
        scanf(" %d", &temp);
        if(temp>mValue || temp<1){
            printf("Enter numbers in given range");
            return 0;
        }
        values[temp]=1;
    }
    
    printf("Missing values:\n");
    for(int i = 1; i <= mValue; i  )
    {
        if(!values[i])
        printf("%d\n",i);
    }
    return 0;
}
  • Related