Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
One dimensional distance
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. One dimensional distance
The following program requests units and points on a 1D (one dimensional) line and output the distance between the points.
Here is the C code.
#include <stdio.h> int main() { // declarations char units1[256]; double x1,x2; double distance1; // input printf("Enter measurement units.\n"); scanf("%[^\n]s",&units1); printf("You entered units as \"%s\"\n",units1); printf("Enter x1 and x2 in \"%s\".\n",units1); scanf("%lf %lf",&x1,&x2); printf("You entered x1 as %0.3lf and x2 as %0.3lf.\n",x1,x2); // process distance1 = x2 - x1; // output printf("\n"); printf("The 1D distance\n"); printf("from\n"); printf("\t%0.3lf to %0.3lf\n",x1,x2); printf("is\n"); printf("\t%0.3lf %s.\n",distance1,units1); return 0; }
2. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
feet 1.0 3.0
For the above example input, here is the expected output.
Enter measurement units. You entered units as "feet" Enter x1 and x2 in "feet". You entered x1 as 1.000 and x2 as 3.000. The 1D distance from 1.000 to 3.000 is 2.000 feet.
Here is an example input.
inches 3.2 6.8
For the above example input, here is the expected output.
Enter measurement units. You entered units as "inches" Enter x1 and x2 in "inches". You entered x1 as 3.200 and x2 as 6.800. The 1D distance from 3.200 to 6.800 is 3.600 inches.
3. End of page
by RS
admin@ycp.powersoftwo.org