Problems while finding the distance between two points - c

#include <stdio.h>
int main()
{
int x1;
int x2;
int y1;
int y2;
printf("Enter the value of x1 and y1 %d %d ",x1,y1);
scanf("%d %d",&x1,&y1);
printf("Enter the value of x2 and y2 %d %d ",x2,y2);
scanf("%d %d",&x2,&y2);
int l1=x2-x1;
int l2=y2-y1;
int len1=l1*l1;
int len2=l2*l2;
int length=len1+len2;
int lengthf=sqrt(length);
printf("The length between the points = %d",lengthf);
return 0;
}
I was not expecting the locations of the variables. I have to get the distance between two points.

There are several faults in the code:
x1, y1, x2, and y2 are used uninitialized that invokes UB.
You are losing precision to the actual result by casting a double to an int.
You are missing math.h library.
You don't check for the scanf() values.
Lots of redundant variables.
The code can be rather written this way. Follow the comments to understand.
#include <math.h>
#include <stdio.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
// Storing the coordinate values in a structure.
struct point {
int x;
int y;
};
int main(void)
{
struct point p1 = { 0, 0 };
struct point p2 = { 0, 0 };
fprintf(stdout, "Enter the value of x1 and y1: ");
// Validation of input -- considered good practices.
if (fscanf(stdin, "%d %d", &p1.x, &p1.y) != 2) {
fprintf(stderr, "error: The value(s) are invalid.\n");
return EXIT_FAILURE;
}
fprintf(stdout, "Enter the value of x2 and y2: ");
// Validation of input -- considered good practices.
if (fscanf(stdin, "%d %d", &p2.x, &p2.y) != 2) {
fprintf(stderr, "error: The value(s) are invalid.\n");
return EXIT_FAILURE;
}
// Finally, evaluation of the distance using pow() and sqrt().
double distance = sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
fprintf(stdout, "Distance between two points: %.2lf\n", distance);
return EXIT_SUCCESS;
}

Related

Why is this code not producing an output?

