ITP_Exp8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Introduction To Programming Laboratory, Fall 2020

COMPUTER PROGRAMMING LABORATORY


Experiment # 8:
Functions II

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

Random Number Generation

In C Programming, programmer-defined or standard functions could be used to develop game


applications. The chance factor is an important issue in most game such as backgammon,
blackjack and lottery. In order to implement this chance factor in the game application the C
standard Library function rand from the <stdlib.h> could be used. The rand function generates
an integer number between 0 and 32767 (Fig. 1). It is important to note that, each integer
between 0 and 32676 has equal probability of being chosen when rand function is invoked.

Fig. 1 Random Number Generator [1].

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).

Fig. 2 Dice Example [2].


An example C program to simulate rolling a die is given in Fig. 3. roll function is invoked at
line 19. Line 29 shows the rand function. The rand function must be used with the remainder
operator (%) to scale the interval of the generated numbers. rand()%6 generates integers between
0 and 5, and the number 6 is called the scaling factor. Unfortunately, die applications require
integer numbers in the range of 1 to 6. At that point, the generated numbers must shift by adding
1 with 1+rand()%6.

E-mail : burakaleci@gmail.com
Introduction To Programming Laboratory, Fall 2020

Fig. 3 Rolling a Die Program.

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.

[1] http://css-tricks.com/generate-a-random-number/, 2017.


[2] http://en.wikipedia.org/wiki/Dice, 2017.
[3] P. J. Deitel and H. M. Deitel, “C How To Program Fifth Edition”, Prentice Hall, 2007.

E-mail : burakaleci@gmail.com

You might also like