*, + and - with Matrices

Hi
I'm new to programing so close one eye when you look the code
the program basicly can multilply, sum and - 2 matrices so here is the code:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

void inputMatrices(); // this one input to matrices
void matrixMultiplication(); // multiply 2 matrices
void sumMatrix(); 
void displayMatrix();  // display the result
void minusMatrix();
double matrix1[20][20];
double matrix2[20][20];
double ansMatrix[20][20];
int fMrows, fMcolumns, sMrows, sMcolumns; // defines the number of rows and colum for matrices 
int main()
{	
	
cout << "                          Matrix *, + and -" << endl;
char choice;
cout << "Enter the function that you to use  * , + or -  !" << endl;
char repeat = 'n';
do
{
switch(choice = cin.get())
{


case '*':
inputMatrices();
matrixMultiplication();
break;

case '+':
inputMatrices();
sumMatrix();
break;

case '-':
inputMatrices();
minusMatrix();
break;

default:
cout << "Incorrect character pleas type again!" << endl;
repeat = 'y';
break;
}
}while(repeat == 'y');
void displayMatrix();

cout <<"SH.H" << endl;
system("pause");
return 0;
}



void inputMatrices()
{
		
	cout << "Enter first matrix:" << endl;
	cout << "Enter rows: ";

   cin >> fMrows ;
   cout << "Enter columns: ";
   cin >> fMcolumns;
   for(int i = 0; i < fMrows; i++){
	cout << "Enter the elemnts of " << i + 1 << " row" << endl;
	for(int j = 0; j < fMcolumns; j++){
   cin >> matrix1[i][j];
	}
   }
  cout << "Enter second matrix:" << endl;
	cout << "Enter rows: ";
   cin >> sMrows ;
   cout << "Enter columns: ";
   cin >> sMcolumns;
   for(int i = 0; i < sMrows; i++){
	cout << "Enter the elemnts of " << i + 1 << " row" << endl;
	for(int j = 0; j < sMcolumns; j++){
   cin >> matrix2[i][j];
	}
   }
}
void matrixMultiplication()
{



   if(fMcolumns == sMrows)
   {
   for(int rows = 0; rows < fMrows; rows++){
	 for(int columns = 0; columns < sMcolumns; columns++){
	double sum = 0;
	for(int i = 0; i < fMcolumns; i++){
	sum += matrix1[rows][i] * matrix2[i][columns];
	}
	ansMatrix[rows][columns] = sum;
	 }
   }
	 
   }
   else
   {
   cout << "The matrixs of order " << fMrows << "X" << fMcolumns << " and " << sMrows << "X" << sMcolumns 
	   << " can not be multiplacated!\nPleas enter the correct matrices!" << endl;
	main();
   }
}
 void sumMatrix()
 {
	 if( fMrows == sMrows && fMcolumns == sMcolumns)
	 {
	 for(int i = 0; i < fMrows; i++){
	for(int j = 0; j < fMcolumns; i++)
	ansMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
	 }
 }else
cout << "Incorrect Matrice.\nPleas type the matrice again!" << endl;
	main();
 }
 void minusMatrix()
   {
	 if( fMrows == sMrows && fMcolumns == sMcolumns)
	 {
	 for(int i = 0; i < fMrows; i++){
	for(int j = 0; j < fMcolumns; i++)
	ansMatrix[i][j] = matrix1[i][j] - matrix2[i][j];
	 }

	 }else
cout << "Incorrect Matrice.\nPleas type the matrice again!" << endl;
	main();
 }

 void displayMatrix()
 {
	 for(int i = 0; i < fMrows; i++){
	   cout << "|";
	   for(int j = 0; j < sMcolumns; j++)
	   cout << setw(4) << ansMatrix[i][j];
	   cout << " |" << endl;
	 }
 }


Pleas tale me what do you think for the code I'm really interested in your opinions
Last edited on
Inconsistent indentation, recursion with main(), global data, functions producing output...
You still have a long way to go.
Topic archived. No new replies allowed.