Adding numbers with arrays

This is a discussion on Adding numbers with arrays within the C Programming forums, part of the General Programming Boards category; Hi mates, I'm new in this board and also newbie on c programming... I have (for me :P) a hard ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    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.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    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...

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    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

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  2. adding large numbers
    By derek23 in forum C Programming
    Replies: 6
    Last Post: 07-22-2005, 09:02 PM
  3. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 12:40 PM
  4. Adding multidimensional arrays
    By newbie2C++ in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 04:05 PM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 02:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21