Hi all, I've hit a bit of a brick wall with a project assignment I'm working on. I understand the underlying principle of what I have to do, because I've sketched out (visualised) the functionality of the program. I am just struggling at the last hurdle.
I have to create a program that calculates the sum, product or subtraction of two entered matrices. The program allows users to:
1) enter two 2x2 matrices
2) then gives the user a choice of
operation (+ - *)
3) then outputs the answer.
I have done parts 1 and 2, its part 3 I'm stuck on.
The point of this exercise is help solidify my understanding of Multi-dimensional arrays and how they can be used within a program, as well as how they store elements in memory. So I've been using static 2D arrays for this program. I've used code from a website (exforsys.com) and it was really helpful for stages 1 and 2. The things I am trying to work through is how to perform arithmetical operations on elements within a data structure... (is this even possible).
This is a hunch I have, do I need to find some way of accessing an element in the array (maybe via its array index) then ask the program to add it to its corresponding element in the other array, then do it in a loop until the full sum is done ??. I totally understand that I will need a third 2d array which will act as an "answer" array. e.g.
Matrix Matrix Matrix
Array 1 Array 2 Array 3 (answer Matrix)
-------- -------- --------
|1,n | |5,n| |6,n|
|n,n | + |n,n| = |n,n|
** Please note that I know there is a whole library in the STL(or I think boost) for matrices, but what I would please ask anyone who reads this post to understand is that I am a student and in my syllabus libraries (STL, Boost, OpenGL) comes later in my course. I was also taught that understanding arrays thoroughly is best foundation for understanding all other data structures(De-ques, Vectors, Link-List).
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 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 "stdafx.h"
#include <iostream>
using namespace std;
int main(array<System::String ^> ^args)
{
// Array Declarations
//--------------------
const int ROW = 2;
const int COLUMN = 2;
int MatrixCalArr1[ROW][COLUMN]; // This is the declarion of the MultiD array
// 20/05/2011: Turns out will need a MD array
// for both matrices that will be calculated and
// for the answer matrix
int MatrixCalArr2[ROW][COLUMN]; // This is the MD array for the second matrix, I'm
// gonna use the const ints not sure if it will work
// is it even good practice ???
// int MatrixAns_Arr[ROW][COLUMN]; // This array is for the eventual answer.....
// Array Initialisations
//-----------------------
int iter1;
int iter2;
cout << "Welcome To The Matrix Calculator" << endl
<< "================================" << endl
<< endl
<< endl
<< endl;
cout << "Please Enter values of your first matrix" << endl
<< "----------------------------------------" << endl
<< "*Please note you can only enter values for 2x2 matrix*" << endl
<< endl;
for(iter1 = 0; iter1 < ROW; iter1++) // This is for the elements that will me placed in
for(iter2 = 0; iter2 < COLUMN; iter2++)
{
cout << "Enter The Value of Row" << iter1+1; // This asks for the value of the rows and colums
cout << ", Column " << iter2+1 << ":__";
cin >> MatrixCalArr1[iter1][iter2]; // This places the elements inside the array.
}
cout << endl;
cout << endl;
cout << "First Matrix" << endl
<< "------------" << endl;
for(iter1 = 0; iter1 < ROW; iter1++)
{
cout << endl;
for(iter2 = 0; iter2 < COLUMN; iter2++)
cout << MatrixCalArr1[iter1][iter2];
}
cout << endl;
cout << endl;
cout << "Please Enter values of your second matrix" << endl
<< "----------------------------------------" << endl;
// Array Initialisation Matrix 2
int iter3;
int iter4;
for(iter3 = 0; iter3 < ROW; iter3++)
for(iter4 = 0; iter4 < COLUMN; iter4++)
{ // This is section of code will now ask for the arrays elements
cout << "Enter The Value of Row" << iter3+1; // This asks for the value of the rows and colums
cout << ", Column " << iter4+1 << ":__";
cin >> MatrixCalArr2[iter3][iter4]; // This places the elements inside the array.
}
/*
This whole section is a repetition of what was done for the first matrix, entering the elements
for the second matrix follows exactly the same principle and code (with ammendments) as the first matrix
*/
cout << endl;
cout << endl;
cout << "Second Matrix" << endl
<< "------------" << endl;
for(iter3 = 0; iter3 < ROW; iter3++)
{
cout << endl;
for(iter4 = 0; iter4 < COLUMN; iter4++)
cout << MatrixCalArr2[iter3][iter4];
}
// cout << MatrixCalArr1[iter1][iter2];
char MatrixCal_Oper = 0;
cout << endl;
cout << endl;
cout << "Enter Operation: + * -" << endl; // This is a user prompt
cin >> MatrixCal_Oper; // This is what actually takes in the user choice of operator
cout << endl;
cout << endl;
cout << "Your Sum is" << endl;
cout <<"-------------" << endl;
/*
To display the matrices like a sum I have to use the same method as
above using the for loops
for(iter3 = 0; iter3 < ROW; iter3++)
{
cout << endl;
for(iter4 = 0; iter4 < COLUMN; iter4++)
cout << MatrixCalArr2[iter3][iter4];
}
This should display on the console, the two
matrices entered and the operator choice, like a sum.
*/
for(iter1 = 0; iter1 < ROW; iter1++)
{
cout << endl;
for(iter2 = 0; iter2 < COLUMN; iter2++)
cout << MatrixCalArr1[iter1][iter2];
}
cout << endl;
cout << endl;
cout << MatrixCal_Oper;
cout << endl;
for(iter3 = 0; iter3 < ROW; iter3++)
{
cout << endl;
for(iter4 = 0; iter4 < COLUMN; iter4++)
cout << MatrixCalArr2[iter3][iter4];
}
cout << endl;
/*
Part 3
------
Gonna see if a switch statement will work for the arthmetic operations of this calculator in the same way
it worked for the four func calculator exercise.
*/
/*
// Just a Test
*/
cout << endl;
cout << endl;
cout << MatrixCalArr2[0][2]+ MatrixCalArr2[0][1] << endl;
//cout << MatrixCalArr2[1][3] << endl;
while(true){}
return 0;
}
|
I've looked online in other forums and I have done a few google searches, I saw something about The Gaussien Elimination but this seemed to apply to solving algebraic equations using matrices.
Another hunch is that I have to use a function of some sort.....
Sorry for the length of this post,
Thanks