lehpron
Topic Author
Posts: 6846
Joined: Tue Jul 10, 2001 3:42 am

Someone Debug My C Source Code

Tue Nov 05, 2002 1:36 pm

Our assignment (I admit to procrastinating) is due by midnight, which is in 2.5 hours. I already tried looking for examples online but none was found.

Our assignment was in the C programming language, and we were to find the root of a quadratic function, Ax2 + Bx + C = 0, and thier corresponding slopes. The program is supposed to terminate when 'A', 'B' and 'C' equals zero, but instead it terminates at just 'A' = 0.

This is my code:


#include
#include

float flt_a;
float flt_b;
float flt_c;

int int_x1;
int int_x2;
int int_m1;
int int_m2;
int int_root;

void main ()

{

do

{

printf("For formula reading Ax^2 + Bx + C = 0,n");
printf("n");
printf("Enter first coefficent, A:n");
scanf("%f", & flt_a);
printf("n");
printf("Enter first coefficent, B:n");
scanf("%f", & flt_b);
printf("n");
printf("Enter first coefficent, C:n");
scanf("%f", & flt_c);
printf("n");

if (flt_a != 0.0 || flt_b != 0.0 || flt_c != 0.0)
{
if (flt_b * flt_b - 4 * flt_a * flt_c < 0.0)
{
int_root = 0;
}
else
{
int_root = 2;
int_x1=(-flt_b + sqrt((flt_b * flt_b)- 4*flt_a*flt_c))/(2*flt_a);
int_x2=(-flt_b - sqrt((flt_b * flt_b)- 4*flt_a*flt_c))/(2*flt_a);
int_m1= 2 * flt_a * int_x1 + flt_b;
int_m2= 2 * flt_a * int_x2 + flt_b;
}
}
else
{
if (flt_a == 0)
{
int_root= 1;
int_x1=(-flt_c / flt_b);
int_m1= flt_b;
}
else
{
int_root= 0;
}
}
}

while (flt_a != 0.0 || flt_b != 0.0 || flt_c != 0.0);

printf("n");
printf("n");
printf("Program terminated:n");
printf("n");

}

What am I missing, cuz it complied perfectly?
The meaning of life is curiosity; we were put on this planet to explore opportunities.
 
PPGMD
Posts: 2398
Joined: Sun Sep 30, 2001 5:39 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 1:39 pm

Where are your includes?
At worst, you screw up and die.
 
PPGMD
Posts: 2398
Joined: Sun Sep 30, 2001 5:39 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 1:41 pm

Also just a tip its bad programming mojo/style to declare all your varibles are global varibles.
At worst, you screw up and die.
 
lehpron
Topic Author
Posts: 6846
Joined: Tue Jul 10, 2001 3:42 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 1:47 pm

Sorry, it was supposed to be:

#include (stdio.h)
#include (math.h)

the carrots make the *.h files disappear.

Our teacher couldn't care less about complex roots, we should just let everything equal -999.99. I have not got there yet.
The meaning of life is curiosity; we were put on this planet to explore opportunities.
 
PPGMD
Posts: 2398
Joined: Sun Sep 30, 2001 5:39 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 1:50 pm

Got any test cases that I could run though it.
At worst, you screw up and die.
 
PPGMD
Posts: 2398
Joined: Sun Sep 30, 2001 5:39 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 1:54 pm

First problem:

while (flt_a != 0.0 || flt_b != 0.0 || flt_c != 0.0);

should be:
while (flt_a != 0.0 && flt_b != 0.0 && flt_c != 0.0);

Need to use ands and not ors.

Second problem you have no output until the program exits? That doesn't seem right.
At worst, you screw up and die.
 
PPGMD
Posts: 2398
Joined: Sun Sep 30, 2001 5:39 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 1:56 pm

third problem all you n's should be /n for new line.
At worst, you screw up and die.
 
lehpron
Topic Author
Posts: 6846
Joined: Tue Jul 10, 2001 3:42 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 2:04 pm

Okay I changed the || into &&, now it loops through (like it supposed to) until put all coeff's as zero, then it freezes and doesn't terminate.

What did you mean by "n's should be /n for new line"? Which lines are these in reference to?
The meaning of life is curiosity; we were put on this planet to explore opportunities.
 
lehpron
Topic Author
Posts: 6846
Joined: Tue Jul 10, 2001 3:42 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 2:06 pm

In terms of out put, I'm taking baby steps to make sure this part works, if it does properly, i.e. it'll complie and run, then I'll just throw that stuff in last.
The meaning of life is curiosity; we were put on this planet to explore opportunities.
 
PPGMD
Posts: 2398
Joined: Sun Sep 30, 2001 5:39 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 2:11 pm

These three lines are a good example(there are more example that appear to need to be changed):

printf("For formula reading Ax^2 + Bx + C = 0,n");
printf("n");
printf("Enter first coefficent, A:n");

Which has the output of:
"For formula reading Ax^2 + Bx + C = 0,nnEnter first coefficent, A:n"

They should be if I read your code right:

printf("For formula reading Ax^2 + Bx + C = 0,/n");
printf("/n");
printf("Enter first coefficent, A:/n");

Which could have the output of:
"For formula reading Ax^2 + Bx + C = 0,

Enter first coefficent, A:
"

Sorry I can't help you much more, I do need to goto sleep I have a Digital Logic test in the morning. But I suggest that you print it out and go though it line by line on paper(away from the computer), you will see alot of mistakes that way. Thats the verification phase the Personal Software Process (PSP) which dominates my 4 years in Computer Science.
At worst, you screw up and die.
 
lehpron
Topic Author
Posts: 6846
Joined: Tue Jul 10, 2001 3:42 am

RE: Someone Debug My C Source Code

Tue Nov 05, 2002 2:23 pm

10Q 4 the help, PPGMD, good luck on ur test.
The meaning of life is curiosity; we were put on this planet to explore opportunities.

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Popular Searches On Airliners.net

Top Photos of Last:   24 Hours  •  48 Hours  •  7 Days  •  30 Days  •  180 Days  •  365 Days  •  All Time

Military Aircraft Every type from fighters to helicopters from air forces around the globe

Classic Airliners Props and jets from the good old days

Flight Decks Views from inside the cockpit

Aircraft Cabins Passenger cabin shots showing seat arrangements as well as cargo aircraft interior

Cargo Aircraft Pictures of great freighter aircraft

Government Aircraft Aircraft flying government officials

Helicopters Our large helicopter section. Both military and civil versions

Blimps / Airships Everything from the Goodyear blimp to the Zeppelin

Night Photos Beautiful shots taken while the sun is below the horizon

Accidents Accident, incident and crash related photos

Air to Air Photos taken by airborne photographers of airborne aircraft

Special Paint Schemes Aircraft painted in beautiful and original liveries

Airport Overviews Airport overviews from the air or ground

Tails and Winglets Tail and Winglet closeups with beautiful airline logos