Im writing a simple program to show the distance/time between two converging trains. I wanted to test the program and return output value through the function float converge, and then apply that to the main function, but it seems that the converge function does not work.
#include <stdio.h>
float converge(int x, int y, int z) {
return x / (y + z);
}
int main(void) {
int dist, speed1, speed2;
printf("Enter distance in miles");
scanf("%d\n", &dist);
printf("Enter train 1 speed and train 2 speed in mph");
scanf("%d%d\n", &speed1, &speed2);
converge(dist, speed1, speed2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
float converge (float x, float y, float z)
{
int time=x/(y+z);
return time;
}
int main ()
{
float dist, speed1, speed2;
printf("Enter distance in miles:\t");
scanf("%f", &dist);
printf("Enter speed of first train in mph:\t");
scanf("%f", &speed1);
printf("Enter speed of second train in mph:\t");
scanf("%f", &speed2);
printf("Time between this two trains is %f",converge(dist, speed1, speed2));
}
There are multiple problems in your code:
converge performs integer arithmetic and converts the result to float only for the return value. If you want to compute a fractional number, you should change it to: double converge(int x, int y, int z) { return (double)x / ((double)y + z); } or better use double for the input values and the argument types:
double converge(double x, double y, double z) { return x / (y + z); }
There are trailing newlines in the scanf() conversion formats: this will cause scanf() to consume any trailing white space typed after the numbers, including any number of newlines typed at the prompts. You will not get the second prompt as long as you enter empty lines. Remove these \n from the format strings.
The result of the computation is not printed.
Here is a modified version:
#include <stdio.h>
double converge(double x, double y, double z) {
return x / (y + z);
}
int main(void) {
double dist = 0, speed1 = 0, speed2 = 0;
printf("Enter distance in miles: ");
scanf("%lf", &dist);
printf("Enter train 1 speed and train 2 speeds in mph: ");
scanf("%lf%lf", &speed1, &speed2);
if (speed1 + speed2 <= 0)
printf("No collision\n");
else
printf("Time until collision: %f seconds\", 3600 * converge(dist, speed1, speed2));
return 0;
}
Why is this code not producing an output?
It produces no output for the expected output from the result of converge() because there is no statement in the provided code, which could cause this output.
You need for example one printf() statement after the call to converge() in order to print the result of converge():
#include <stdio.h>
float converge (int x, int y, int z)
{
return x/(y+z);
}
int main (void)
{
int dist, speed1, speed2;
float converge_result;
printf("Enter the distance between the two trains in miles:\n");
scanf("%d", &dist);
printf("\n");
printf("Enter the speed of train 1 and the speed of train 2 in mph:\n");
scanf("%d %d", &speed1,&speed2);
printf("\n");
converge_result = converge(dist, speed1, speed2);
printf("The time until the two trains encounter each other is:\n %f",converge_result);
return 0;
}
or alternatively:
#include <stdio.h>
float converge (int x, int y, int z)
{
return x/(y+z);
}
int main (void)
{
int dist, speed1, speed2;
printf("Enter the distance between the two trains in miles:\n");
scanf("%d", &dist);
printf("\n");
printf("Enter the speed of train 1 and the speed of train 2 in mph:\n");
scanf("%d %d", &speed1,&speed2);
printf("\n");
printf("The time until the two trains encounter each other is: \n%f ",
converge(dist,speed1,speed2);
return 0;
}
By the way, the calculation of the distance in time seems incorrect or at least incomplete.

Using pow() to power tow variables

I have 2 variables and after i gave those number to computer it power them
like:
And I want to do this: a^b. Then print it:
int a ;
int b ;
scanf ("%d" , &a);
scanf ("%d" , &b);
man pow says:
double
pow(double x, double y);
...
The pow() functions compute x raised to the power y.
You need to include math.h.
In code it would look like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int a, b;
if(scanf("%d", &a) != 1) {
fprintf(stderr, "wrong input for a");
exit(1);
}
if(scanf("%d", &b) != 1) {
fprintf(stderr, "wrong input for b");
exit(1);
}
double result = pow(a, b);
printf("result of %d^%d=%g\n", a, b, result);
return 0;
}
Please note that scanf returns the number of input items assigned. So it makes sense to check for invalid input there.

Can't find the reason why value stored to 'delta' isn't read

While analyzing can't seem to avoid the value stored to 'delta' not being read... what part of my loop isn't working and why?
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float delta;
printf("a=");
scanf("%f", &a);
printf("b=");
scanf("%f", &b);
printf("c=");
scanf("%f", &c);
float x, x1, x2;
delta=((b*b)-(4*a*c));
if("delta==0")
{
x=((-b)/(2*a));
printf("x=%f \n", x);
}
else if("delta>0")
{
x1=((-b+sqrt(delta))/(2*a));
x2=((-b-sqrt(delta))/(2*a));
printf("x1=%f i x2=%f \n", x1, x2);
}
else printf("Nie ma w zbiorze liczb rzeczywistych rozwiazania tego rownania.\n");
return 0;
}
1st thing, if("delta==0") inside "" whatever is present that will be treated as a address so if(address) means true, remove the double quotation.
if(delta==0)
// some code
else if (delta > 0)
// some code
2nd thing, you are comparing(==) a float & an integer, so be aware of behaviour of comparing different operands.

Quadratic Equation not working properly

I am new to both this site and C programming and I am attempting to make a quadratic formula but I cannot get the roots to work. I believe I am not calling a function or perhaps there is something else wrong. Any help would be appreciated, thank you!
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
float root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is |n%fx^2%+fx%+f=0", a, b, c);
return 0;
}
float root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f,x1 , x2");
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f,x");
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2f,x3i and");
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2f,x4i");
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
You never call the function root() so it will never be executed. Put the function call somewhere before return 0; of main():
root(a, b, c);
Also, fix the printf() calls as mentioned above.
I'm not sure if this will fix everything but in your code you have your print statements formatted incorrectly. For example, this line:
printf("%.2f and %.2f,x1 , x2");
Should be printf("%.2f and %.2f", x1, x2);
The variables whose values you are trying to use should be outside of the quotation marks. Hopefully this helps.
You have several errors.
You never call root().
root is declared to return float, but it doesn't return anything, it just prints the roots. It should be declared void.
You have mistakes in many of your printf() calls. When you're displaying the equation, you have |n instead of \n, and %+f instead of +%f. When you're displaying the roots, you have the variables that you want to print inside the format string, instead of as separate arguments to printf.
Here's the corrected code.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
void root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is \n%fx^2+%fx+%f=0\n", a, b, c);
root(a, b, c);
return 0;
}
void root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f" ,x1 , x2);
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f",x);
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2fi and ", x3);
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2fi", x4);
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
DEMO

Calculate distance between 2 coordinates with function in c programing (x,y,z)

I'm trying to calculate the distance between 2 coordinates in 3D, however, when complied, and entered (1,1,1), (1,1,1), output returns 2 instead of 0.
Code here
#include <stdio.h>
#include <math.h>
int get_dist(int x_i,int y_i,int z_i,int x_o,int y_o,int z_o){
int coord_dist = 0;
coord_dist = sqrt(((x_o - x_i)^2) + ((y_o - y_i)^2) + ((z_o - z_i)^2));
return coord_dist;
}
int main(void){
int x0,y0,z0,x1,y1,z1;
int dist0_1 = 0;
printf("coord 0:");
scanf("%d %d %d", &x0, &y0, &z0);
printf("coord 1:");
scanf("%d %d %d", &x1, &y1, &z1);
dist0_1 = get_dist(x0,y0,z0,x1,y1,z1);
printf("%d\n", dist0_1);
return 0;
}
^ is XOR, not exponent. You want the pow() function.

Resources