Matrices

Write your question here.
Please i have a submission of a c++ code that add , subtract, multiply, inverse, power , determinant ,divide matrices taking a string input from user exactly like this [10 2 3;5 6 8;9 8 7] (an example of 3x3 matrix) ; basically i will use 2d array to store the string , (atof) command to transform the string elements to float ;the problem here that i can't take the string input as the required form and transform it into 2d array . any ideas ?!
Last edited on
Well, as you said you need to take input in string format from the user; to take input you would need to make use of the getline function. Basically this is how it works:

1
2
3
4
5
6
7
...
string input; // declares a variable to contain the input
getline(cin, input); // function to retrieve the line of input

// code to retrieve numbers from the string
...
...


Also, I think that it would make things far easier if you used a 3-dimensional array instead of a 2D one since you are reading in a 3-dimensional matrix.
Thanks Tom56785 .
But i knew that i must take the string using (getline) function but i can't take the numbers specifically and put it into the 2d array.
And i will use 2d array as 1 for the rows and 1 for the columns .
One method of doing this (which I hate doing!) is to use the string.find() function to search for space and semi-colon characters and then use string.substr() to split the string up into the designated areas.

Or, an alternative method would be to take input the old-fashioned way using cin >> int. You could loop through placing the input into a number variable each time; when invalid input is reached (like a space or semi-colon) then an error flag would be placed onto 'cin'. You could then clear that flag and then take further input for the next number. This could be repeated until the end-of-file is reached.

Hope you followed!
Last edited on
Thanks Tom56785 again
my submission requires a string input , so I can't use cin ; althought I though about using string.substr() and string.find() but I am confused about using them together to get the number specifically from a specific index to another .
Sometimes getting the 'find' and 'substr' functions to work together perfectly is a little bit difficult to get my head around sometimes so I've thought of a better way to solve this problem and tested it. It is similar to inputting using 'cin' but this still gets the entire input using 'getline'.

It involves using stringstreams: it takes input and puts it into an integer until a character that isn't a number is hit. Once that happens, it skips that character and then moves on until it finds another number to extract.

Here's the code I wrote:

1
2
3
4
5
6
7
8
9
10
11
12
13
string inp;
getline(cin, inp); // get the entire input
istringstream ss(inp); // declare a stringstream and give it the input just taken
while (!ss.eof()) {
    int temp; // variable to hold the current number
    ss >> temp; // go through the string until an invalid character is hit     
    if (ss.fail()) { // an invalid character was hit
        ss.clear(); // clear all error flags
        ss.ignore(1); // skip a character in the stream
        continue; // start the loop again and do the next number
    }
    cout << temp << endl; // outputs the number
}


P.S. - You have to include <sstream> to use stringstreams
Last edited on
That really helped me thanks alot .
Topic archived. No new replies allowed.