Lecture 3 CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions Review Conditions: if( ) / else switch Loops: for( ) do...while( ) while( )... 1
Examples Display the first 10 Even Numbers > 0; Display the odd numbers between 60 and 70; Display a random number between 1 and 10, as many times as the user requests. First 10 even numbers, > 0 What are we after? The trick is to understand the question and document a proper solution. We know: METHOD 1: we can check for an even number by using modulus 2 and check if the remainder is 0. METHOD 2: or we can figure out that the values we are after are 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 and the pattern is to start from 2 and increment by 2 2
Method 2: simpler! int main() int num; // stores the even number for (num = 2; num <= 20; num+=2) printf("%d ", num); return 0; Display Odd numbers between 60 and 70 What is an odd number? A number when divided by 2 the remainder is 1. or given the question parameters, a number list: 61, 63, 65, 67, 69 3
code the simpler method int main() int num; // stores the odd number for (num = 61; num <= 69; num+=2) printf("%d ", num); return 0; Problem 3: Display a random number between 1 and 10, as many times as the user requests. What is a random number? we can find that someone else figured this one out! the calculation is packaged into a function and shipped with the C compiler tools as part of the STANDARD LIBRARY 4
What is the Standard Library? Standard means that the solution or function is supported by a national and international committee (ANSI / ISO) and is known to work the same way across different compilers. A Library is a term used to refer to the packaged functions and files shipped together. Standard Library Functions If your compiler is following a standard (say ANSI) then we know that there are many (thousands) of functions that are shipped with that compiler tools as part of a standard library. We can make use of any of these functions to save on development time and reliability of our code. (standard functions are often rigorously tested and well documented) 5
So, What is a FUNCTION? Functions allow us to divide a large task into smaller components or modules and conquer each one independently. They are a prime example of software reuse and avoiding re-inventing the wheel As a general rule, any piece of logic that is likely to be repeated is worth placing it in a separate function. Where do functions fit in a program? You have already been using functions! int main ( ) is a function! We can: Use existing functions (standard library) or Make our own! We will begin by understanding how to use existing functions first. 6
Sample Libraries common math library functions (math.h) #include <math.h> you have to include the math header file so you can tell the compiler where to find the definitions of each of the math functions you want to use. sqrt( x ) square root of x fabs( x ) absolute value of x ceil( x ) rounds x up floor( x ) rounds x down pow( x, y ) x raised to the power y Example: #include <math.h>... double x, y;... x = sqrt( 25 ) + 5; y = 10 + pow(x, 3);... // Square root. // x raised to // power of 3. 7
More Libraries: fig 5.6 page 153, 154 <string.h> string processing <time.h> time and date <stdio.h> input / output <stdlib.h> conversions, memory allocations, random numbers, and other utilities Random Number Generation found in the library <stdlib.h> function is: rand( ) rand( ) generates an int between 0 and 32767 sample usage: 1 + (rand () % 6 ) ) generates an integer between 1 and 6 Related functions: srand( time( NULL ) ); uses time.h to generate a unique seed from the clock 8
Back to Problem 3: Display a random number between 1 and 10, as many times as the user requests. Given: we already have the function rand( ) to generate a random number, we need to modify its results to match what we are looking for. We need to build an interactive code with the user to display the random number as many times as the user requests. rand ( ) #include <stdio.h> #include <stdlib.h> #include <time.h> int main() char done = N ; char skip; srand( time( NULL )); do printf( %d, 1 + ( rand() % 10 ) ); printf( \ndo you want another number? ); scanf( %c%c, &done, &skip); while (done == Y done == y ); return 0; 9
rand ( ) #include <stdio.h> #include <stdlib.h> #include <time.h> int main() char done = N ; char skip; srand( time( NULL )); do printf( %d, 1 + ( rand() % 10 ) ); printf( \ndo you want another number? ); scanf( %c%c, &done, &skip); while (done == Y done == y ); return 0; 10