below is a code i have been working on, but i keep run-inning in to trouble
#include <iostream> #include <ctime> #include <iomanip> using namespace std; int main(){ int n; cout << "Enter a number: "; cin>>n; double m[100][100]; for (int r=0;r<n;r++){for (int c=0;c<n+1;c++){ m[r][c]=1+rand()%9;} } for (int r=0;r<n;r++){for (int c=0;c<n+1;c++){ cout<<m[r][c]<<" ";}cout<<endl; for(int p=0;p<n+1;p++){ for (int i=0; i<n+1;i++){ m[r][i]/=m[r][p];}} } cout<<endl; for (int r=0;r<n;r++){for (int c=0;c<n+1;c++){ cout << setprecision(3)<<m[r][c]<<"\t";}cout<<endl; } return 0; }
i can't get this part for(int p=0;p<n+1;p++){ for (int i=0; i<n+1;i++){ m[r][i]/=m[r][p];}} to divide the row by the first column number for ever column
@e.mccormick this is program is the linear program that u have helped me before, and this is wat i came up with, but i get stuck on dividing. Also how do i subtract the one column by another with in a loop i tried doing it but i get stuck. please help
Did you try tossing a little test print in there to see what the m[r][i] and m[r][p] are?
m[r][i] and m[r][p] carries the row and the column, it should divide the rows by the first number of each column, but it doesn't just do that,; it does other things also
Yes, but I am saying if it is not doing what you expect, it is time to see what values are going in and coming out.
when i replace the p with numbers, i kinda get the right values but it only calculates that first row column and the ones before it.. idk if what im saying makes sense
One thing that can help is a test harness. That is, you make a matrix you know what it will do when as it runs through the code. Once you have worked out, perhaps even tested in a matrix solver. Then you can try the other code against it easier because you will know what that should do at that moment. Then looking at what is really happening is easier because the values are known.
@e.mccormick i tried different way, but instead of the first columns being divided, it is the last row being divided, here is the new one.. it would be nice if u tell me what i should change or switch, so i can start doing the other problem for(int p=0;p<n;p++){ for (int i=0; i<n+1;i++){ m[r][i]/=m[p][i];}}
Well, without making a matrix that I know how to work, it is kind of going in blind. I have been looking for a good worked example that would make use of fracrtions, and therefore be useful in this program. Did not have much luck. On this page: http://www.wyzant.com/resources/lessons/math/precalculus/systems_of_equations/matrix_method They start by talking about the process. Same basic thing I went over with you. But about half way down they have a nice system of 3 equations that makes a 3x4 matrix that solves to \(\{x,y,z\} = \{1,-2,1\}\). Because they have steps and a solution, it can be used as a way to make a test matrix in the software and see what happnes.
@wio im trying to divide and multiply matrix like the example e.mccormick showed above, but i keep getting wrong answers, here is the code that i revised for(int i =0; i<n+1;i++){ m[r][i]= ((1/m[r][i]))*m[0][i]; }} can u help
Hmmm
Can you create a new function for it?
Lol, why don't tap stuff and put loops on multiple lines?
Are you doing matrix multiplication?
i can create a new function, i just wanted to fix up the math part first. i am doing linear algebra matrix to find the solutions for x,y and z
What is the problem currently then?
int main(){ int n; cout << "Enter a number: "; cin>>n; double m[100][100]; for (int r=0;r<n;r++){for (int c=0;c<n+1;c++){ m[r][c]=1+rand()%9;} } for (int r=0;r<n;r++){for (int c=0;c<n+1;c++){ cout<<m[r][c]<<" ";}cout<<endl; for(int i =0; i<n+1;i++){ m[r][i]= ((1/m[r][i]))*m[0][i]; }} cout<<endl; for (int r=0;r<n;r++){for (int c=0;c<n+1;c++){ cout << setprecision(3)<<m[r][c]<<"\t";}cout<<endl; } return 0; } this is the current program, im trying to divide the first row by the first row column numbers
Okay make a function for any particular column.
``` void divideColumn(double m[100][100], int n, int col, double val) { int i; for (i = 0; i < n; i++) { m[col][i] /= val; } } ```
Hold on, let me double check.
Try this ``` void divideColumn(double m[100][100], int n, int c, double val) { int i; for (i = 0; i < n; i++) { m[i][c] /= val; } } ```
Then do to the first column by the first number, say ``` divideColumn(m, n, 0, m[0][0]); ``` Using `0` since it is 0 based.
Do you get it?
i got it, the math works, but im suppose to divide the row not the columns, so do u thing it would would work if i do this void dividerow(double m[100][100], int n, int r, double val) { int i; for (i = 0; i < n+1; i++) { m[r][i] /= val; } } instead since im dividing the rows.
Yeah, I think so. But why are you goint up to n+1?
+1 creats the solution for x , y and z, the last row is the solution and once the could is complete it needs to look like this 1 0 0 | 34 0 1 0 | 45 0 0 1 | 32 something like that
Ok
Thank alot for ur help
Don't divide by 0
i won't. i will have an if statement for that
@e.mccormick Thanks
Yah, he is building a matrix solver.
Join our real-time social learning platform and learn together with your friends!