I'm coding a program which accepts a number as a divisor and the other numbers from the user. My problem is with segregating the array where the ten entered numbers into two different arrays, one array is for numbers divisible by a divisor entered by the user and one is for non-divisible ones. I think I've got most of it down but whenever I try to display the contents of the array it would show a 0 at the end of the line. Also when none of the entered numbers are divisible it would dispaly "16 0 1" even if those numbers are not entered by the user. Here's my code:
int main(){
int num, arr[size], div[size], nondiv[size], d=0, nd=0;
int divsize = 0;
int nondivsize = 0;
int arrsize = 0;
do{
printf("Enter a number within 1 and 5: ");
scanf("%d", &num);
if(num<1 || num>5)
printf("\nThe number you have entered is not within the given range.\n");
} while(num<1 || num>5);
printf("\nEnter ten numbers: \n");
for(int i=0; i<10; i++){
printf("Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEntered numbers: ");
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
//calculates the arrays size of arr and displays it
for(int i; i<10; i++){
if(arr[i]!= 0)
arrsize++;
}
printf("\narrsize: %d\n", arrsize);
//Stores divisible and non-divisible inputs in to different arrays
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else{
nondiv[nd] = arr[i];
nd++;
}
}
//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
printf("Number of divisible numbers: %d ", divsize);
printf("\nDivisible numbers: ");
for(int i=0; i<divsize; i++){
printf("%d ", div[i]);
}
}