multiplication at 2 tables

You have two tables of integer A(NXM), B(MXR).Write a program which finds the product of the 2 tables. The result is the table C(NXR).


Correct my code plz
Don't write a program for specific rows and columns. We'll give them


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
#include<stdlib.h>


int main()
{
	int A[N][M],B[M][R],C[N][R];
	int i,j,k;

	for(i=0; i<N; i++){
		for(j=0;j<M;j++)
	  printf("give the numbers A");
	  scanf("%d %d",&A[i][j]);}

	for (i=0;i<M;i++){
		for(j=0;j<R;j++)
			printf("give the numbers of B");
	        scanf("%d %d",&B[i][j]);}

	for(i= 0; i< N; ++i)
for(j= 0; j<R; ++j)
for(k= 0; k< N; ++k)
C[i][j]+= A[i][k]* B[k][j];

	system("pause");
	return 0;
}

Last edited on
closed account (D80DSL3A)
You need &B[i][j] not &A[i][j] on line 18.
oops you're right.... thanks
closed account (D80DSL3A)
You're welcome. Is it working now?
i have to give to N,M,R values, right? But i want the program to work for general values, not to give spesific rows and columns
Last edited on
Would this help?
http://en.wikipedia.org/wiki/Matrix_multiplication

the basic rule would be a product matrix AxB = C would give the result:
CA-rows, B-columns
e.g
A3,4 times B4,3 would give C3,3

edit: I sucked at this in school...
Last edited on
gcc would let you create multi-dimensional arrays like that, but it is extension:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
  int M = 6;
  int N = 6;
  int A[M][N];
  int (* pA)[N] = new int[M][N];
}

With the pedantic option turned on the compiler will say:
main.cpp: In function 'int main()':
main.cpp:7:13: warning: ISO C++ forbids variable length array 'A'
main.cpp:7:13: warning: ISO C++ forbids variable length array 'A'
main.cpp:8:15: warning: ISO C++ forbids variable length array 'pA'
main.cpp:8:30: error: 'N' cannot appear in a constant-expression
main.cpp:7:13: warning: unused variable 'A'
main.cpp:8:15: warning: unused variable 'pA'

You could use vectors of vectors, although it is not the most efficient solution (since it stores pointer to and the size of each row vector behind the curtains):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>

using namespace std;

int main()
{
  int N = 10;
  int M = 8;

  vector< vector<int> > A(N, vector<int>(M));
  //int* pA = new int[M*N];

  for (int i = 0; i < N; ++i)
  {
    for (int j = 0; j < M; ++j)
    {
      A[i][j] =
      //pA[i * M + j] =
           i * M + j;
    }
  }

  for (int i = 0; i < N; ++i)
  {
    for (int j = 0; j < M; ++j)
    {
      cout.width(3);
      cout <<
              A[i][j]
              //pA[i * M + j]
               << ' ';
    }

    cout << endl;
  }
  
  //delete[] pA;
}
The commented lines show you how to use the dynamic memory allocation and custom address arithmetic instead. As dangerous as it is, this is arguably more efficient approach, both performance and memory-wise (but marginally so, especially for wider matrices).

Regards

Then, there are these guys, but apparently we are too lazy/busy/apathetic to review their effort:
http://www.cplusplus.com/forum/general/38435/
http://www.cplusplus.com/forum/general/38418/
:)
Last edited on
Topic archived. No new replies allowed.