Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.

Similar presentations


Presentation on theme: "Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions."— Presentation transcript:

1 Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions –Purpose –Function Structure: Prototype / Heading / Body / Function Calls

2 for loops  The for loop is used when the number of repetitions is already known.  The for loop can be convenient since all of the controls for the loop appear at the top of the for statement initialize Test condition commands Change (update)

3 for loop main ( ) { int i; printf (“\nMy 13 times table\n\n”); for ( i = 1; i <= 12; i++) printf(“%d times 13 = %d\n”, i, i * 13); printf (“\n”); } Initialize the variable to be used in the for loop There are 3 parts in the for Statement: (separated by semicolons “;”) Initialization – performed once, when for loops first starts. Test Condition - test to determine if loop is to be continued. Change or Update – evaluated at the end of each loop. Perform action within the loop. (use braces for code block)

4 In Class Exercise #1 Display the output from the following program: (show your rough work) #include main () { int num; printf(“\tn\tn cubed\n”); for (num = 1; num <= 6; num++) printf (“%5d %5d\n”, num, num*num*num); }

5 In Class Exercise #2 Write a program that prints a table with each line giving an integer, its square, and its cube. Ask the user to input the lower and upper limits for the table. (use a for loop)

6 Storing Characters  Another data type that variables can contain is a character.  The C keyword to declare a variable as a character data type is char variable_name;  The character data type actually stores a character as an integer (a numerical code) that represents certain characters. You will learn about data representation in your ICS124 course and the standards to represent characters on different computer systems. (eg. ASCII or EBCDIC)

7 Storing Characters To prove that characters are stored as integers, please view the following C program (available in ~msaul/ipc144/samples): #include main ( ) { char ch; printf (“Please enter a character: ”); scanf (“ %c”, &ch); printf (“The code for %c is %d.\n”, ch, ch); } SAMPLE OUTPUT: Please enter a character: C The code for C is 67. When scanning a character, after double quote, place space before %C” to have system ignore or skip “whitespace” characters such as spaces or tabs. This only has to be done when scanning characters.

8 Storing Characters We can improve our programs by allowing the user to select “y” for yes and “n” for no

9 In Class Exercise #3 Q What is the weakness with the following program? main () { char selection; double amount, delivery = 30.5; printf (“\nEnter the amount of your purchase: ”); scanf (“%lf”, &amount); printf (“\nDo you want item delivered? (Y/N): “); scanf (“ %c”, &selection); if ( selection == ‘Y’) amount = amount + delivery; printf (“\nThe total amount of the purchase is: $%.2lf\n\n”, amount); } A What if the user enters a lowercase “y”? The delivery amount will not be added to the total amount!

10 Character Conversion Functions A common method to prevent problems with case sensitivity is to use a function convert one type of case to another to prevent the confusion and errors associated with mixing up case sensitivity. toupper (variable) –Used to convert lowercase characters to uppercase characters. This function will not affect the character if it is already uppercase. tolower (variable) –Used to convert uppercase characters to lowercase characters. This function will not affect the character if it is already lowercase.

11 Solution to In Class Exercise #3 main () { char selection; double amount delivery = 30.5; printf (“\nEnter the amount of your purchase: ”); scanf (“%lf”, &amount); printf (“\nDo you want item delivered? (Y/N): ”); scanf (“ %c”, &selection); if ( toupper(selection) == ‘Y’) amount = amount + delivery; printf (“\nThe total amount of the purchase is: %.2lf\n\n”, amount); }

12 Advantages of Modularity  Use modules, that were previously created, in current program you are creating (saves time)  Split large program into modules and assign them to several programmers to have programs developed at a fast rate.  It may be easier to modify or replace a module of a program than trying understand and modify a large program without modules

13 Functions  So far, the programming examples have been fairly simple.  Since programs that we develop become larger and larger, it may become more difficult to understand all the elements in one large program  When designing large programs, it would be better to reduce elements of the program into smaller parts or modules.  The process of having programs consisting of separate parts or modules is referred to as modularity

14 Functions  Many computer programming languages (such as C) uses the word “function” to represent the term modules.  main() is actually a function that is the first and last function performed when the compiled C program is executed.The main function, while running, can execute or “call” other functions.  Functions should contain simple specific operations, so they can be used when creating other programs. –Examples include: prompt and scan data & perform error-checking, make calculates, display output, etc…

15 Functions (Visual Example) declare variables main function: Run get_input function Run print_result function Calculate result get_input function: print_result function: entire program Small C program

16 Elements of a Function  Function Prototype –If a function appears after the main function (preferred method of many programmers), then a function prototype is required to “declare” a function for main.  Function Heading –A function heading is place just above the function body for identification.  Function Body –The function body is enclosed in braces and contains commands (C code) to perform the function. You can think of the body of the function as a small C program  Function Calls –A function can be run or “called” within the main function or within another other functions.

17 Example #include void report_heading (void); main () { int i, num; printf ("\nEnter an integer for Times Table: "); scanf ("%d", &num); report_heading (); for (i = 1; i <= num; i++) printf ("%d times %d equals %d\n", num, i, num*i ); printf ("\n"); } void report_heading (void) { printf ("\nMy Times Table:\n\n"); }

18 In Class Exercise #4  Modify In Class Exercise #2 to use a function called table_header to print the title: My Little Awesome Times Table By: YOUR NAME  And use another function called table_footer to print the message at the end of the times table: That’s all Folks!!!  These functions should be executed or “called” within the main function or program.


Download ppt "Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions."

Similar presentations


Ads by Google