ITP_Exp8
ITP_Exp8
ITP_Exp8
OBJECTIVES
The main purpose of the experiment is to introduce you to C functions. In this experiment,
firstly, random number generation is explained. Lastly, simple function examples are studied.
INFORMATION
In different game, different range of random number is required to implement the application.
For example, in backgammon game, six-sided dice is used and the programmer must generate
integer numbers between 1 and 6 (Fig 2).
E-mail : burakaleci@gmail.com
Introduction To Programming Laboratory, Fall 2020
rand function actually generates pseudorandom numbers. It means that when rand function is
invoked it produces a sequence of numbers that appears to be random. However, the sequence of
number is same for each call of function. In order to generate different sequence of number
standard library function srand must be used. srand function receives an unsigned integer
argument and uses the argument to produce different sequence of number. Another way to use
srand function is utilizing the computer clock. srand(time(NULL)) statement reads computer
clock and uses it to generate different sequence of number [3].
QUESTIONS
1) Write a C program to consider triangular inequality. Prompt the boundaries of the random
number interval. The program must include the triangular function that receives lower and upper
boundaries and returns whether the three random numbers can form a triangular or not. In
triangular function, generate and print three random numbers, return 1, if they can construct a
triangular, otherwise return 0. Print the result in the main function. (Hint: For any triangle, the
sum of the lengths of any two sides must be greater than or equal to the length of the
remaining side.)
2) Write a C program to calculate geometric mean of the random numbers. Input the boundaries
of the random number interval. The program must include the geoMean function that receives
lower and upper boundaries and returns the geometric mean of the random numbers. In geoMean
function, generate and print six random numbers, then calculate the geometric mean of these
numbers and return it. Print the result in the main function.
𝑛 1⁄𝑛
(∏ 𝑎𝑖 ) = 𝑛√𝑎1 𝑎2 ⋯ 𝑎𝑛
𝑖=1
E-mail : burakaleci@gmail.com
Introduction To Programming Laboratory, Fall 2020
3) Write a C program to calculate the dot product of 2D two vectors. Consider we have two 2D
vectors such as A and B. Therefore, each vector has two components such as (a1, a2) and (b1,
b2). The dot product of 2D two vectors is calculated as follows:
2
𝑨. 𝑩 = ∑ 𝐴𝑖 𝐵𝑖 = 𝑎1 𝑏1 + 𝑎2 𝑏2
𝑖=1
Prompt the boundaries of the random number interval. Generate the a1, a2, b1,and b2 and print
the numbers. The program must include the dotProduct function that receives these components
and returns nothing. In the function, calculate the dot product of these numbers and print the
result.
4) Write a C program to calculate the midpoint of a line segment. Consider we have a line
segment with end points such as A and B. Therefore, each end points has two components such
as (a1, a2) and (b1,b2). The midpoint of a line segment is calculated as follows:
𝑎1 + 𝑏1
𝑚1 =
2
𝑎2 + 𝑏2
𝑚2 =
2
The program must include the midPoint function that receives nothing and returns nothing. In the
function, prompt the shift value and scaling factor. Generate the a1, a2, b1, and b2 and print the
numbers. Then, calculate m1 and m2; and print these values.
E-mail : burakaleci@gmail.com