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.

You haven't provided the input file so we can't run it, and while you've given a description of what goes wrong, you haven't explained what the program is SUPPOSED to do. InputMatrix is commented out, so I'm guessing that's where your problem is. I imagine it gave you compile errors since your function declaration is this:

void InputMatrix(int matrix[][J], int matrix2[][J]);

and your function call is this:

InputMatrix(matrix, matrix2);

where matrix1 and matrix2 are declared as integers. You can't pass integers to a function that is expecting arrays. It looks to me like nothing else can be done in your program until that problem is solved. I don't see how you even got the output that you did get. You never read in the array so how can you display it? Thus if it prints gibberish, that makes sense since you've never put anything into the array. You are printing whatever was already in those memory locations, which could be anything.

/*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];

I commented a couple of stuff out to try hard coding the array, etc. Normally they aren't commented out

The program does compile, above code is the one that inputs data from "data1.txt" (which contains numbers from 1 to 9 basically).

Printarray function does print but not multidimensional, like I mentioned. I'll check my functions out like you suggested for errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.