Matrix Function

Hi , i wrote this code it suppose to multiply two 3X3 matrix
and it works but now i want to make it with a function and im really noob
can anyone help me please or give me some advice thanks alot

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
  #include <iostream.h>
int main(){

    int a[3][3],b[3][3],c[3][3],p,q,x;
    int sum = 0;
      cout <<"enter first matrix";
      for (p = 0; p < 3 ; p++){
          for (q = 0; q < 3 ; q++){
               cin >> a[p][q];
          }
      }
      cout <<"enter second matrix";
      for (p = 0; p < 3 ; p++ ){
          for (q = 0 ; q < 3 ; q++ ){
              cin >> b[p][q];
          }
      }
      for (p = 0 ; p <= 2 ; p++){
          for (q = 0 ; q <= 2 ; q++){

           sum = 0 ;
           for (x = 0 ; x <=2 ; x++){
               sum = sum + a[p][x] * b[x][q];
           }
           c[p][q] = sum;
      }
}
cout <<endl<<"multiply of two matrix ="<<endl;
for (p = 0 ; p < 3 ; p++)
for (q = 0 ; q < 3 ; q ++)
{
    cout<< " " << c[p][q];
}
  return 0; 
  }
Last edited on
Hello propurple,

Some questions that will help:

What IDE/compiler are you using?

Do you know what version of the C++ standards you are compiling to?

What part or parts do you want in functions?

"iostream.h" is a very old file that is no longer used. I am not good at history, but I am thinking it is pre-1998 standards or through the '98 standards.

I have reworked your code. This works with the 2011 standards.
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
#include <iostream>

int main()
{
	constexpr int MAXROW{ 3 }, MAXCOL{ 3 };

	int a[MAXROW][MAXCOL]{}, b[MAXROW][MAXCOL]{}, c[MAXROW][MAXCOL]{};
	int sum{};

	std::cout << "matris e aval ro vared kon";

	for (int p = 0; p < MAXROW; p++)
	{
		for (int q = 0; q < MAXCOL; q++)
		{
			std::cin >> a[p][q];
		}
	}

	std::cout << "matris e dovom ro vared kon";

	for (int p = 0; p < MAXROW; p++)
	{
		for (int q = 0; q < MAXCOL; q++)
		{
			std::cin >> b[p][q];
		}
	}

	for (int p = 0; p <= 2; p++)
	{
		for (int q = 0; q <= 2; q++)
		{

			sum = 0;
			for (int x = 0; x <= 2; x++)
			{
				sum = sum + a[p][x] * b[x][q];
			}
			c[p][q] = sum;
		}
	}

	std::cout << '\n' << "zarbe matris ha=" << std::endl;

	for (int p = 0; p < MAXROW; p++)
		for (int q = 0; q < MAXCOL; q++)
		{
			std::cout << " " << c[p][q];
		}

	return 0;
}

The blank lines I added make the code easier to read. Also I find the use of the {}s work better, but you are free to choose whatever form you like or have learned.

One last note: Since you "cout" statements are in a language other than English, mention what your native language is so translations can be done correctly with out having to guess.

Andy
Topic archived. No new replies allowed.