Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
example:
if array input: 1 0 0 0
incremented array should be:1 0 0 1
decremented array: 0 9 9 9

i have used labels 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 in improving my c skill.
here is my code.
C#
#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 3:05am
v2
Comments
Maciej Los 16-Dec-14 7:44am    
?
Jochen Arndt 16-Dec-14 8:04am    
You may show us your loop code.

Such a loop should be not very complex:
- Start at the index with the lowest weight
- Increment or decrement the array item value
- If there is no overflow (> 9) or underflow (< 0) leave the loop using the break statement
- Adjust the value (0 upon overflow and 9 upon underflow)
- Change the loop index to process the next item

Try converting your array into a long value doing your computations and converting back int an array.
 
Share this answer
 
Comments
moyna coder 16-Dec-14 8:56am    
how can i convert array to a long value. i am beginner i thought of this idea but i cant code it. so i used another method
At first, you may code it without the (more elegant) for loop. Just do it as you would do with pencil and paper.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900