I've been trying to figure out whats wrong with my project. Its about multiimensional arrays.
I am trying to input data from a file to a 2d array. Here is my code below;
#include <iostream>
#include <fstream>
using namespace std;
const int I=3;
const int J=3;
//will input 2 matrices from files
//void InputMatrix(int matrix[][J], int matrix2[][J]);
//Will print first matrix
void PrintMatrix(int matrix[][J]);
// Will print second matrix
void PrintMatrix2(int matrix2[][J]);
//Will print the transpose of matrices
void PrintTranspose(int matrix[][J], int matrix2[][J]);
//Will add 2 matrices
void AddMatrice(int matrix[][J], int matrix2[][J]);
//Will scalar-multiply
void Multiply(int matrix[][J], int matrix2[][J]);
//Will get diagonal
void GetDiagonal(int matrix[][J]);
int GetChoice();
int main(){
int matrix;
int matrix2;
int result = GetChoice();
while (result != 0){
switch(result){
case 1: //InputMatrix(matrix, matrix2);
break;
case 2: PrintMatrix(matrix);
break;
case 3: PrintMatrix2(matrix2);
break;
case 4: PrintTranspose(matrix, matrix2);
break;
case 5: AddMatrice(matrix, matrix2);
break;
case 6: //void Multiply(int matrix[I][J], int matrix2[I][J], int s); (Omitted)
break;
case 7: GetDiagonal(matrix);
break;
default:cout << "INVALID CHOICE, Choose again" << endl;
}
result = GetChoice();
}
return 0;
}
/*void InputMatrix(int matrix[][J], int matrix2[][J])
{
ifstream inFile("data1.txt"); //read from data1.txt in the same directory of the project
//ifstream inFile2("data2.txt"); //read from second data file
int a=0, b=0;
for(a=0; a < I; a++)
for(b=0; b < J; b++) //loop for filling the array. I = row, J= col variables
inFile >> matrix[a][b];
}
*/
void PrintMatrix(int matrix[][J]) // function for printing the first matrix
{
int a=0, b=0;
for (a=0; a < I; a++){
for(b=0; b < J; b++){
cout << matrix[a][b] << " ";
cout << endl;
}
}
}
void PrintMatrix2(int matrix2[I][J]) // function for pringing the second matrix
{
int c=0, d=0;
for (c=0; c < I; c++){
for(d=0; d < J; d++){
cout << matrix2[c][d] << endl;
}
}
}
void PrintTranspose(int matrix[I][J], int matrix2[I][J]){ //Transpose (errors fixed)
int r=0, c=0;
for (r = 0; r < I; r++) {
for (c = 0; c < J; c++) {
int temp = matrix[r][c];
matrix[r][c] = matrix[c][r];
matrix[c][r] = temp;
}
matrix++;
}
}
void AddMatrice(int matrix[I][J], int matrix2[I][J]){ // Adding 2 Matrices
int add=0;
int row=0, col=0;
for ( row=0; row < I; row++){
for ( col=0; col < J; col++){
if (row==0 || col==0)
add += matrix[row][col];
}
cout << add << endl;
}
}
//void Multiply();
//planning to omit
void GetDiagonal(int matrix[I][J]){ //Diagonal of matrix
matrix[I][J];
int a=0, b=0;
for(a=0; a < I; a++){
for(a=0; a < J ;a++)
if(a>=b)
cout << matrix[a][b]<<"\t";
else cout<<"\t";
cout<<"\n";
}
}
int GetChoice(){
int result;
cout << "Please enter your choice " << endl;
cout << "1. Input Matrix" << endl;
cout << "2. Print 1st Matrix" << endl;
cout << "3. Print 2nd Matrix" << endl;
cout << "4. Print Transpose" << endl;
cout << "5. Add Matrices" <<endl;
cout << "6. Multipy" << endl;
cout << "7. Get Diagonal" << endl;
cout << "0. Quit "<< endl;
cin >> result;
return result;
}
Unfortunately it prints a one dimensional array
i.e.
1
2
3
4
5
6
7
8
9
and giberrish after
instead of
123
456
789
I don't know what I am doing wrong, could you please help me out finding whats wrong with it?
Thanks in advance.