14,694,530 members
See more:
if array input: 1 0 0 0
incremented array should be:1 0 0 1
decremented array: 0 9 9 9

i have used label and loop to complete this task. but it seems complex in reciting it. so i need to know is there any simple method to solve this program. if so help me.
here is my code.

```#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],c[20],n,i,j;
clrscr();
printf("\n Enter the no of elemnts");
scanf("%d",&n);
printf("\n enter the array\t");
for(i=0;i<n;i++)>
scanf("%d",&a[i]);
for(i=0;i<n;i++)>
c[i]=a[i];
j=n;
check:
if((a[j-1]+1)<=9)
a[j-1]++;
else
{
a[j-1]=0;
j--;
goto check;
}
if(a[0]==0)
{
for(i=1,j=0;i<=n;i++,j++)
b[i]=a[j];
b[0]=1;
printf("result of incremnted array\t");
for(i=0;i<=n;i++)
printf("%d\t",b[i]);
}
else
{
printf("result of incremnted array\t");
for(i=0;i<n;i++)>
printf("%d\t",a[i]);
}
j=n;
while(j>=0)
{
if(c[j-1]==0)
{
if(c[j-2]==0)
j=j-1;
else
{
c[j-2]--;
c[j-1]=c[j-1]+10;
if(j!=n)
j=j+1;
}
}
else
{
c[j-1]--;
j=-1;
}
}
printf("\n decremented array");
for(i=0;i<n;i++)>
printf("%d\t",c[i]);
getch();
}```
Posted
Updated 16-Dec-14 4:17am
v2

## Solution 1

A simple increment function may look like this (assuming the values with the lowest weight are stored at the end):
```void incrementArray(int size, int *arr)
{
int i;
for (i = size - 1; i >= 0; i--)
{
// Increment value and check for overflow
// Stop processing if no overflow
if (++arr[i] < 10)
break;
// Overflow: Set item to zero and continue processing
arr[i] = 0;
}
}```

The decrement function would be similar:
```if (--arr[i] >= 0)
break;
arr[i] = 9;```
moyna coder 16-Dec-14 10:46am

this is incrementing each element in an array right.. thanks for ur answer. but what i asked is incrementing the whole content of array not each of its elements
Jochen Arndt 16-Dec-14 11:44am

It is not incrementing each element. There is a condition that leaves the loop so that the remaining elements are left unchanged.
Input: 1000
1. loop: 1001
2. loop: Not executed because the loop is left by the break statement upon 1 < 10
Output: 1001 (as requested)
moyna coder 17-Dec-14 3:26am

yeah i got it. i did not think of this. thank u

## Solution 2

If I were to do such a thing, I would use recursion and work with my arrays as pointer references for array x[n] I would work with x, reference it's content via *x, and move through the array via x++.

The recursive function for 'add' would check the value in it's particular reference x (as *x) and increment/decrement the value, and handle boundary conditions. The boundary conditions would be where the recursion comes in.

You process reminds me a bit of the old BCD data type.
moyna coder 16-Dec-14 10:48am

yeah you are right. i could also used pointers if i were to increment each element in an array. but our task is to consider the whole content of an array as single element and need to increment that value. hope u understand. if so try
W Balboos, GHB 16-Dec-14 11:08am

But you're using references to the elements such as x[i]. I just changed the method of referencing the elements as it works out better with recursion. To do what you suggest, above, you'd need to cast the pointer to the head of the array as the appropriate type - and then do your increment/decrement on that BUT since that will implement as hexadecimal you'd need that cast to be to a BCD type for the casting method to work. I don't believe many CPU's/Compilers still support BCD operations.