I want to perform Gaussian Elimination on a matrix which is read in as a text file. The format of the text file is as follows:
1 3 2 5
0 2 4 4
3 0 9 1

where the first line is the dimension n of the matrix, the subsequent lines contain n + 1 entries each, n for coefficients in the matrix, the remaining entry is the right hand side of the system.
Basically what I want to do is put the matrix in an array and the right hand side vector in a seperate array but ive had problems seperating the content from the text file. Here is the code I've managed to create so far.

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()

    cout << "Enter file name - " ;
    char filename[50];
    ifstream file;
    cin.getline(filename, 50);

        cout << "file is not valid" << endl;

    int n;
    int matrix;
    int i;

    //Matrix has to be of size 10 or less
    file >> n;
    if (n > 10){
        cout << "error";
        file >> matrix;
        cout << matrix << " ";
        file >> matrix;


    return 0;


I've used ints instead of arrays just so I have something which runs, what the program is doing is assigning the variable n the number in the first line, but then I want it to assign the matrix portion of the file to a matrix array(which is seperated by spaces and lines) and the right hand side vector to a vector array(which I need to create yet), any help would be appreciated.

Edited by deceptikon: Moved to C++

4 Years
Discussion Span
Last Post by phorce

I don't really understand what you are trying to do, or, the question here.

You need to store the values in an array /matrix/ You can read the values inside a 1D arrayint* matrix = new int[R*H] which can then act as a 2D array. But where do you get the value of '10' from the dataset that you provide?

Edited by phorce

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.