Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,095 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,624 people online right now. Registration is fast and FREE... Join Now!




Having trouble in guass elimination for turbo c..

 
Reply to this topicStart new topic

Having trouble in guass elimination for turbo c.., guass-jordan elimination

nebross000
2 Oct, 2007 - 08:41 PM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2007
Posts: 3


My Contributions
hi guys im a beginner here, can someone help me out.. i need a guide in
solving guass-jordan in using arrays and pointers only..

1. the program must have at least four functions aside from the main(): input function, function that forms the matrix, function that performs the elimination process, and function that computes for the values of the variables.
2. the program must display the original matrix and the matrices generated at different stages of the elimination process. it must display also the resulting values of the variables used in the linear equations.
3. do not declare global variables.
4. function parameters should be limited to arrays and pointers only.
i really need your help and i would appreciate it very much..thank you very much!!!
please reply!!!


here is a program in C i made. but it only runs in 2 inputted values.. can somone give me some pointers... sorry for the messy formatting im new in programming...

#include<stdio.h>
#include<conio.h>

main()
{
float m[100][100], n;
clrscr();
input(&n, m);
eliminate(&n, m);
substitution(&n, m);
getch();
}

input(int *a, float b[100][100])
{
int i, j;
printf("Input number of equation:");
scanf("%d", a);
printf("\n\Enter variable and costant:\n");
for(i=0;i<*a;i++){
printf("Equation #%d\n",i+1);
for(j=0;j<*a;j++){
if (j==*a) printf("constant:");
else printf("variable #%d:", j+1);
scanf("%f", &b[i][j]);
}printf("\n");}
printf("Input letters of variable:\n");
for(i=0;i<*a;i++){
printf("Variable #%d:",i+1);
}
printf("\n\n\n GAUSS ORIGIAL MATRIX\n\n");
for(i=0;i<*a;i++){
for(j=0;j<*a;j++){
printf("%10.5f ", b[i][j]);
} printf("\n");
}
}



eliminate(int *k, float d[100][100])
{
int i, j, l,h;
printf("\n\n\n Elimination");
for(l=0;l<*k;l++){
printf("\n\n");
for(i=1;i<*k;i++)
{h=d[l+i] [l]/ d[l][l];
for(j=0;j<*k;j++)
{d[l+i] [j]= d[l+i] [j]-h*d[l][j];
}
}
for(i=0;i<*k;i++)
{for(j=0;j<*k;j++)
{printf("%10.5f ",d[i][j]);
}printf("\n");
}
}
}


substitution(int *s, float e[100][100])
{
int i, j;
float r[100], x;
char c[100];
for(i=*s-1;i>=0;i--)
{for(j=i+1,x=0;j<*s;j++)
{
x=x+e[i][j]*r[i];
}
r[i]=(e[i][*s]-x)/e[i][i];
}
printf("\n\n\n VALUE OF VARIABLES");
for(i=0;i<*s;i++)
{if(i==*s-1) printf("%c.", c[i]);
else printf("%c,",c[i]);
}
for(i=0;i<*s;i++)
{printf("\n");
printf("%c=%10.5f", c[i], r[i]);
}
}


This post has been edited by nebross000: 2 Oct, 2007 - 09:40 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Having Trouble In Guass Elimination For Turbo C..
2 Oct, 2007 - 09:32 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
please post your code using code tags (highlight then hit the # button above the reply text box).

please clarify your question. what do you mean by "it only runs in 2 values"?

-jjh
User is offlineProfile CardPM
+Quote Post

nebross000
RE: Having Trouble In Guass Elimination For Turbo C..
2 Oct, 2007 - 09:42 PM
Post #3

New D.I.C Head
*

Joined: 2 Oct, 2007
Posts: 3


My Contributions
QUOTE(jjhaag @ 2 Oct, 2007 - 10:32 PM) *

please post your code using code tags (highlight then hit the # button above the reply text box).

please clarify your question. what do you mean by "it only runs in 2 values"?

-jjh


aww.. sorry, im new here... My question is that it only runs properly when the inputted value is only 2 values?.. can you help me
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Having Trouble In Guass Elimination For Turbo C..
2 Oct, 2007 - 10:21 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
a few things...

you need to use floating point numbers for variables that you're going to be using in floating point operations. specifically, your h variable in eliminate() needs to be floating point, because you use it later in a couple of steps. if you try to assign a floating point value to an int, the decimals will get truncated, giving you incorrect results (at least in this case - there may be scenarios in which this truncation is desirable).

you assign x=0 within the control structure of a for-loop...move it either into or outside of the loop, depending on where you actually want it. let the control structures be just that; don't make the code any more obfuscated than is absolutely necessary.

i believe that you are reading past the boundaries of the valid data in all of your functions, specifically anywhere that has the form:

h=d[l+i] [l]/ d[l][l]

or something similar. since your ranges for l and i are 0...k-1, this means that you are accessing row 2*k-2, which is outside of the limits of your input arrays. this will result in divide by zeros at several points, which is why you are seeing indeterminate numbers in your output array for n>2.

you are also passing float* for your first arguments to all of the functions, when they've been defined as accepting int*.

and the code doesn't work for 2-variable systems...the output is always {0,0}, regardless of the input. i have a feeling that correcting your array boundaries may help fix this problem.

you should also be specifying a return value for main, and using int main().

and finally, you need to comment your code. i am familiar with gauss-jordan elimination, so some of it makes sense, but you need to explain, at the very least, the steps take in each of your functions.

if you continue to experience problems with the output, search of go back through this forum - a number of recent threads have covered gj elimination.

-jjh

This post has been edited by jjhaag: 2 Oct, 2007 - 10:21 PM
User is offlineProfile CardPM
+Quote Post

nebross000
RE: Having Trouble In Guass Elimination For Turbo C..
2 Oct, 2007 - 10:34 PM
Post #5

New D.I.C Head
*

Joined: 2 Oct, 2007
Posts: 3


My Contributions

aww.. ok, thanks for the help.. smile.gif ill find some references..
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Having Trouble In Guass Elimination For Turbo C..
2 Oct, 2007 - 10:52 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
no worries...as for references, i'd take a look at numerical recipes, which is available online (in pdf) here. it's a great reference for numerical computation, although the version available is fairly outdated.

-jjh
User is offlineProfile CardPM
+Quote Post

vijaysnkar
RE: Having Trouble In Guass Elimination For Turbo C..
3 Oct, 2007 - 02:28 AM
Post #7

D.I.C Head
**

Joined: 11 Aug, 2007
Posts: 55


My Contributions
Please post your code within the CODE tag. Before posting the query do read the rules of the forum.


QUOTE(nebross000 @ 2 Oct, 2007 - 09:41 PM) *

hi guys im a beginner here, can someone help me out.. i need a guide in
solving guass-jordan in using arrays and pointers only..

1. the program must have at least four functions aside from the main(): input function, function that forms the matrix, function that performs the elimination process, and function that computes for the values of the variables.
2. the program must display the original matrix and the matrices generated at different stages of the elimination process. it must display also the resulting values of the variables used in the linear equations.
3. do not declare global variables.
4. function parameters should be limited to arrays and pointers only.
i really need your help and i would appreciate it very much..thank you very much!!!
please reply!!!


here is a program in C i made. but it only runs in 2 inputted values.. can somone give me some pointers... sorry for the messy formatting im new in programming...

#include<stdio.h>
#include<conio.h>

main()
{
float m[100][100], n;
clrscr();
input(&n, m);
eliminate(&n, m);
substitution(&n, m);
getch();
}

input(int *a, float b[100][100])
{
int i, j;
printf("Input number of equation:");
scanf("%d", a);
printf("\n\Enter variable and costant:\n");
for(i=0;i<*a;i++){
printf("Equation #%d\n",i+1);
for(j=0;j<*a;j++){
if (j==*a) printf("constant:");
else printf("variable #%d:", j+1);
scanf("%f", &b[i][j]);
}printf("\n");}
printf("Input letters of variable:\n");
for(i=0;i<*a;i++){
printf("Variable #%d:",i+1);
}
printf("\n\n\n GAUSS ORIGIAL MATRIX\n\n");
for(i=0;i<*a;i++){
for(j=0;j<*a;j++){
printf("%10.5f ", b[i][j]);
} printf("\n");
}
}



eliminate(int *k, float d[100][100])
{
int i, j, l,h;
printf("\n\n\n Elimination");
for(l=0;l<*k;l++){
printf("\n\n");
for(i=1;i<*k;i++)
{h=d[l+i] [l]/ d[l][l];
for(j=0;j<*k;j++)
{d[l+i] [j]= d[l+i] [j]-h*d[l][j];
}
}
for(i=0;i<*k;i++)
{for(j=0;j<*k;j++)
{printf("%10.5f ",d[i][j]);
}printf("\n");
}
}
}


substitution(int *s, float e[100][100])
{
int i, j;
float r[100], x;
char c[100];
for(i=*s-1;i>=0;i--)
{for(j=i+1,x=0;j<*s;j++)
{
x=x+e[i][j]*r[i];
}
r[i]=(e[i][*s]-x)/e[i][i];
}
printf("\n\n\n VALUE OF VARIABLES");
for(i=0;i<*s;i++)
{if(i==*s-1) printf("%c.", c[i]);
else printf("%c,",c[i]);
}
for(i=0;i<*s;i++)
{printf("\n");
printf("%c=%10.5f", c[i], r[i]);
}
}



User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Having Trouble In Guass Elimination For Turbo C..
3 Oct, 2007 - 02:52 AM
Post #8

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
I think some one already said that.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:30PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month