mercredi 2 mai 2018

Filling in missing numbers from an array

I am trying to fill in the missing numbers from a user input (which is stored in an array. For example, if the user inputs 9, 5, 2, 7, 1. My program must output the missing numbers which are 3, 4, 6, 8.

You can see the missing numbers are bolded: 1, 2, 3, 4, 5, 6, 7, 8

However, my code doesn't seem to run. The while loop doesn't function. I feel like my logic is working, but its not outputting anything.

void missing_num(int length, int numbers[]) { // length is the length of the array

// A function which finds the biggest number the user inputed
int big_num_answer = biggest_num(length, numbers);

// A functon which finds the smallest number the user inputed
int small_num_answer = smallest_num(length, numbers);

int has_been_used = 0;
int i = 0;
int start_num = small_num_answer;
while(start_num <= big_num_answer) {

    if(numbers[i] == start_num + 1) {
        start_num += 1;
        has_been_used = 1;
    } else if(i >= length && has_been_used == 0) {
        printf("%d", start_num + 1);
    }

    i++;
    if (i > length) {
        i = 0;
    }
}

}

Aucun commentaire:

Enregistrer un commentaire