9Cookiee
9Cookiee

Reputation: 45

adding two array of numbers together and put it into a new array (C)

I am trying to randomly generate two 30-digits array and add them up. The result has to be put into a separate new array. I am having trouble to add up two numbers together if their sum is bigger than 10. Can anyone help me?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>


int main()
{
int numlist[30],numlist2[30],addnum[60],i,j,k;
srand(time(NULL));


for (i=0;i<30;i++)
{
    numlist[i] = rand()%10;

}

for (j=0;j<30;j++)
{
    numlist2[j]=rand()%10;
}

for (k=0;k<30;k++)
{

    if ((numlist[k]+numlist2[k])<10)
        addnum[k] =  numlist[k]+numlist2[k];
    else
       /*dont know what to do*/



}
return 0;
}

Upvotes: 0

Views: 82

Answers (1)

Geoduck
Geoduck

Reputation: 8995

Use a carry register:

int carry = 0;
for (k=0;k<30;k++)
{
    int adder = numlist[k]+numlist2[k]+carry;
    carry = adder/10;
    addnum[k] = adder % 10;
}
addnum[k] = carry;

Upvotes: 1

Related Questions