0

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;
 }
 }
 }
}
asked Sep 26, 2022 at 11:15
2
  • My method would be something like this: Get input; Sort the array; Loop over the array, if the difference between the previous value (initialized to 0) and the current value is larger than 1 then there's a value missing; Save the current value as the previous value; Iterate the loop. Commented Sep 26, 2022 at 11:20
  • 1
    You want the array values (and, of course, also the array table) to have space for N elements, not M elements as in your code. Commented Sep 26, 2022 at 11:40

2 Answers 2

1
#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
 }
}
answered Sep 26, 2022 at 11:32

Comments

0

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;
}
answered Sep 26, 2022 at 12:40

2 Comments

might be my brain is tired right now, but it feels a bit harder for me to understand this one... But I am sure that it is good :)
Try to dry run it, They both are time efficient. And second one is memory efficient also.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.