- #1
doktorwho
- 181
- 6
Hi, i wrote a code and annotated it but i have few problems with it. First it won't run on Visual Studio but it runs fine on repl.it online site which uses a C ide. Second, it seems wrong when i input
5
45 6 -4 8 3
It should print out 8 3 but for some reason it misses out on 3. Could you see what's wrong with it and give me a hint on how to resolve?
5
45 6 -4 8 3
It should print out 8 3 but for some reason it misses out on 3. Could you see what's wrong with it and give me a hint on how to resolve?
C:
#include <stdio.h>
#include <stdlib.h>
//Program that from the numbers in the array searches for non-fibonacci numbers and then deletes them from the list.
void main() {
int MAX_NUM_EL = 500;
int numbers[MAX_NUM_EL];
int i, n;
int k, j, f0 = 0, f1 = 1, fib = 1;
printf("Enter the size of your array: \n");
scanf("%d", &n);
if (n>MAX_NUM_EL || n<0) {
printf("You've typed an invalid length of the array\n");
return 0;
}
if (n == 0) {
printf("Your array has no elements to check. The program will exit now\n");
return 0;
}
printf("Enter the numbers: \n");
for (i = 0; i < n; i++)
scanf("%d", &numbers[i]); for (i = 0; i < n; i++) {
if (numbers[i] < 0) { //Checking for negative numbers.
for (k = i; k < n; k++) { //If it find a negative number it deletes it by placing the forward element.
numbers[k] = numbers[k + 1]; //into it's place and reduces the number of elements by 1.
}
n--;
i--;
continue;
}
if (numbers[i] == 0)
continue;
for (j = 0; j < numbers[i]; j++) {
if (numbers[i] == fib)
continue;
f0 = f1;
f1 = fib;
fib = f0 + f1; //It goes through all the fibonacci numbers to see if that element fits.
if (numbers[i] < fib) {
for (k = i; k < n; k++) { //If we reach fibonacci that is bigger than that element does fit and we remove it.
numbers[k] = numbers[k + 1];
}
n--;
i--;
f0 = 0;
f1 = 1;
fib = 1;
continue;
}
}
}
printf("This is the resulting array: \n");
for (i = 0; i < n; i++)
printf(" %d ", numbers[i]);
return 0;
}