# Thread: C Program - Accept rectangular points and calculate distance between

1. ## C Program - Accept rectangular points and calculate distance between

I am writing a c program that accepts two coordinate points from a rectangle, using a function distance, calculates the distance between using the formula distance = sqrt (x2-x1)^2+(y2-y1)^2
I know my beginning code is off base but I am just beginning in code and struggling to find correct codes.

Code:
```#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#pragma warning (disable:4996)
int main()
{
float x1, x2, y1, y2, midpoint, xtotal, ytotal;
printf("Enter the first x coordinate: ");
scanf("%f", &x1);
printf("Enter the first y coordinate: ");
scanf("%f", &y1);
printf("Enter the second x coordinate: ");
scanf("%f", &x2);
printf("Enter the second y coordinate: ");
scanf("%f", &y2);
xtotal = (x2 - x1) * (x2 - x1);
ytotal = (y2 - y1) * (y2 - y1);
midpoint = sqrt(xtotal) + (ytotal);
printf("The distance of the coordinate is %f", midpoint);
return 0;
system("pause");
}```

2. So what is it you are actually having trouble with?

Quzah.

3. > midpoint = sqrt(xtotal) + (ytotal);
How much of this is actually within the sqrt() call?

4. I am supposed to right a seperate function, that is distance. When I did that, it would not recognize the characters that I have under float currently. So, how to write the distance function correctly.

5. It should be both xtotal and ytotal, which is written wrong on second look, my main issue is with correctly writing the solution with the function distance included.

6. 2,2 and 4,4
(4-2)*(4-2) = 4
4 + 4 = 8
distance = square root of 8
distance = 2.8 and some change

Is that right? If you know it's right on paper then, you can test that your program is doing it right with the same input.

Quzah.

7. My solution exits before the answer is displayed

You aren't passing enough to sqrt().

You're returning before you can system("pause"); (which you shouldn't be doing in the first place)

Code:
`#pragma warning (disable:4996)`
What does this ^^ do? It sounds like you're trying to force your compiler to stop showing you warnings, which is a dumb idea in it of itself, because warnings necessarily indicate that you're doing something wrong. I just googled it, and it seems like it has something to do with deprecated functions.

I'm making a wild guess and assuming that you kept getting warned about the non-standard use of system(pause), and instead of fixing it, told the compiler to shut up instead.

9. Originally Posted by memcpy
What does this ^^ do? It sounds like you're trying to force your compiler to stop showing you warnings, which is a dumb idea in it of itself, because warnings necessarily indicate that you're doing something wrong. I just googled it, and it seems like it has something to do with deprecated functions.

I'm making a wild guess and assuming that you kept getting warned about the non-standard use of system(pause), and instead of fixing it, told the compiler to shut up instead.
In this case, the warning suppression is legitimate. Microsoft compilers mark certain standard library functions as "deprecated" (even though they are not) because the standard library implementation was extended to provide versions of these functions that are intended to be safer to use carelessly. However, the better way of suppressing these warnings is to define _SCL_SECURE_NO_WARNINGS through the compiler options.

10. Why we use #pragma?
I can search over net, but i need a simple answer and a refrense. Thank u.

11. The pragma warning is scanf, which our instructor told us to do. What do I need to do differently so the solution doesn't exit before displaying the answer.

12. Ok my solutions displays the answer now, but I am receiving zero as the answer. What is wrong with the equation I have entered?

14. Code:
```#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#pragmawarning (disable:4996)
int main()
{
float x1, x2, y1 ,y2, distance,xtotal, ytotal;
printf("Enter the first x coordinate: ");
scanf("%f", &x1);
printf("Enter the first y coordinate: ");
scanf("%f", &y1);
printf("Enter the second x coordinate: ");
scanf("%f", &x2);
printf("Enter the second y coordinate: ");
scanf("%f", &y2);
xtotal = (x2-x1)*(x2-x1);
ytotal = (y2-y1)*(y2-y1);
distance = sqrt((xtotal)+(ytotal));
printf("The distance of the coordinate is %d", distance);
system ("pause");
return 0;
}
```

15. distance is a float, but your printf is trying to print an integer.