Professional Documents
Culture Documents
Lab 2
Lab 2
Lab 2
EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 2: USER INPUT
Introduction:
Arithmetic operators "+" and "-" are used to manipulate pointers by adding or
subtracting the numeric value to or from the pointers without generating any exception
during overflow of the of the pointer's domain. Arithmetic operators can be overloaded
when used with user-defined types to extend the nature of normal operators, thus
providing additional functionalities.
Methodology:
1. Write a program that will prompt the user to input the height, length and width of
a box in centimeters. From the user input the program must calculate and
display box’s volume.
2. Write a program that simulates an odometer. The user inputs the speed (in
kmph) and time the vehicle has traveled (in seconds!!). From the user input,
the program must calculate and display the distance traveled. The program
must also display the speed and time entered by the user.
3. Given by a user are the x- and y- coordinates of two points in a plain: (x 1, y1) and
(x2, y2). Write a C program that will compute and print the distance between
these two points using given below.
2 2
Distance = √ ( x −x ) +( y − y )
1 2 1 2
QUESTION 1
PSEUDOCODE:
1.START
4. End
CODING:
#include<stdio.h>
#include<stdlib.h>
int main ()
scanf("%f",&Height);
scanf("%f",&Length);
scanf("%f",&Width);
TotalVolume=Height*Width*Length;
return 0;
PSEDUCODE:
1. START
2. ENTER THE SPEED, TIME
3. CALCULATE THE DISTANCE
4. END
CODING
#include <stdio.h>
#include <stdlib.h>
int main ()
{
float speed, time,T,distance;
distance=speed/T;
PSEUDOCODE
START
a = pow((x1-x2),2);
b = pow((y1-y2),2);
dis = pow((a+b),0.5);
PRINT DISTANCE
END
CODING:
#include <stdio.h>
#include <math.h>
int main()
int x1,y1,x2,y2;
float a,b,dis;
scanf("%d",&x1);
scanf("%d",&x2);
scanf("%d",&y2);
a = pow((x1-x2),2);
b = pow((y1-y2),2);
dis = pow((a+b),0.5);
printf("Distance: %f",dis);
return 0;
}
Listing 3.1: Program code to calculate distance between the x and y coordinates using
the arithmetic operation in C.
DISCUSSION:
In the question 1, I input the value of the height, length and width in the program using
the function of Scanf and used the function of the Printf to print the result. According to
the instruction to calculate the volume which is height*width*length were declared in the
program later, I used the function of Printf to print or display the output. In the listing 1.1,
full coding of the program to calculate volume is given and once it executed, desired
output achieved in the listing 1.2. In the question 2, to calculate the distance, the float
value of time and speed entered in the program using the scanf function. As, the
instruction asked the speed were given kmph and time were given in the program in
seconds. After establishing the full program, I was able to calculate distance which is
stated in the Listing 2.1 and Listing 2.2. In the question 3, to calculate the distance
between two coordinate x and y at first declared the function respectively a = pow((x1-
x2),2), b = pow((y1-y2),2) and dis = pow((a+b),0.5 to make it easy to solve the
arithmetic function of c program. Scanf function allowed to input value of x1, x2, y1 and
y2. Finally, I used the printf function to print out the distance between two coordinates
which is given in the listing 3.1 and 3.2. In this lab, I was able to perform the arithmetic
operation using the scanf and printf function.
CONCLUSION: