Removing certain values of an array

First of all let me say hello as this is my first post.

Now here is my problem, i just started programming in school, i have like 2 weeks since i started working in C++ and here is my question.

I have an integer:
1
2
printf("\nA = ");
scanf("%d", &a);


Which will be decomposed into characters and stored into an array

1
2
3
4
5
6
7
8
9
10
for(i = 9 ; i >= 0 ; i--)
{
array[i] = a%10;
a = a / 10;
}
printf("\nThe array's elements are {");
for(i=0; i<10; i++)
{
printf("%2d,", array[i]);
}


Now my question, if i insert any number smaller that a 9 digits integer the array will be displayed as {0, 0 ... 0, n, n .. n}

I tried to figure out how to remove the "0" from the array without messing the other digits so when i call for an echo i would get {n, n .. n}
Last edited on
Hello there.

You can't "remove" elements to make them appear like they do not exists.
What you could do is searching the array starting from the beginning for a value that is different from 0. That will be the element that needs to be moved (actually you will copy it) in the first position (array[0]). From that number onwards, until the end of the array, you need to move back all the numbers to the beginning of the array, and then printf() them using a loop that executes a number of times equal to the number of significant digits of the array
Thank you for the answer, the solution seems a bit hard for me at this time, i just used

printf("%010d", a)

So now my number is 00..0nn..n (000029312) :D it`s the best solution i could come up with.
Last edited on
Topic archived. No new replies allowed.