Sunteți pe pagina 1din 2

ALGORITHMS AND DATA STRUCTURES IN C

Exercises 4 :

4.1 Modify the print_hello function of Lecture 4a to have an additional character parameter. The hello's should be boxed in by the character parameter supplied. For example, if the number of hellos required is two and the "box" character is '/', the output should look something like this :////////////////////// / Hello World / / Hello World / //////////////////////

4.2 Convert the program which prints a triangle of asterisks of Exercises 3, into a local function with n as the parameter. The main method should read n and call the local triangle drawing function. 4.3 Write a function called symm_triangle which draws a symmetrical triangle indented m spaces . eg n=3, m=20.

* *** ***** Hence or otherwise draw a "Fir Tree" from main() , as shown below. * *** * *** ***** ******* * *** ***** ******** *********** * *** ****** ********* *********** ************* ***************

4.4 Use comp_swap of Lecture 4b, to sort four integers a,b,c and d. What is the general number of comparisons required to sort N integers ?

4.5 Implement each of the following functions a) d) and write a testbed program to call each function. The testbed should; accept an integer to select a function, read the values required to test the function, then call the function which calculates the result, returning it to main() - where it is printed. This selection process is performed repeatedly until 0 for quit is input. Any invalid option number should receive an appropriate error message. i) The testbed should be implemented first and should initially output function x not yet implemented for each valid selection x in the range1-4. ii) Add the functions one at a time to the testbed. iii) All input and output must occur in the testbed i.e. in main(). a) A function mult(x,y) which returns the value of x*y where x and y are integers. b) A function find_abs(x) which returns the absolute value of the double precision floating point argument x. eg find_abs(3.0) produces 3.0, find-abs(-3.0) produces 3.0 c) A function fact(x) which returns x! ( x factorial ) where x is an integer. d) A function power(x,y) which returns x to the power of y, where x is a floating point and y is a positive integer e.g. power(3.0,4) gives 81.0 i.e. 3.04. Hint. Use repeated multiplication. Also., add a function to print a menu for the selections and incorporate this in a while with read-ahead structure in your program.

NB Update your portfolio of C programs as appropriate.

S-ar putea să vă placă și