Help
This is my code so far
#include <iostream> #include <ctime> #include <iomanip> #include<cmath> using namespace std; void dividerow(double m[100][100], int n, int r, double val) { int i; for (i = 0; i < n+1; i++) { if(val==0){m[r][i]=false;} else m[r][i] *= (1/val); } } void dividerow1(double m[100][100], int n, int r, double val){ for(int i =0; i<n+1; i++){ if(val==0){m[r][i]=false;} else m[r][i] *= (1/val); } } void dividerow2(double m[100][100], int n, int r, double val){ for(int i =0; i<n+1; i++){ if(val==0){m[r][i]=false;} else m[r][i] *= (1/val); } } void dividerow3(double m[100][100], int n, int r, double val){ for(int i =0; i<n+1; i++){ if(val==0){m[r][i]=false;} else m[r][i] *= (1/val); } } void dividerow4(double m[100][100], int n, int r, double val){ for(int i =0; i<n+1; i++){ if(val==0){m[r][i]=false;} else m[r][i] *= (1/val); } } int main(){ while(1){ 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()%20;} } for (int r=0;r<n;r++){for (int c=0;c<n+1;c++){ cout<<m[r][c]<<"\t";}cout<<endl;} for (int r=n;r>=0;r--){for (int c=n+1;c>=0;c--){ dividerow(m, n, r, m[r][c]);}} //multadd(m, n, m[0][c], m[1][c]); for (int r= 1; r<n; r++){ for(int i =0; i<n+1; i++){ // subtract the rows m[r][i] -= m[0][i];}} for (int c=n+1;c>0;c--){for (int r=n;r>0;r--){ dividerow1(m, n, r ,m[r][c]);}} for (int r= 2; r<n; r++){ for(int i =1; i<n+1; i++){ m[r][i] -= m[1][i];}} for (int c=n+1;c>1;c--){for (int r=n;r>1;r--){ dividerow2(m, n, r, m[r][c]);}} for (int r= 3; r<n; r++){ for(int i =2; i<n+1; i++){ m[r][i] -= m[2][i];}} for (int c=n+1;c>2;c--){for (int r=n;r>2;r--){ dividerow2(m, n, r, m[r][c]);}} for (int r= 4; r<n; r++){ for(int i =3; i<n+1; i++){ m[r][i] -= m[3][i];}} for (int c=n+1;c>3;c--){for (int r=n;r>3;r--){ dividerow3(m, n, r, m[r][c]);}} for (int r= 5; r<n; r++){ for(int i =4; i<n+1; i++){ m[r][i] -= m[4][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; }
the out put
Enter a number: 3 2 8 15 1 10 5 19 19 3 5 6 6 1 4 7.5 0.5 -0 1 1.6 -0.4 -0 0 1 -0.321 Press any key to continue . . .
im doing linear algebra matrix. and on the second metrix i want to turn 4, 7.5, 1.6 in to zeros without effecting the ones for more info http://www.math.odu.edu/~bogacki/cgi-bin/lat.cgi?cmd=sys;m1_1=4;m1_2=54;m1_3=64;m1_4=4;m2_1=98;m2_2=49;m2_3=4;m2_4=46;m3_1=26;m3_2=4;m3_3=78;m3_4=12;curn=3;curn=4;vdat=3;vdat=R3;whid=V;whid=1;submit21=Submit&hideops=0
i used this to turn them into zeros but i ran into other problems.. please help this hw was due like a week ago for (int r=0; r<n;r++){ for(int i =0; i<n; i++){ m[0][i]-=(m[1][i]*m[r][1]); }} for (int r=0; r<n;r++){ for(int i =0; i<n+1; i++){ m[1][i]-= (m[2][i]*m[r][2]); }} for (int r=0; r<n;r++){ for(int i =0; i<n+1; i++){ m[0][i]-= (m[2][i]*m[r][2]); }}
help @wio
So you have divide row and subtract row, but need the logic to return the rows to 0s?
yes but whenever i try to turn them in to zeros this is wat i get Enter a number: 3 2 8 15 1 10 5 19 19 3 5 6 6 1 -1 0 0.5 0 1 -1 1.81 -0 0 1 -0.321 Press any key to continue . . .
i somehow get -1 for first column, second row and second column 3rd row
Here is my guess. Try this on every row. ``` void zero_cell(double m[10][10], int r, int c) { int i; for (i = c; i < n+1; i++) { m[r][i] = m[r][c] * m[c][i]; } } void zero_row(double m[10][10], int r) { int i; for (i = r+1; i < n; i++) { if (m[r][c] != 0) { /* now how you should compare doubles, technically speaking */ zero_cell(m, r, c); } } } ```
How did you get -0 ?
its when it was dividing a negative value to it, the compiler just wrote it as -0
Need to change that `c` to an `i` ``` void zero_cell(double m[10][10], int r, int c) { int i; for (i = c; i < n+1; i++) { m[r][i] = m[r][c] * m[c][i]; } } void zero_row(double m[10][10], int r) { int i; for (i = r+1; i < n; i++) { if (m[r][i] != 0) { /* now how you should compare doubles, technically speaking */ zero_cell(m, r, i); } } } ```
Get it to work for one cell, then do it for the whole row, then do that for every column
can you get it to work for one cell?
i get ur code but im a bit confused on how i should combine it to mine
Don't use my code, just try to do something like what I did in your own way.
okay
@wio
i'm still getting similar result as before. this is how i did it for (int r=0; r<n;r++){ for(int c =0; c<n+1; c++){ for (int i = r+1; i < n; i++) { if (m[r][i] != 0) { for(int i =c; i<n+1; i++) m[0][i] -= m[1][c] * m[r][1]; }}}}
that not 0 check should happen before your i loop.
you are making it so confusing by not using functions.
if i do that will that not show a syntax error.
on i
sorry, i'm not used to using functions alot
you need to do the check first, with r and c, not r and i
like this for (int r=0; r<n;r++){ for(int c =0; c<n+1; c++){ if (m[r][c] != 0) { for (int i = r+1; i < n; i++) { for(int i =c; i<n+1; i++) m[0][i] -= m[1][c] * m[r][1]; }}}}
keep trying.
i tried different ways i can think of but, it's still not working @wio
Thanks
for (int r=0; r<n;r++){ for(int c =0; c<n+1; c++){ m[0][c] -= m[1][c] * m[r][1]; if (m[0][1] != 0) { m[0][1] = m[1][c] * m[r][1]; }}} @wio This gives me a zero, but when i plug x,y,z to the original matrix, it doesn't give me the right answer, it is off by a bit
Enter a number: 3 2 8 15 1 10 5 19 19 3 5 6 6 1 0 -0 1.22 0 1 -0 1.81 -0 0 1 -0.321 Press any key to continue . . . when i plug the x,y,z 3*1.22 + 5*1.81 + 6*-.321 = 10.784 i don't get 6 for the outcome
So you know the answer should be 6 and get 10.7?
correct
use that to get the real answer and compare
Do they really expect you to code this without using functions?
What has your class covered so far?
in this class, the teacher is not really teaching anything, he just give us an assignment to code. a lot of students dropped his class, and i can't switch cuz he is the only one teaching that course
We should do this program with a top down approach.
Do you have a syllabus?
for the assignment, yes
I want to see what you can technically use for this assignment.
I know you can do loops, arrays, etc. But functions and structures?
Linear Algebra Programming. Write a C/C++ program that solves a linear system of n equations and n unknowns. Input: can be generated at random, a matrix of, float m[n][n+1] while n is entered from the key board. Output: a list of n values representing the n unknowns solution.
as far as i know, we can use anything we like, as long as the it is in a loop, and the code work and the values are correct
What do you do if the matrix isn't singular?
i think it has to be singular like i try to do at the top
Can you get that site to work?
it works
Join our real-time social learning platform and learn together with your friends!