1
#include <stdio.h>

void seperate(int intNum);

int main()
{
   int x;
   
   printf("Please enter an integer: \n");
   scanf("%d",&x);
   seperate(x);
}

void seperate(int intNum)
{
    int i,count=0;
    while (intNum!=0)
    {
        intNum/=10;
        ++count;  //to calculate the number of digits for user input = size of array
    }
    int array[count];   
    
    printf("The number on seperate line as follows:\n");

    for(i=count-1; i>=0; i--)
    {
        array[i]= intNum%10;
        intNum /= 10;
    }           
    for(i=0; i<=count-1; i++)
    {
        printf("%d\n", array[i]);
    }
}

The expected output(it works when the array size is fixed):

Please enter an integer:
42568
The number on separate line as follows:
4
2
5
6
8

The output:

Please enter an integer:
42568
The number on separate line as follows:
0
0
0
0
0

The code works only if the array sized is fixed, how to solve this problem? Does it not work if I create an array without declaring the size?

EDIT: the value for variable intNum changed in while loop, nothing to do with the declaration of the array

Wea 44
  • 15
  • 4
  • 1
    Do basic debugging. Use a debugger or even just debug print statements to trace the execution of the program. It has nothing to do with the declaration of the array. Consider what the value of `intNum` is at the start of the `for` loop. That's something you should be able to easily spot if you do any basic debugging. – kaylum Dec 10 '20 at 03:06
  • Here are a couple of really good links on debugging [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), consider a chat with the duck, and see [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/3422102) – David C. Rankin Dec 10 '20 at 03:19
  • `while (intNum!=0){ ... }` Guess the value of `intNum` after that loop. – Lundin Dec 10 '20 at 09:16

2 Answers2

3

Your while loop, in which you count the number of digits, is changing the given intNum variable, which will be zero at the end of that loop.

You should make a copy of intNum and use/modify that in the first (while) loop, like this:

void seperate(int intNum)
{
    int i, count = 0, temp = intNum; // Make a copy to use in the "count" loop
    while (temp != 0) {
        temp /= 10;
        ++count;  //to calculate the number of digits for user input = size of array
    }
    if (count == 0) count = 1; // To take care of the case when intNum == 0
    int array[count];

    printf("The number on seperate line as follows:\n");

    for (i = count - 1; i >= 0; i--) {
        array[i] = intNum % 10;
        intNum /= 10;
    }
    for (i = 0; i <= count - 1; i++) {
        printf("%d\n", array[i]);
    }
}

Alternatively, you could save the value of intNum and restore that after the while loop has completed:

void seperate(int intNum)
{
    int i, count = 0, save = intNum; // Save the original value of intNum ...
    while (intNum != 0) {
        intNum /= 10;
        ++count;  //to calculate the number of digits for user input = size of array
    }
    intNum = save;                   // ... and restore it after counting the digits
    if (!count) count = 1;
    int array[count];
//...

EDIT: There is a much simpler and shorter solution, with only one loop, using the sprintf function to get the digits (in the right order) for you:

void seperate(int intNum)
{
    char output[100];// Should be long enough for the largest integer.
    sprintf(output, "%d", intNum);
    printf("The number on seperate line as follows:\n");
    for (char* cp = output; *cp; ++cp) {
        printf("%c\n", *cp);
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
1

You could simplify this problem with a little math. First,

#include <math.h>

Then, the count of digits can be determined by calculating log10intNum. And you only need a single loop. Something like,

void seperate(int intNum)
{
        printf("The number on seperate line as follows:\n");
        double log10 = log(10);
        while (intNum > 0) {
                int count = (int) (log(intNum) / log10);
                int pow10 = pow(10, count);
                int digit = (int) (intNum / pow10);
                printf("%d\n", digit);
                intNum -= digit * pow10;
        }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249