Passing Dynamic Multi-Dimensional arrays
Jan 29, 2015 at 9:32am UTC
If any one is on this late I could really use some help. I need to create subfunctions to do basic Matrix mathematics (addition, subtraction, etc.) I need to be able to pass bot of my Matrices to subfunctions. I started with the addition sub function and I cant get the code to run. I keep getting expected primary-expression before ']' token error for line 75. If your out there please help!!!!
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
#include <iostream>
#include <stdio.h>
using namespace std;
void Matrix_Addition(double Matrix_A [][10],double Matrix_B [][10]);
int main()
{
int Matrix_size = 10;
double Matrix_A [Matrix_size][Matrix_size];
double Matrix_B [Matrix_size][Matrix_size];
double Subraction_resultant [Matrix_size][Matrix_size];
double Transpose_resultant [Matrix_size][Matrix_size];
double Matrix_C [Matrix_size][Matrix_size];
// Have the user enter the initial size of the matrix
cout << ("Enter size of the square matrix\n(Example Enter 3 for a 3x3 Matrix):\n" );
cin >> Matrix_size;
// Program will not compile with a matrix size grater than 10
// User will enter elements of each matrix going down rows than over in columns
//Dump elements into the first Matrix
cout << ("Enter elements of the first matrix:\n" );
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
cin >> Matrix_A[y][x];
}
cout << "\n" ;
}
//Dump elements into a Second matrix
cout << ("Enter elements of the Second matrix:\n" );
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
cin >> Matrix_B[y][x];
}
cout << "\n" ;
}
// Actual matrices
cout << ("Resulting Matrices:\n" );
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
cout << Matrix_A[y][x] << " " ;
}
cout << "\n" ;
}
cout <<"\n\n" ;
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
cout << Matrix_B[y][x] << " " ;
}
cout << "\n" ;
}
// Addition function call
Matrix_Addition ( Matrix_A[][10] , Matrix_B[][10], Matrix_size);
//matrix subtraction
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
Subraction_resultant[y][x] = Matrix_A [y][x] - Matrix_B [y][x];
}
}
cout << ("Resulting subtraction:\n" );
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
cout << Subraction_resultant[y][x] << " " ;
}
cout << "\n" ;
}
// matrix Transpose
cout << ("Resulting Matrices:\n" );
for (int x=0; x < Matrix_size; x++)
{
for (int y=0; y < Matrix_size; y++)
{
cout << Matrix_A[y][x] << " " ;
}
cout << "\n" ;
}
cout <<"\n\n" ;
for (int x=0; x < Matrix_size; x++)
{
for (int y=0; y < Matrix_size; y++)
{
cout << Matrix_B[y][x] << " " ;
}
cout << "\n" ;
}
//matrix multiplication
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
Matrix_C[y][x]=0;
for (int z=0; z< Matrix_size; z++)
{
Matrix_C[y][x] = Matrix_C[y][x]+( Matrix_A [y][z] * Matrix_B [z][x]);
}
}
}
cout << ("Resulting Multiplication:\n" );
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
cout << Matrix_C [y][x] << " " ;
}
cout << "\n" ;
}
/* cout << ("Resulting Matrices:\n");
for(int y=0; y < Matrix_size; y++)
{
for(int x=0; x < Matrix_size; x++)
{
cout << Matrix_A[y][x] << " ";
}
cout << "\n";
}
cout <<"\n\n";
for(int y=0; y < Matrix_size; y++)
{
for(int x=0; x < Matrix_size; x++)
{
cout << Matrix_B[y][x] << " ";
}
cout << "\n";
}
*/
return 0;
}
//matrix addition
void Matrix_Addition(double Matrix_A [][10], double Matrix_B [][10], int Matrix_size)
{ double Addition_resultant [Matrix_size][Matrix_size];
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
Addition_resultant[y][x] = Matrix_A [y][x] + Matrix_B [y][x];
}
}
cout << ("Resulting addition:\n" );
for (int y=0; y < Matrix_size; y++)
{
for (int x=0; x < Matrix_size; x++)
{
cout << Addition_resultant[y][x] << " " ;
}
cout << "\n" ;
}
return ;
}
Jan 29, 2015 at 1:26pm UTC
The parameter are passed like so:
Matrix_Addition ( Matrix_A , Matrix_B, Matrix_size);
Topic archived. No new replies allowed.