Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review Lab assignments Homework #3

Similar presentations


Presentation on theme: "Review Lab assignments Homework #3"— Presentation transcript:

1 Review Lab assignments Homework #3
ICE0125 Programming Fundamentals II – C/C++ Lab 3: C Functions Review Lab assignments Homework #3

2 Why Functions? The best way to develop and maintain a large program is to divide it into several smaller program modules each of which is more manageable than the original program. Modules are written as functions in C. Functions are normally invoked in a program by function name (argument)

3 C Library Functions Random Number Generation (“stdlib.h”)
rand function Returns "random" number between 0 and RAND_MAX (at least 32767) i = rand(); srand function Takes an integer seed and jumps to that location in its "random" sequence srand( seed ); srand( time( NULL ) ); //(“time.h”)

4 Fig. | Commonly used math library functions

5 Creating own Functions
Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function prototype Function prototype indicates function will be defined later in the program Placed after header files declaration and before the main function.

6 Function prototype indicates function will be defined later in the program
Call to square function Function definition

7 Iteration and Recursion
A recursive function is a function that calls itself either directly or indirectly. Both Iteration and Recursion are based on control structure; Iteration uses repetition structure Recursion uses a selection structure

8 Example: factorial // recursive // iterative
//INPUT: n is an integer such that n >= 0 int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } // iterative //INPUT: n is an integer such that n >= 0 int fact(int n) { int running_total; for (running_total = 1; n != 0; --n) running_total *= n; return prod; }

9 Lab assignments #1 Write a program that finds out Greatest Common Divisor of two integers. Your program should be able to calculate GCD in two ways; iterative and recursive way. The program should ask the user to select way of calculation. A sample screen is given below: Enter the calculation method: [1] Iterative [2] Recursive 1 Enter 1st number: 42 Enter 2nd number: 56 The GCD is: 14

10 Lab assignments #2 Write a program that helps elementary school students learn multiplication table. Create your own function using rand to produce one digit integers. It should produce questions like: How much is 6 times 7? If the student types correct answer then the program should print VERY GOOD, if wrong then it will print Incorrect, Try Again and let the student try the same problem again repeatedly until he gets it right.

11 See Course Website at 12pm for Homework 3
GOOD LUCK


Download ppt "Review Lab assignments Homework #3"

Similar presentations


Ads by Google