Hello everyone;

I've got a project at hand that I must pass and I have to admit, I am not good at c++. No excuse, though. I've got all day to get this thing done.

Its a project that asks me to;

*1. Input 2 matrices from 2 files!!!!!*

0

i guess your first file should have these 2 lines

"3 3

1 2 3 4 5 6 7 8 9"

for a 3*3 matrix

1st line of the file gives you the number of rows(3) and number of cols(3) and the second line is givng row*col numbers. hence a[0][0] = 1,a[0][1]=2,a[0][2]=3,a[1][0]=4 so on and so forth.

you can read the row and col values into some variables and then the actual values into a 2D array.

0

Thanks alot. How should I fill my array with from that file? So far, I've got;

```
void InputMatrix();
ifstream infile;
inFile.open("data1.txt");
int matrix[row][col];
infile >> matrix;
int i,j;
for(i=0; i < matrix[][]; i++){
infile >> matrix[i][j];
infile.close();
}
```

And it doesn't seem to work. There seems the be a problem with filling array indexes maybe?

Also, I keep getting a lot of errors with ifstream / infile portion but I really don't know another way to do it. This is exactly what I've got in my notes :(

0

you can do something like..

int rows=0;

int cols=0;

infile>>rows;

inflie>>cols;

this will give you the row and cols values. for taking array values use nested for loops

for(loop from 0 to rows)

for(loop from o-cols)

infile>>arr[rowcount][colcount]

endloop

endloop

try this. for removing the errors start with removing all silly errors like different spellings at time of declaration and definition, differnt case etc.

ex:

ifstream infile;

inFile.open("data1.txt");

'infile' and 'inFile' are not same, will throw error.

0

Thanks again, that was really helpful.

I still keep getting very silly errors, like "Syntax Error: for" (c2059). Here is my full work so far;

```
#include <iostream>
#include <fstream>
using namespace std;
const int I=10;
const int J=10;
//will input 2 matrices from files
void InputMatrix();
//Will print the matrices
void PrintMatrix(int matrix[10][10], int s, ostream& os);
//Will print the transpose of matrices
void PrintTranspose(int matrix[10][10], int s, ostream& os);
//Will add 2 matrices
void AddMatrice(int mat1[10][10], int mat2[10][10], int s, int x);
//Will scalar-multiply
void Multiply(int matrix[10][10], int s);
//Will get diagonal
void GetDiagonal(int matrix[10][10], int s);
int GetChoice();
int main(){
int result = GetChoice();
while (result != 0){
switch(result){
case 1: //call InputMatrix;
break;
case 2: //call PrintMatrix;
break;
case 3: //call PrintTranspose;
break;
case 4: //call AddMatrice;
break;
case 5: //call Multiply;
break;
case 6: //call GetDiagonal;
break;
default:cout << "INVALID CHOICE, Choose again" << endl;
}
result = GetChoice();
}
return 0;
}
void InputMatrix();
int matrix[I][J];
ifstream inFile("data1.txt"); //read from data1.txt in the same directory of the project
int a=0, b=0;
for(a=0; a < J; a++){
for(b=0; b < I; b++) //loop for filling the array. I = row, J= col variables
inFile >> matrix[b][a];
}
for(a=0; a < I; a++){
for(b=0; b < J; b++){
inFile >> matrix[a][b];
cout << matrix[a][b];
}
//void PrintMatrix();
/*
void PrintMatrix(int matrix[10][10], int i, int j, ostream& os);
int filenum;
cout << "Which matrix file do you wish to print?" << endl;
cin >> filenum;
*/
int GetChoice(){
int result;
cout << "Please enter your choice " << endl;
cout << "1. Input Matrix" << endl;
cout << "2. Print Matrix" << endl;
cout << "3. Print Transpose" << endl;
cout << "4. Add Matrices" <<endl;
cout << "5. Multipy" << endl;
cout << "6. Get Diagonal" << endl;
cout << "0. Quit "<< endl;
cin >> result;
return result;
}
```

inFile(data1.txt) assumes that the data file is exactly at the same folder with my project file right? I don't know what is going wrong :/

0

couple of things...

1-> for loop needs to be nested. think about it, you are reading values into a 2D array so in the first loop you want to read row 0, all cols, hence the '[row]' index remains constant and you increment only cols. so you get a[0][0].a[0][1],a[0][2]. row remains constant, then once you are done with the first row. increment the 'row' index to point to next row, loop through cols. hope you got that. not the best of explanations i know.

2->when you do the first 2 reads from infile, you will get the row and col values. they should be put in I and J variables and then you should use I and J in the nested loop.

try to understand the working of this nested loop.

```
int matrix[2][3] = { {1,2,3},{4,4,5}};
int rows = 2;
int cols = 3;
for(int k=0;k<rows;k++)
{
for(int l=0;i<cols;l++)
{
cout << matrix[k][l];
}
}
```

0

oops..i'm sorry, you are already doing that... i didnt read it properly :( ... but you dont have to read the file in both the loops. 2nd point still remains valid.

-> you have not opened the file. check ifstream::open and ifstream::is_open, ifstream::close methods.

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

This is the first in (hopefully) a series of posts about vbScript. Please see my post vbScript - The Basics for more details on vbScript.

My wife and I take ...

My plan is to post a series of hopefully useful and informative vbScript Functions and Subs. I started writing up my first code snippet and found ...