# Multiplying Arrays

• (2 Pages)
• 1
• 2

## 19 Replies - 2151 Views - Last Post: 04 May 2010 - 08:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=171786&amp;s=9f63b0cc56753cdfc04471158cbc42fb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

# Multiplying Arrays

Posted 03 May 2010 - 10:43 AM

The add() and add1() functions seem to be working correctly. The multiply function does not and i cant work out why not. It only works for n<=2

```#include <stdio.h>
#define SIZE 1001
#define BASE 10

int add(int *c,int *d,int* e); //adds arrays c and d into e
int multiply(int *f,int multiple,int* g); //multiplies f by multiple into g

int main()
{
int a[SIZE],b[SIZE],test=0,i;
for(i=0;i<SIZE;i++)//initialize a & b to 0
{
a[i]=0;
b[i]=0;
}

a[1000]=9;
a[999]=1;   //a=19
int n=3;        //only works where n<=2
multiply(a,n,B)/>; //b=n*a btw b is not capitalized in my code

for(i=0;i<SIZE;i++) //prints b
{                                       /* /removes leading zero's */
if(test==0 && b[i]==0) continue; // ie 1 instead of 00000001
test=1;
printf("%d",b [i]);
}
if(!test)printf("0");

return 0;
}

int add1(int x,int y,int* z)  //adds digits - and takes care of the carry into z
{
int p;
p=z[x]+y;
if(p<BASE)   //this recursively tests whether there is a carry
{
z[x]+=y;
return 0;
}
else
{
z[x]=(p%BASE);
return 0;
}
}

int add(int *c,int *d,int* e)  //adds two arrays c and dtogether into and puts answer into array e
{
int i,sum;
for(i=SIZE-1;i>=0;i--)
{
sum=c[i]+d[i];
if(e[i]+sum<BASE)       //no carry if true
{
e[i]+=sum;
}
else
{
}
}
return 0;
}

int multiply(int *f,int multiple, int* g)  // times array f by multiple - into array g
{                                          // using 5*a = a+a+a+a+a
int i;
if(multiple==2)
{
return 0;
}
else
for(i=0;i<multiple;i++)
{
}
return 0;
}

```

This post has been edited by simeesta: 03 May 2010 - 10:46 AM

Is This A Good Question/Topic? 0

## Replies To: Multiplying Arrays

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Multiplying Arrays

Posted 03 May 2010 - 11:30 AM

What is the purpose of your constant BASE and what is the purpose of the "carry" code in add and add1? What sort of numbers and what sort of arrays are you dealing with?

### #3 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Multiplying Arrays

Posted 03 May 2010 - 11:41 AM

r.stiltskin, on 03 May 2010 - 05:30 PM, said:

What is the purpose of your constant BASE and what is the purpose of the "carry" code in add and add1? What sort of numbers and what sort of arrays are you dealing with?

dealing with very large numbers e.g. 2^1000.
but using small numbers to test and get the function correct,

e.g

__ 9 9
__ 9 9+
_ 1 9 8
_1 1 carry

base 10 means digits 0 to 9 ie decimal so you cant have 10 or 11 or A (hexadecimal)as a digit

This post has been edited by simeesta: 03 May 2010 - 11:44 AM

### #4 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Multiplying Arrays

Posted 03 May 2010 - 11:49 AM

Are you trying to implement simple assignment
g = f*multiple
or
g = g + (f*multiple) (i.e. g += f*multiple)

This post has been edited by r.stiltskin: 03 May 2010 - 11:49 AM

### #5 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Multiplying Arrays

Posted 03 May 2010 - 11:52 AM

r.stiltskin, on 03 May 2010 - 05:49 PM, said:

Are you trying to implement simple assignment
g = f*multiple
or
g = g + (f*multiple) (i.e. g += f*multiple)

g = f * multiple

using addition though e.g. 5*f = f+f+f+f+f etc

### #6 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: Multiplying Arrays

Posted 03 May 2010 - 11:59 AM

Interesting, rather than outright multiplying digit by digit, you rely on repeated addition....

### #7 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Multiplying Arrays

Posted 03 May 2010 - 12:12 PM

