UMBC CS 104
UMBC CMSC 104 * CSEE | 104 | current 104

CMSC 104, Section 105

Functions & rand() problems


1.) Write C Statements that assign random integers to the variable n in the following ranges: a.) 1 <= n <= 2 b.) 1 <= n <= 100 c.) 0 <= n <= 9 d.) 1000 <= n <= 1112 e.) -1 <= n <= 1 f.) -3 <= n <= 11

2.) Write a function called multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and returns 1 (true) if the second is a multiple of the first, and 0 (false) otherwise.

3.) Find the errors in each of the following program segments and explain how to correct them:

a.) float cube(float); /* function prototype */ ........ ........ cube (float number) /* function definition */ { return number * number * number; } b.) int randomNumber = srand(); c.) float y = 123.45677; int x; x=y; printf("%f\n", (float) x); d.) double square(double number) { double number; return number * number; }

4.) Write a function integerPower ( base, exponent) that returns the value of

exponent (base) For example: integerPower( 3, 4) = 3 * 3 * 3 * 3 Assume the exponent is a positive, nonzero integer, and base is an integer. The function integerPower should use a for loop to control the calculation. Do not use any math library functions.