Presentation is loading. Please wait.

Presentation is loading. Please wait.

Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.

Similar presentations


Presentation on theme: "Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance."— Presentation transcript:

1 Write a program step by step

2 Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance

3 Step 2: Specifying the input and output. Input: coordinates of point one (i.e X1, Y1) and coordinates of point two (i.e X2, Y2) Output: distance between the points. For each of the input and output data you need to declare variables in your program.

4 Step3: Solving some instances of the problem on paper. Suppose the coordinates of point one is (X1=6, Y1=1) and coordinates of point two is (X2=2, Y2=4). The formula to compute the straight distance between two points given their coordinates is Distance = sqrt( (X1-X2)^2 + (Y1 – Y2)^2). In this example the distance will be: Distance = sqrt( (6-2)^2 + (1-4)^2) = sqrt(4^2 + 3^2) = sqrt(25) = 5.

5 Step4: Writing the algorithm on paper Declare the input variables X1, Y1, X2, Y2 and output variable distance. Assign some values to the input variable. Compute the distance using the following formula and assign the results in distance. –Distance = sqrt( (X1-X2)^2 + (Y1 – Y2)^2). Print the value of distance on the screen.

6 Step 5: Write the problem on paper #include int main(void){ /* declaring the input and output variables */ double X1, X2, Y1, Y2; double distance; /* assign some values to the input variables */ X1 = 6; Y1 = 1; X2 = 2; Y2 = 4; /*compute the distance */ distance = sqrt((X1 – X2)*(X1 – X2) + (Y1 – Y2)*(Y1 – Y2)); /*print the results */ printf(“The distance is %f \n“, distance); return 0; }

7 Step 7: Type the program Use a text editor to type your program. If you are typing your program in Unix you can use emacs, vim and pico. pico is the simplest one. Type pico myprog.c to start. Type in your program and press ctrl+o to save. To exit press ctrl+x. If you want to make some changes to your program after you exit type pico myprog.c

8 Step 8: compile your program. The next step is to compile your program. Compiling means translating your program from C language to machine language. You need to do this because the system only understands the machine language. Type g++ myprog.c –o myprog to compile your program. g++ is a compiler for C. If your program has syntax errors the compiler will print the appropriate messages on the screen. In such cases go back to your program (by typing pico myprog.c) and fix the problem. If your program is compiled successfully, no error message will be printed and the machine code will be written in the file myprog (with no extension)

9 Step 9: Test your program. To run your program type./myprog in Unix prompt. Test your program for different input values and see if it will produce the correct output.


Download ppt "Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance."

Similar presentations


Ads by Google