```int multiply(int *f,int multiple, int* g)
{
int i;
for(i=0;i<SIZE;i++)
{
g[i]=f[i];
}

for(i=0;i<multiple-1;i++)
{
}
return 0;
}

```

This post has been edited by r.stiltskin: 03 May 2010 - 12:23 PM

### #8 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Multiplying Arrays

Posted 03 May 2010 - 12:18 PM

r.stiltskin, on 03 May 2010 - 06:12 PM, said:

```int multiply(int *f,int multiple, int* g)
{
int i;
for(i=0;i<SIZE;i++)
{
g[i]=0;
}
for(i=0;i<multiple-1;i++)
{
}
return 0;
}

```

isnt that the same as writing:

g=f+f;
g=f+f;
g=f+f;
... or g=2f which is doubling
multiple times etc???

whereas i want
g=g+f;
g=g+f;
...
etc.

also g is an external variable ( b[] ) so does not have to be zeroed.

This post has been edited by simeesta: 03 May 2010 - 12:19 PM

### #9 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Multiplying Arrays

Posted 03 May 2010 - 12:22 PM

Sorry, while you were writing I was editing. See my revised post above.

### #10 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Multiplying Arrays

Posted 03 May 2010 - 12:27 PM

r.stiltskin, on 03 May 2010 - 06:22 PM, said:

Sorry, while you were writing I was editing. See my revised post above.

Thanks for the help anyway.

My origninal code was to sum to the digits of the number of 2^1000 for project euler no. 16. which gives the correct answer

here it is :

```/* Program to find the sum of the digits of the number 2^1000 */

#include <stdio.h>
#define SIZE 1001
#define BASE 10

int* add(int *c,int *d,int* e); //adds arrays c and d into e
int twice(int *c,int* d);       //doubles c into d

int main()
{
int i,j,sum,value[SIZE];
int a[SIZE];
int test=0;
{
value[i]=0;
a[i]=0;
}

//calculate 2^1000
a[1000]=1;
for(i=0;i<1000;i++)
{
twice(a,value);
for(j=0;j<=1000;j++)
{
a[j]=value[j];
value[j]=0;
}
}

printf("2 ^ 1000 is :\n");
for(i=0;i<SIZE;i++)//print 2^1000
{
if(test==0 && a[i]==0) continue;
test=1;
printf("%d",a [i]);
}
if(!test)printf("0");
sum=0;

for(i=0;i<SIZE;i++)//sum up the digits
{
sum+=a[i];
}
printf("\n\nthe sum of its digits is %d\n\n",sum);
return 0;
}

int* add1(int x,int y,int* z)  //adds digits - and takes care of the carry into z
{
if(z[x]+y<BASE)   //this recursively tests whether there is a carry
{
z[x]+=y;
return z;
}
else
{
z[x]=((z[x]+y)%BASE);
}
}

int* add(int *c,int *d,int* e)  //adds two arrays c and dtogether into and puts answer into array e
{
int i,sum;
for(i=SIZE-1;i>=0;i--)
{
sum=c[i]+d[i];
if(e[i]+sum<BASE)       //no carry if true
{
e[i]+=sum;
}
else
{
}
}
return e;
}

int twice(int *f, int* g)  // times array f by 2 - into array g. g=(f+f)
{

}

```

i've just change the twice function to multiply a number by something else instead of just two. perhaps this may help?

Thanks to everyone trying to help as well, in case I forget

### #11 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Multiplying Arrays

Posted 03 May 2010 - 12:36 PM

???

Is there something wrong with this:
```int multiply(int *f,int multiple, int* g)
{
int i;
for(i=0;i<SIZE;i++)
{
g[i]=f[i];
}

for(i=0;i<multiple-1;i++)
{
}
return 0;
}

```

### #12 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Multiplying Arrays

Posted 03 May 2010 - 12:41 PM

r.stiltskin, on 03 May 2010 - 06:36 PM, said:

???

Is there something wrong with this:
```int multiply(int *f,int multiple, int* g)
{
int i;
for(i=0;i<SIZE;i++)
{
g[i]=f[i];
}

for(i=0;i<multiple-1;i++)
{
}
return 0;
}

```

