Array sum of two arrays

#include<stdio.h>
int main()
{
int n,m,i,j,t;
scanf("%d",&n);
int a[n],c[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
c[n-i-1]=a[i];
}
scanf("%d",&m);
int b[m],d[m];
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<m;i++)
{
d[m-i-1]=b[i];
}
if(n>m)
t=n;
else
t=m;
int x[t];
for(i=0;i<t;i++)
{
if(c[i-1]+d[i-1]>=10&&i!=0)
{
x[i]=(c[i]+d[i]+1);
}
if(i>=n)
{
x[i]=d[i];
}
if(i>=m)
{
x[i]=c[i];
}
if(c[i]+d[i]>=10)
{
x[i]=c[i]+d[i]-10;
}
if(c[i]+d[i]<10)
{
x[i]=c[i]+d[i];
}
}
printf("\n");
for(i=t-1;i>=0;i–)
{
printf("%d, ",x[i]);
}
printf(“END”);

}

Please, send your code on Coding Blocks IDE.
The way you have send it, has introduced many syntax errors.

STEP:

  1. Open coding blocks IDE.
  2. Paste your code.
  3. Save it.
  4. Share the link of the IDE.

Waiting for your reply.:blush:

There are following problems in your code:

  1. I noticed your output is printing one extra digit.
    This is because you have initialized as t=n+1 or t=m+1 which is causing extra output.
    You must be dealing with the case when there would be an carry from first index.
    You have to use check for this.

  2. There are many logical errors in the following code:
    for(i=0;i<t;i++)
    {
    if(c[i-1]+d[i-1]>=10&&i!=0)
    {
    x[i]=(c[i]+d[i]+1);
    }
    if(i>=n)
    {
    x[i]=d[i];
    }
    if(i>=m)
    {
    x[i]=c[i];
    }
    if(c[i]+d[i]>=10)
    {
    x[i]=c[i]+d[i]-10;
    }
    if(c[i]+d[i]<10)
    {
    x[i]=c[i]+d[i];
    }
    }
    I would suggest you to dry run your code on different testcases, it will help you to understand the error.

If you still face issue, then let me know.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.