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:
3
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);
    file.open(filename);

    if(!file.is_open()){
        cout << "file is not valid" << endl;
        exit(EXIT_FAILURE);
    }

    int n;
    int matrix;
    int i;

    //Matrix has to be of size 10 or less
    file >> n;
    if (n > 10){
        cout << "error";
        exit(EXIT_FAILURE);
}
    else
        file >> matrix;
        while(file.good()){
        cout << matrix << " ";
        file >> matrix;

    }

system("pause");
    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.

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?

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.