weird numbers on matrix multiplication

hi, first of all, sorry for my english, is not so good as I wish it was.
I'm having some weird numbers on the output of my code, when I run the program it shows strange numbers at the first row of the matrix

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace::std;
int main(){
int m, n, x, y;
cout<<"introducir filas de matriz a"<<endl; //introduce the number of rows
cin>>m;
cout<<"introducir columnas de matriz a"<<endl; //introduce the number of columns
cin>>n;
cout<<endl;
cout<<"introducir filas de matriz b"<<endl; //introduce the number of rows
cin>>x;
cout<<"introducir filas de matriz b"<<endl; //introduce the number of columns
cin>>y;
cout<<endl;
	if (n == x){
		//matriz a
		int a[m][n];
		cout<<"introduzca valores de matriz a"<<endl; //values of matrix a
		for (int i=0;i<m;i++){
			for (int j=0;j<n;j++){
			cin>>a[i][j];
			}
		}
		//matriz b
		int b[x][y];
		cout<<"introduzca valores de matriz b"<<endl; //values of matrix b
		for (int i=0;i<x;i++){
			for (int j=0;j<y;j++){
			cin>>b[i][j];
			}
		}
		//matriz c
		int c[m][y];
		for (int i=0;i<m;i++){
			for (int j=0;j<y;j++){
				for (int k=0;k<n;k++){
				c[i][j]=c[i][j]+a[i][k]*b[k][j];
				}
			}
		}
		cout<<endl<<"matriz a"<<endl;
		for (int i=0;i<m;i++){
			for (int j=0;j<n;j++){
			cout<<a[i][j]<<" ";
			}
		cout<<endl;
		}
	
		cout<<endl<<"matriz b"<<endl;
		for (int i=0;i<x;i++){
			for (int j=0;j<y;j++){
			cout<<b[i][j]<<" ";
			}
		cout<<endl;
		}
		cout<<endl<<"matriz C"<<endl;
		for (int i=0;i<m;i++){
			for (int j=0;j<y;j++){
			cout<<c[i][j]<<" ";
			}
		cout<<endl;
		}
	}
	else
	cout<<"no es posible realizar la operacion"<<endl; 
             //is not possible to realize the product
cout<<endl;
}

whenever I run the program I get something like this:
1
2
3
4
5
6
7
8
9
10
11
matriz a
1 0 
0 1 

matriz b
1 1 
1 1 

matriz C
-1217947876 17 //what? it suppose to be (1 1) !!!
1 1 

and there is a way to write a function to use less code lines?
Last edited on
Line 35: uninitialized array. Line 37: use of uninitialized value. If you do add to a variable, then it should have some known value to begin with.


Yes, you can write a function. For example, lines 41-46, 49-55, and 56-62 all do same printout and differ only in title, array, and dimensions. Those should thus be the parameters to the function.

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/arrays/
Topic archived. No new replies allowed.