just copied that into my code and gives the same answer as the original
however 3*19 is not 293.

i've got a feeling it's something to do with variable scope of g and f but they're pointer so it should not matter. right?

### #13 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Multiplying Arrays

Posted 03 May 2010 - 12:57 PM

True, 3*19 is not 293. But neither is 19+19==331 nor 19+0==350.

I think you'll find that the problem is in add or add1, not multiply.

I modified your main to this:
```int main()
{
int a[SIZE],b[SIZE],test=0,i;
for(i=0;i<SIZE;i++)//initialize a & b to 0
{
a[i]=0;
b[i]=0;
}

int z[SIZE] = {0};
a[1000]=9;
a[999]=1;   //a=19
int n=3;        //only works where n<=2

multiply(a,n,B)/>; //b=n*a btw b is not capitalized in my code
for(i=0;i<SIZE;i++) //prints b
{                                       /* /removes leading zero's */
if(test==0 && b[i]==0) continue; // ie 1 instead of 00000001
test=1;
printf("%d",b [i]);
}
if(!test)printf("0");
printf("\n\n");
test = 0;

add(a,a,B)/>; //b=n*a btw b is not capitalized in my code
for(i=0;i<SIZE;i++) //prints b
{                                       /* /removes leading zero's */
if(test==0 && b[i]==0) continue; // ie 1 instead of 00000001
test=1;
printf("%d",b [i]);
}
if(!test)printf("0");
printf("\n\n");
test = 0;

add(a,z,B)/>; //b=n*a btw b is not capitalized in my code
for(i=0;i<SIZE;i++) //prints b
{                                       /* /removes leading zero's */
if(test==0 && b[i]==0) continue; // ie 1 instead of 00000001
test=1;
printf("%d",b [i]);
}
if(!test)printf("0");
printf("\n\n");
test = 0;

return 0;
}

```

ed: damn smileys -- watch out for those uppercase B's.

Supposedly if you turn off emoticons that shouldn't happen. Not sure if that works, but I keep forgetting to do it anyway.

This post has been edited by r.stiltskin: 03 May 2010 - 12:59 PM

### #14 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Multiplying Arrays

Posted 03 May 2010 - 01:03 PM

```#include <stdio.h>
#define SIZE 1001
#define BASE 10

int add(int *c,int *d,int* e); //adds arrays c and d into e
int multiply(int *f,int multiple,int* g); //multiplies f by multiple into g

int main()
{
int a[SIZE],b[SIZE],c[SIZE],test=0,i;
for(i=0;i<SIZE;i++)//initialize a & b to 0
{
a[i]=0;
b[i]=0;
c[i]=0;
}

a[1000]=9; //a=19
a[999]=1;
b[1000]=9;
b[999]=1; //b=19

for(i=0;i<SIZE;i++) //prints b
{                                       /* /removes leading zero's */
if(test==0 && c[i]==0) continue; // ie 1 instead of 00000001
test=1;
printf("%d",c [i]); //prints 38???
}
if(!test)printf("0");

return 0;
}

int add1(int x,int y,int* z)  //adds digits - and takes care of the carry into z
{
int p;
p=z[x]+y;
if(p<BASE)   //this recursively tests whether there is a carry
{
z[x]+=y;
return 0;
}
else
{
z[x]=(p%BASE);
return 0;
}
}

int add(int *c,int *d,int* e)  //adds two arrays c and dtogether into and puts answer into array e
{
int i,sum;
for(i=SIZE-1;i>=0;i--)
{
sum=c[i]+d[i];
if(e[i]+sum<BASE)       //no carry if true
{
e[i]+=sum;
}
else
{
}
}
return 0;
}
int multiply(int *f,int multiple, int* g)  // times array f by multiple - into array g
{                                          // using 5*a = a+a+a+a+a
int i;
if(multiple==2)
{
return 0;
}
else
for(i=0;i<multiple;i++)
{
}
return 0;
}

```

which is correct

This post has been edited by simeesta: 03 May 2010 - 01:04 PM

### #15 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Multiplying Arrays

Posted 03 May 2010 - 01:21 PM

Right. Sorry, I forgot to zero b in between.