txt to matrix back to matrix.

i got the barebones of my program down. i need to split it into two functions one that prints the amtrix back into my txt file and the other that reads my input from the txt file.

i got the program running and the outputs do what i want them to. but i want to split them up into functions for later work on structural analysis. easier to copy functions than to try and adapt to your new code right. i have just never done functions with arrays.

here is what i got without trying to implement my function.


#include <cstdlib>
#include <iostream> // User input output to the screen
#include <iomanip>
#include <fstream>
using namespace std;
float matrix[100][2];
int row = 10;
int column = 2;

int main()
{

ifstream in_file;
in_file.open("lab01inp.txt");
in_file >> row;

if (in_file.fail())
{
cout << "the file cannot be opened, the program will stop"; //<< ""end"";
system ("pause");
exit(1);
}
else
{
cout<<row;
for(int i=0; i<row; i++)
{
cout << "\n";
for(int j=0; j<column; j++)
{
in_file >> matrix[i][j];
cout << scientific << setprecision (3) <<matrix[i][j] << " ";
}
}
}
in_file.close();

// create an output file

ofstream out_file;
// open/create the file
out_file.open("lab01out.txt");
out_file<< row;
for(int i=0; i<row; i++)
{
out_file<< " \n";
for(int j=0; j<column; j++)
{
out_file << scientific << setprecision(3) << matrix[i][j]<< " ";
}


}

{
out_file.close();
}
//close file
in_file.close();

cout << "\n\n";
system ("pause");
return 0;
}




Next- i tried to implement my print_matrix function.... see new code below.... this one dosnt work it says something about converting float to int in the function.



#include <cstdlib>
#include <iostream> // User input output to the screen
#include <iomanip>
#include <fstream>
using namespace std;
float matrix[100][2];
int row = 10;
int column = 2;

void print_matrix (int matrix[100][2], int row, int column);
int main()
{

ifstream in_file;
in_file.open("lab01inp.txt");
in_file >> row;

if (in_file.fail())
{
cout << "the file cannot be opened, the program will stop"; //<< ""end"";
system ("pause");
exit(1);
}
else
{
cout<<row;
for(int i=0; i<row; i++)
{
cout << "\n";
for(int j=0; j<column; j++)
{
in_file >> matrix[i][j];
cout << scientific << setprecision (3) <<matrix[i][j] << " ";
}
}
}
in_file.close();

// create an output file

print_matrix(matrix, row, column);




cout << "\n\n";
system ("pause");
return 0;
}




void print_matrix (float matrix[100][2], int row, int column)
{
ofstream out_file;
// open/create the file
out_file.open("lab01out.txt");
out_file<< row;
for(int i=0; i<row; i++)
{
out_file<< " \n";
for(int j=0; j<column; j++)
{
out_file << scientific << setprecision(3) << matrix[i][j]<< " ";
}

out_file.close();
}


}


Oopsy: The problem is this line:

void print_matrix (int matrix[100][2], int row, int column);

You defined you function as taking a two-dimentional array of ints instead of floats. It's an easy mistake: I make it all the time!
Topic archived. No new replies allowed.