# Adding numbers with arrays

Printable View

• 04-16-2009
Oliveira
Adding numbers with arrays
Hi mates, I'm new in this board and also newbie on c programming...
I have (for me :P) a hard task to add two numbers with the help of two arrays as follows:

1 - Ask to the user to put the number of algarisms of the first number
2 - Ask again for the second number
3 - Do the sum and print the result

Example:

n1=3
E1 - 2
E2 - 5
E3 - 6

n2=2

E1 - 3
E2 - 4

Result = 290

I can do entirely the "performing" part (ask the length and the numbers one by one) but can't do properly the sum itself.

Thanks for the opportunity to expose my task here, any help would be appreciated.
• 04-16-2009
whiteflags
For every element add the two elements together in a temp variable. Add the temp to the sum after you've done the multiplication to reflect the place value.

For example

350 + 150
sum += (0 + 0) * 1
sum += (5 + 5) * 10
sum += (3 + 1) * 100
• 04-16-2009
Oliveira
Hi whiteflags

but how can I fix the trouble (the "carrying 1") that appears when I take a sum of two algarisms that results on a number >9?

I'm a little bit confused, I've understood your idea under a mathematics point of view, but I simply can't realize how to "translate it" to C.

I'm browsing through the thread about recommended books, I've had a good impression on "Deitel & Deitel" C How to Program, I think I have to go deeper if I want to finish my course hehe...
• 04-16-2009
whiteflags
If you're going to print the sum I suggest you use an integer, but I will explain further.

I assume by your statement that you know how to do the performing part, you can get two arrays from the users input. Take those arrays, and add the elements like I showed you.

The only bit of trouble is if one array is longer than the other.

For example 350 + 16
sum += (0 + 6) * 1
sum += (5 + 1) * 10
sum += 3 * 100

This means that you should be able to do this with two loops. One loop iterates over the smaller array and adds it with the longer one as part of the sum, and another loop has to add the rest of the longer array to the sum while accounting for place value.

I kinda had hopes you would figure that out, but there should be no problems with carrying here. Try it out.
• 04-16-2009
Oliveira
Hi whiteflags

I didn't read yet your previous post, I will put below what I did until now so you can "feel" my level on C, I think I'm near from my objective :p

Code:

```#include<stdio.h> #define MAX 1000 int main () {         int v[3][MAX];         int temp[2][MAX];         int i,n1,n2, max;         printf("\nDigite o valor de N1\n");         scanf("%d",&n1);         printf("\nPrimeira Sequencia\n");         for(i=0;i<n1;i++)         {                 printf("Elemento %d\t",i+1);                 scanf("%d",&temp[0][i]);         }         printf("\nDigite o valor de N2\n");         scanf("%d",&n2);         for(i=0;i<n2;i++)         {                 printf("Elemento %d\t",i+1);                 scanf("%d",&temp[1][i]);         }         //---------------------------------------------------------------                 /*Padronizando o tamanho dos vetores*/         if(n1>=n2)                 max=n1;         else                 max=n2;         for(i=max;i>=0;i--)         {                 v[0][i]=0;                 v[1][i]=0;                 }                 for(i=n1-1;i>=0;i--)         {                 v[0][i]=temp[0][i];         }         for(i=n2-1;i>=0;i--)         {                 v[1][i]=temp[1][i];                }         for(i=max-1;i>=0;i--)                 v[2][i+1]=v[0][i]+v[1][i];                for(i=max-1;i>=0;i--)                 v[2][i+1]=v[0][i]+v[1][i];         v[2][0]=0;                for(i=max-1;i>=0;i--)                {                 if(v[2][i+1]>9)                 {                         v[2][i]+=1;                         v[2][i+1]-=10;                 }                                } //---------------------------------------------------------------------                printf("0");                for(i=0;i<max;i++)         {                 printf("%d",v[0][i]);         }         printf("\n");         printf("0");         for(i=0;i<max;i++)         {                 printf("%d",v[1][i]);         }         printf("\n"); //--------------------------------------------------------------------         for(i=0;i<=max;i++)         {                 printf("%d",v[2][i]);         } //---------------------------------------------------------------------         printf("\n");         return 0; }```
the written on the printf's are in Portuguese, they are just asking for the user put the length of the numbers and their respective algarisms.

Now I will read your post, thanks for trying to help me :)