Function integerToArray

Function integerToArray: The function/method integerToArray accepts an argument N representing an integer value.

The function/method integerToArray must return an array of integers containing the digits of N in the order of their occurrence (i.e., the integers in the array represent the digits of N).

Your task is to implement the function integerToArray so that it passes all the test cases.

The following structure is used to represent the boundedArray and is already defined in the default code (Do not write this definition again in your code).

typedef struct BoundedArray
{
    int SIZE;
    int *arr;
} boundedArray;

IMPORTANT: Do not write the main() function as it is already defined.

Example Input/Output 1:
Input:
12345

Output:
Array: 1 2 3 4 5

Explanation:
There are 5 digits in the given integer 12345, which are given below.
1 2 3 4 5

Example Input/Output 2:
Input:
-704990

Output:
Array: 7 0 4 9 9 0

#include <stdio.h>
#include <stdlib.h>
typedef struct BoundedArray
{
    int SIZE;
    int *arr;
} boundedArray;
boundedArray* integerToArray(int N)
{
boundedArray *bArr=malloc(sizeof(boundedArray));
bArr->arr=malloc(sizeof(int)*101);
bArr->SIZE=0;
char num[101];
int index=0;
sprintf(num,"%d",N);
if(num[0]=='-')
index=1;
for(;index<strlen(num);index++)
{
    bArr->arr[bArr->SIZE++]=num[index]-'0';
}
return bArr;
}
int main()
{
    int N;
    scanf("%d", &N);
    boundedArray *bArr = integerToArray(N);
    printf("Array: ");
    for(int index = 0; index < bArr->SIZE; index++)
    {
        printf("%d ", bArr->arr[index]);
    }
    return 0;
}
Previous Article

String Decryption - Character Position

Next Article

Toggle Format - List of Integers

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *