Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Help

OpenStudy (anonymous):

This is my code so far

OpenStudy (anonymous):

#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];}}

OpenStudy (anonymous):

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; }

OpenStudy (anonymous):

the out put

OpenStudy (anonymous):

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 . . .

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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]); }}

OpenStudy (anonymous):

help @wio

OpenStudy (anonymous):

So you have divide row and subtract row, but need the logic to return the rows to 0s?

OpenStudy (anonymous):

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 . . .

OpenStudy (anonymous):

i somehow get -1 for first column, second row and second column 3rd row

OpenStudy (anonymous):

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); } } } ```

OpenStudy (anonymous):

How did you get -0 ?

OpenStudy (anonymous):

its when it was dividing a negative value to it, the compiler just wrote it as -0

OpenStudy (anonymous):

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); } } } ```

OpenStudy (anonymous):

Get it to work for one cell, then do it for the whole row, then do that for every column

OpenStudy (anonymous):

can you get it to work for one cell?

OpenStudy (anonymous):

i get ur code but im a bit confused on how i should combine it to mine

OpenStudy (anonymous):

Don't use my code, just try to do something like what I did in your own way.

OpenStudy (anonymous):

okay

OpenStudy (anonymous):

@wio

OpenStudy (anonymous):

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]; }}}}

OpenStudy (anonymous):

that not 0 check should happen before your i loop.

OpenStudy (anonymous):

you are making it so confusing by not using functions.

OpenStudy (anonymous):

if i do that will that not show a syntax error.

OpenStudy (anonymous):

on i

OpenStudy (anonymous):

sorry, i'm not used to using functions alot

OpenStudy (anonymous):

you need to do the check first, with r and c, not r and i

OpenStudy (anonymous):

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]; }}}}

OpenStudy (anonymous):

keep trying.

OpenStudy (anonymous):

i tried different ways i can think of but, it's still not working @wio

OpenStudy (anonymous):

Thanks

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

So you know the answer should be 6 and get 10.7?

OpenStudy (anonymous):

correct

OpenStudy (anonymous):

http://www.math.ncsu.edu/ma114/tools/row_ops.html

OpenStudy (anonymous):

use that to get the real answer and compare

OpenStudy (anonymous):

Do they really expect you to code this without using functions?

OpenStudy (anonymous):

What has your class covered so far?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

We should do this program with a top down approach.

OpenStudy (anonymous):

Do you have a syllabus?

OpenStudy (anonymous):

for the assignment, yes

OpenStudy (anonymous):

I want to see what you can technically use for this assignment.

OpenStudy (anonymous):

I know you can do loops, arrays, etc. But functions and structures?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

What do you do if the matrix isn't singular?

OpenStudy (anonymous):

i think it has to be singular like i try to do at the top

OpenStudy (anonymous):

Can you get that site to work?

OpenStudy (anonymous):

it works

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!