Home > Software engineering >  How to turn my function returns an array in C?(code to fix)
How to turn my function returns an array in C?(code to fix)

Time:10-30

I am trying to make an array store each number I type in.

Some like: Enter number:687544

And return as an array like: 6 8 7 5 4 4

I only figure out how to store the nums in order in the "store (num) " function below:

#include<stdio.h>

int store(int num);

int main()
{
    int num;

    printf("Enter number: ");

    scanf("%d",&num);    

    return store(num);
}

int store(int num)
{   
    if(num!= 0)
    {
        int mod = num % 10;  //split last digit from number

        store(num/10);  //recuring it back to the right order

        printf("%i\n",mod); //divide num by 10. num /= 10 also a valid one 
    }
}

Following is I tried but not working code

#include<stdio.h>

int store(int num);

int main()
{
    int num,d;
    printf("Enter number: ");
    scanf("%d",&num);

    for(d=0; num<0;d  );
    {
        num /= 10;
    }

    int a[d];

    int i;

    for(i=0;i<d;i  ){
        a[d]=store(num);
    }

    printf("%d \n",a[d]);
}

int store(int num)
{   
    if(num!= 0)
    {
        int mod = num % 10;  

        store(num/10);  

        return mod;
    }
}

Unexpected Result.......

Enter number: 123

115183680

Feel like I almost there but I have no idea which part goes wrong. May I ask how to fix this?

I only figure out how to store the nums in order in the "store (num) " function, however I tried the to expand my code that the result is not I expected.

CodePudding user response:

Here's a modifed version with comments on what I changed

int store(int num, int digit);

int a[20];  // Don't bother with dynamic allocation

int main()
{
    int num,d;
    printf("Enter number: ");
    scanf("%d",&num);

    int digit = 0;
    int i;
    digit = store(num, 0);

    // Always print at least 1 digit
    for(i=0; i< (digit ? digit : 1); i  ){
        printf(" %d", a[i]);
    }
    printf("\n");
}

// Pass in which digit you are on
int store(int num, int digit)
{   
    if(num!= 0)
    {
        int mod = num % 10;
        int last = store(num/10, digit 1);  // next digit

        // Fill array right-to-left
        a[last - digit - 1] = mod;

        return last;
    }
    return digit;  // return number of digits
}

Output

prog$ ./foo
Enter number: 0
 0
prog$ ./foo
Enter number: 5
 5
prog$ ./foo
Enter number: 123
 1 2 3

Note that if you don't want a global array, you can add it as a third parameter to store. Don't forget to initialize a[0] in main in that case.

CodePudding user response:

Here is you code fixed, based on the same routing you have wrote, (using recursive calls) and reading int and parsing digits, etc

#include<stdio.h>
 
void store(int num, int* a, int* d);
int main()
{
     int a[10]; // int is 32bit, (Depending on compiler, you can not store more than 10 digit) log10(2**32) ~ 9.xx
     int num,d = 0;
     printf("Enter number: ");
     scanf("%d",&num);
     // the recursive function, should need the pointer to array, and the index which is currently parsing
     store(num, a, &d);
     
     // print your array till valid index,
     for(int i; i< d; i  )
         printf("%d ",a[i]);
     printf("\n");
 }   
void store(int num, int* a, int* d)
{    
     if(num!= 0)
     {
         store(num/10, a, d);
         int mod = num % 10; 
         a[(*d)  ] = mod;
     }   
} 
  • Related