C++ array/matrix in a file -
completely new c++ , have use programming language. , never done programming before.
basically need read matrix stored file it's output can seen when press debug button.
when i've tried doing matrix
1 3 5 2 4 6 5 7 9 when come reading it, comes matrix in line so.... 1 3 5 2 4 6 5 7 9.
so know how it's read matrix, if that's possible?
in future need find determinant of matrix e.t.c. , other sums between multiple matrices.
here have:
#include <iostream> #include <conio.h> #include <cmath> #include <fstream> #include <cstdlib> using namespace std; int main() { char filename[50]; ifstream matrixa; cin.getline(filename, 50); matrixa.open(filename); if (!matrixa. is_open()) { exit (exit_failure); } char word[50]; matrixa >> word; while (matrixa.good()) { cout << word << " "; matrixa >> word; } system("pause"); return 0; }
if have array of 9 numbers, can read them in ifstream object
float elements[9]; ifstream reader(/*your file*/); reader >> elements[0] >> elements[1] >> elements[2]; //read first line reader >> elements[3] >> elements[4] >> elements[5]; //read second line reader >> elements[6] >> elements[7] >> elements[8]; //read third line reader.close(); better still can create class / structure matrix , read in using member function, , add operators (such determinant).
edit:
if want print matrix in... matrix form, use std::getline;
ifstream reader(/*your file*/); char buffer[300]; //1st line std::getline(reader, buffer); cout << buffer << endl; //2nd line std::getline(reader, buffer); cout << buffer << endl; //3nd line std::getline(reader, buffer); cout << buffer << endl; reader.close(); this assume format data matrix.
Comments
Post a Comment