multiply two arrays

L

l.j.

Hi. I've a homework including calculations with arrays. I've stuck on
multiply them. Can you help me?
 
V

Vladimir S. Oka

l.j. said:
Hi. I've a homework including calculations with arrays. I've stuck on
multiply them. Can you help me?

Quite likely, but pray tell what the actual problem is.

If it's not in C, you're better of in comp.programming, or a group
dedicated to the language you're using.

If you expect us to do your homework, that can be arranged as well. For
a fee, and after you give us your teacher's details so we can send her
the solution directly.
 
L

l.j.

It is in c,we just start to learn it. I don't expect you to do my
homework, I'm working on it for days, I managed to do add up and
substraction. But I couldn't do multiplication. I just asked for help.
 
O

osmium

l.j. said:
Hi. I've a homework including calculations with arrays. I've stuck on
multiply them. Can you help me?

Well, this works in the simplest case. Does that help any?

c = a * b;
 
V

Vladimir S. Oka

l.j. said:
It is in c,we just start to learn it. I don't expect you to do my
homework, I'm working on it for days, I managed to do add up and
substraction. But I couldn't do multiplication. I just asked for help.

Please don't top-post.

I did offer to help, but you still refuse to say what your problem is?

<OT>
Do you need scalar or vector multiplication?

V1 = [a1, a2, ...], V2 = [b1, b2, ...]

Scalar (I'll use `*` for this): V1*V2 = a1*b1 + a2*b2 + ...

Vector (I'll use `x`): V1*V2 = [a1*b1, a2*b2, ...]
</OT>

Now, try to write this in C, and let us know if you have problems.
 
M

Michael Mair

l.j. said:
Hi. I've a homework including calculations with arrays. I've stuck on
multiply them. Can you help me?

Elementwise, inner, dyadic, ... product?
Give us code which can be compiled and exhibits your exact
problem. State clearly and concisely what your problem is
at this point.
If you know that your problem is a problem with data structures
or algorithms, ask in comp.programming. Once more: State your
problem clearly and concisely.

Maybe this helps:
<http://www.catb.org/~esr/faqs/smart-questions.html>


Cheers
Michael
 
L

l.j.

I want a result like this:
(5,6)*(5,2) = (2,9,1,2)
but I get this: (2,6,2)
I'm using net.2005 and my code is:
#include <stdio.h>
#define n 2


int main(){
int A[n]={5, 6},
B[n]={5, 2},
C[n+1];
int carry=0;
int i;

for(i=n-1;i>=0;i--){
C[i+1]=( carry+A*B )%10;

carry=(int)(( carry+A*B )/10);
}
C[0]=carry;
printf(" ");
for(i=0;i<n;i++)
printf("%d ",A);
printf("\n ");

for(i=0;i<n;i++)
printf("%d ",B);
printf("\n");
for(i=0;i<=n;i++)
printf("%d ",C);
printf("\n");

return 0;
}
I think I need to use another loop but where and how?
 
B

Ben Bacarisse

I want a result like this:
(5,6)*(5,2) = (2,9,1,2)
but I get this: (2,6,2)
I think I need to use another loop but where and how?

I don't want to solve this for you, but you are right. You need another
loop. Digit by digit multiplication involves multiplying the *whole* of
one array, by *each* digit of the other in turn. The results can be
accumulated into an array as you go provided you remember to add the
digits in the right position: i.e. when multiplying by the second digit
of A you add into the second (and higher) digits of C.

Note: you have declared C of size n+1 (3) but you are clearly ware that
{5,6} * {5,2} has four digits.
 
P

Pedro Graca

l.j. said:
I want a result like this:
(5,6)*(5,2) = (2,9,1,2)
but I get this: (2,6,2)
I'm using net.2005 and my code is:


#include <stdio.h>
#define n 2

int main(){
int A[n]={5, 6},
B[n]={5, 2},
C[n+1];

The C array has enough space for three elements.
There's no way it's going to be something like
(2,9,1,2)

<snip>

Oh! Just noticed this was a top post.
Please don't top post:
see <http://ursine.dyndns.org/wiki/index.php/Top_Posting>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top