Home
Friday, November 24, 2017
5:29:06 AM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
   
  1  
Doubles in C
Shikimaru 
28/8/08 1:56:43 PM
Titan

Gday, as you may know i am currently writing a program in C for uni that calculates the x and y co-ordinates of the intercept between two lines on the number plane.

 

#include <stdio.h>

int main (void)
{
double a0, b0, c0, a1, b1, c1, topx, topy, botx, boty, x, y, diffa, diffc, diffy;

a0 = 0.0;
b0 = 0.0;
c0 = 0.0;
a1 = 0.0;
b1 = 0.0;
c1 = 0.0;
topx = 0.0;
topy = 0.0;
botx = 0.0;
boty = 0.0;
x = 0.0;
y = 0.0;
diffa = 0.0;
diffc = 0.0;
diffy = 0.0;

/*
* Stage 1 : Get the Co-efficients a, b and c.
*/

printf("\n\nWelcome to the 2X2 linear equation intercept calculator");
printf("\n\nThis is a program designed to find the x and y co-ordinates of the\nintercept between two equations on the number plane.");
printf("\n\nYou will be asked to enter the co-efficients a, b and c of two equations\nthat are in the form ax + by = c (ie 2x + 5y = 10, a=2, b=5, and c=10)");
printf("\n\n\nPlease enter the co-efficients a, b and c for equation 1 in that order\nseperated by a space: ");

scanf("%lf %lf %lf", &a0, &b0, &c0);

while((a0>-0.0000001) && (a0<0.0000001)){
printf("Variable 'a' not entered correctly, please enter 'a' again now: ");

scanf("%lf", &a0);
}
while((b0>-0.0000001) && (b0<0.0000001)){
printf("Variable 'b' not entered correctly, please enter 'b' again now: ");

scanf("%lf", &b0);
}
while((c0>-0.0000001) && (c0<0.0000001)){
printf("Variable 'c' not entered correctly, please enter 'c' again now: ");

scanf("%lf", &c0);
}
printf("\n\n\nPlease enter the co-efficients a, b and c for equation 2 in that order\nseperated by a space: ");

scanf("%lf %lf %lf", &a1, &b1, &c1);

while((a1>-0.0000001) && (a1<0.0000001)){
printf("Variable 'a' not entered correctly, please enter 'a' again now: ");

scanf("%lf", &a1);
}
while((b1>-0.0000001) && (b1<0.0000001)){
printf("Variable 'b' not entered correctly, please enter 'b' again now: ");

scanf("%lf", &b1);
}
while((c1>-0.0000001) && (c1 < 0.0000001)){
printf("Variable 'c' not entered correctly, please enter 'c' again now: ");

scanf("%lf", &c1);
}

/*
* Calculations : Calculate the top and bottom of the equations to be used
* to differentiate between the three classes of results.
*/


topx=(c0*b1)-(c1*b0);
botx=(a0*b1)-(a1*b0);
topy=(a0*c1)-(a1*c0);
boty=(a0*b1)-(a1*b0);
diffa=(a0/a1);
diffc=(c0/c1);
diffy=(diffa-diffc);

/*
* Differentiate : Sort the results depending on whether they are asking to
* divide by zero. Using that knowledge we can then sort the parrallel lines from
* the same line and from the genuine intercept results.
*/

if((botx<0.0000001) && (botx>-0.0000001)){
if((diffy>-0.0000001) && (diffy<0.0000001)){
printf("\n\n\nThe equations, \n %fx + %fy = %f and \n %fx + %fy = %f are the same line.\nTherefore they share an infinite number of intercepts.", a0, b0, c0, a1, b1, c1);
}else{
printf("\n\n\nThe equations, \n %fx + %fy = %f and \n %fx + %fy = %f \ndo not have an intercept as they are parrallel.", a0, b0, c0, a1, b1, c1);
}
}else if((boty<0.0000001) && (boty>-0.0000001)){
if((diffy>-0.0000001) && (diffy<0.0000001)){
printf("\n\n\nThe equations, \n %fx + %fy = %f and \n %fx + %fy = %f are the same line.\nTherefore they share an infinite number of intercepts.", a0, b0, c0, a1, b1, c1);
}else{
printf("\n\n\nThe equations, \n %fx + %fy = %f and \n %fx + %fy = %f \ndo not have an intercept as they are parrallel.", a0, b0, c0, a1, b1, c1);
}
}else{
x=(topx/botx);
y=(topy/boty);

printf("\n\n\nThe equations,\n %fx + %fy = %f and\n %fx + %fy = %f intercept at co-ordinates \nx = %f y = %f \n(x = %f/%f, y = %f/%f).", a0, b0, c0, a1, b1, c1, x, y, topx, botx, topy, boty);
}

return (0);

}



I have streamlined the calculation process and so far am very happy with it, however i am a little concerned with the output.

I am using double precision floating point real numbers in the program, as the user may want to enter real numbers and the program definitely deals with real numbers during the calculation process.

Now this is fine for calculations and the such, but when it comes to displaying the output, there is a huge mess of zeroes on the end of all the numbers, which makes it very hard to read.

ie
 
The equations,
1.000000x + 2.000000y = 30.000000 and
2.000000x + 5.000000y = 8.000000 intercept at co-ordinates
x = 134.000000 y = -52.000000
(x = 134.000000/1.000000, y = -52.000000/1.000000).

What i really would like to do is shave those extra zeroes off the end of the numbers to make the output more readable.

thanks for your time.

-----
Quote by Catmosphere
Know thyself



Shikimaru 
28/8/08 2:39:51 PM
Titan

Got it sorted, %g does the trick :)

-----
Quote by Catmosphere
Know thyself



freespace 
28/8/08 9:22:36 PM
Hero
Titan


I am really happy to see you solving your own problems, and not expecting other ppl to spoon feed you.

Best yet, you share your solutions :-)

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

pwarren 
30/8/08 9:55:08 AM
Champion

$ man printf

ftw :)

-----
“Money is a powerful aphrodisiac. But flowers work almost as well.”
-- Lazarus Long

Shikimaru 
31/8/08 11:21:10 AM
Titan

I'm not sure, but i think that that is a reference to C++, unfortunately I'm only in C. Thanks for your time anyway :)

-----
Quote by Catmosphere
Know thyself



  1  
Forums | Programming