2

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]);
}
}
11
  • 1
    It would be useful to explain what 'size' is and where it's set. Commented Nov 25, 2015 at 4:17
  • did you define size globally? @QuinnDumala Commented Nov 25, 2015 at 4:18
  • when posting a question about a runtime problem, the posted code needs to cleanly compile. It greatly helps to post some example input and the expected output and the actual output. Given the posted code, which does not cleanly compile, are we expected guess as to which header files are #include'd? Commented Nov 25, 2015 at 17:16
  • Please consistently indent the code, indent after every opening brace '{' and un-indent before every closing brace '}' Commented Nov 25, 2015 at 17:18
  • For us humans the code needs to be very readable. in general, this means `only one statement per line and no more than one variable declaration per statement. Commented Nov 25, 2015 at 17:19

2 Answers 2

2

I did some changes to your code and i think it works fine.....

in your code:

//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
    if(div[i] != 0){
        divsize++;
    }
}

i don't think this is necessary as you've already caluclated number of divisble and non divisble numbers and stored them in d and nd respectively (and) the numbers in the arrays div[] and nondiv[] in this loop:

 for(int i=0; i<10; i++){
    if(arr[i]%num == 0){
        div[d] = arr[i];
        d++;
    }

    else{
        nondiv[nd] = arr[i];
        nd++;
    }
}

so while printing the number of divisible numbers and the divisible numbers array you can us d as parameter instead of divsize like this:

printf("Number of divisible numbers: %d ", d);//changed to d
printf("\nDivisible numbers: ");
for(int i=0; i<d; i++) //even here
{
    printf("%d ", div[i]);
}

so to sum it all up, remove the last but one loop and change the parameters of last loop from divsize to d in the last loop

*** and by the way I hope you declared size globally

so the code would be:

#include<stdio.h>
#define size 10 //i defined size globally here
int main()
{
int num, arr[size], div[size],nondiv[size],d=0, nd=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=0; 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++;
    }
}


printf("Number of divisible numbers: %d ", d);
printf("\nDivisible numbers: ");
for(int i=0; i<d; i++){
    printf("%d ", div[i]);
}
}

-thank you

Sign up to request clarification or add additional context in comments.

2 Comments

It works now! I can't believe the solution was that simple. Thank you so much man,
ya...buddy.. you just added some extra loop, your welcome :) @QuinnDumala
1
  1. You declared a loop variable but didn't initialize it so its values is garbage value. Where you wrote

    for(int i; i< size ;i++)
    

    you should use i=0.

  2. Other mistake is you can not declare size globally mean declare size as global variable and use as Loop variants means

    for(int i=0;i< size;i++)
    

    I also add commenting in your program where I change.

  3. This loop isn't required, it makes the program too complex:

    for(int i=0; i<10; i++){
        if(div[i] != 0){
            divsize++;
        }
    }
    

    Because You already have counts of div array with the name of d.

Here is your updated Program

#include<stdio.h>
    #include<conio.h>
    #define size 10 // Every where you use hard coded 10 change it to size 
    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=0; i<10; i++){ 
    // In loop variable you did'nt initialize i that's why it shows garbage value which is greater than 10000
        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 ", d/*Here I just print d Because it is the count of divisible*/);
    printf("\nDivisible numbers: ");
    for(int i=0; i<d/*Here also used d */  ; i++){
        printf("%d ", div[i]);
    }
    }

Comments

Your Answer

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.