CMCS 104, Lecture 25 - REVIEW1 Review l Read textbook chapters 1 - 6 l Read Lectures 11 - 24 l General Programming Tips: ouse Top-Down Design ouse Incremental.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Introduction to C Programming
Introduction to C Programming
True or false A variable of type char can hold the value 301. ( F )
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Introduction to C Programming
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CMSC 1041 Functions II Functions that return a value.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
The ‘while’ loop ‘round and ‘round we go.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Final Review1 Final Exam l Final Exam: Thursday December 15, 2005 at 8:30 pm in room MP-008, the regular classroom. If you can not make it to the exam.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 4 C Program Control Part I
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Functions, Part 2 of 2 Topics Functions That Return a Value
Introduction to C Programming
The C “switch” Statement
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
The C “switch” Statement
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Final Exam Final Exam: Thursday Dec 13th, 2001 at 8:30 pm in SS-111, the regular classroom.
Variables in C Topics Naming Variables Declaring Variables
Chapter 7 Additional Control Structures
Introduction to C Programming
The switch Statement Topics Multiple Selection switch Statement
3 Control Statements:.
Variables in C Topics Naming Variables Declaring Variables
The ‘while’ loop ‘round and ‘round we go.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
The while Looping Structure
DATA TYPES There are four basic data types associated with variables:
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Variables in C Topics Naming Variables Declaring Variables
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Assignment Operators Topics Increment and Decrement Operators
Functions that return a value
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

CMCS 104, Lecture 25 - REVIEW1 Review l Read textbook chapters l Read Lectures l General Programming Tips: ouse Top-Down Design ouse Incremental Programming ouse Functions to simplify code and create modules

CMCS 104, Lecture 25 - REVIEW2 Using Variables l You may declare variables in C using the data type you need - e.g.: oIntegers (int, long int, short int) oFloating point (float, double) oCharacters (char) l Examples of variable declarations: int meatballs ; float area ;

CMCS 104, Lecture 25 - REVIEW3 Declaring Variables l When we declare a variable: o space in memory is set aside to hold that data type o That space is associated with the variable name o Visualization of the declaration int meatballs ; meatballs FE07

CMCS 104, Lecture 25 - REVIEW4 Keywords in C l auto break l case char l const continue l default do l double else l enum extern l float for l goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

CMCS 104, Lecture 25 - REVIEW5 Arithmetic Operators Name Operator Example Addition +num1 + num2 Subtraction -initial - spent Multiplication *fathoms * 6 Division /sum / count Modulus %m % n

CMCS 104, Lecture 25 - REVIEW6 Modulus % l The expression m % n yields the remainder after m is divided by n. l Modulus is an integer operation. - Both operands MUST be integers. l Examples :17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5

CMCS 104, Lecture 25 - REVIEW7 Integer Division l If both operands of a division expression are integers, you get an integer answer. The fractional portion is thrown away. l Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 3 l Division where one operand is a floating point number will produce a floating point answer. Automatic promotion.

CMCS 104, Lecture 25 - REVIEW8 Arithmetic Operators Rules of Operator Precedence Operator(s) Precedence & Associativity ( )Evaluated first. If nested - innermost first. If on same level - left to right. * / % Evaluated second. If many, they are evaluated left to right + -Evaluated third. If there are several, evaluated left to right. =Evaluated last, right to left.

CMCS 104, Lecture 25 - REVIEW9 Relational Operators <less than >greater than < =less than or equal to > =grater than or equal to = =is equal to !=is not equal to Relational expressions evaluate to the int value 1 (True) or the int value 0 (False) All of these operators are called binary operators because they take two expressions as operands

CMCS 104, Lecture 25 - REVIEW10 True or False l Arithmetic expressions also evaluate to true of false. l Any expression that has a zero value is false. ( 0 ) is False l Any expression that has a non-zero value is true. ( 1 ) is True

CMCS 104, Lecture 25 - REVIEW11 Structured Programming l All programs can be written in terms of only 3 control structures o The sequence structure – Unless otherwise directed, the statements are executed in the order in which they are written. o The selection structure – Used to choose among alternative courses of action o The repetitive structure – Allows that an action is to be repeated while some condition remains true.

CMCS 104, Lecture 25 - REVIEW12 A Selection Structure the if statement if ( condition is “true” ) { statement(s) } if ( value = = 0 ) { printf (“The value you entered was zero\n”); }

CMCS 104, Lecture 25 - REVIEW13 Gotcha int a = 2; if (a = 1) { printf (“ a is one \n”); } else if (a == 2) { printf (“ a is two \n ”); } else { printf (“ The vaue of a is %d \n”, a); }

CMCS 104, Lecture 25 - REVIEW14 Example while loop children = 10 ; cookies = 1024 ; while ( children > 0 ) { children = children - 1; cookies = cookies / 2 ; }

CMCS 104, Lecture 25 - REVIEW15 Using a Sentinel Value l We could let the user keep entering the grades and when he’s done enter some special value that signals us that he’s done. l This special signal value is called a sentinel value. l We have to make sure that the value we choose as the sentinel isn’t a legal grade. (ie. can’t use 0 as the sentinel )

CMCS 104, Lecture 25 - REVIEW16 The Priming Read l When we use a sentinel value to contol a while loop, we have to get the first value from the user before we encounter the loop so that it will be tested and the loop can be entered. l This is known as a priming read. l We have to give significant thought to the initialization of variables, the sentinel value and getting into the loop.

CMCS 104, Lecture 25 - REVIEW17 The cast operator ( ) l We can use a cast operator to create a temporary value of the desired type, to be used in a calculation. l Does NOT change the variable’s type or how it is stored. l Is only good for the statement it’s in. l Often used to avoid integer division. l Used anytime we want to temporarily change a type for a calculation.

CMCS 104, Lecture 25 - REVIEW18 Using a Sentinel (continued) printf (“Enter grade, -1 to end : “); scanf (“%d”, &grade); while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf (“Enter grade, -1 to end : “); scanf (“%d”, &grade); } average = ( float ) total / counter ; printf (“The average was %.2f\n”, average) ;

CMCS 104, Lecture 25 - REVIEW19 Increment and Decrement Operators l The Increment Operator ++ l The Decrement Operator -- l Precedence - lower than (), but higher than * / and % l Associativity - right to left l Increment and decrement operators can only be applied to variables, NOT to constants or expressions

CMCS 104, Lecture 25 - REVIEW20 The for loop Repetitive Structure l The for loop handles details of the counter-controlled loop automatically l The initialization of the the loop control variable, termination conditional test and modification are handled in for loop structure for ( i = 1; i < 11; i++) { initialization modification } test

CMCS 104, Lecture 25 - REVIEW21 A for loop that counts from 0 to 10 for (i = 0; i < 11 ; i++) { printf (“%d”, i); } printf (“\n”);

CMCS 104, Lecture 25 - REVIEW22 do-while loop example do { printf (“Enter a positive number: “); scanf (“%d”, &num); if (num < = 0) { printf (“ \n That is not positive, try again \n ”); } } while (num <= 0);

CMCS 104, Lecture 25 - REVIEW23 for vs while l use a for loop when your program “knows” exactly how many times to loop l use a while loop when there is a condition that will terminate your loop

CMCS 104, Lecture 25 - REVIEW24 Nested for loops for (i = 1; i < 5; i++) { for (j = 1; j < 3; j+) { if (j % 2 = = 0) { printf (“O”); } else { printf (“X”); } printf (“\n”); } How many times is the ‘if’ statement executed? What is the output ??

CMCS 104, Lecture 25 - REVIEW25 The char data type l The char data type holds a single characterchar ch; l The char is held as a one-byte integer in memory. The ASCII code is what is actually stored, so we can use them as characters or integers, depending on our purpose l Use scanf (“%c”, &ch); to input 1 char

CMCS 104, Lecture 25 - REVIEW26 Character Example #include main ( ) { char ch; printf (“Enter a character: “); scanf (“%c”, &ch); printf (“The value of %c is %d.\n”, ch, ch); } If the user entered an A the output would be The value of A is 65.

CMCS 104, Lecture 25 - REVIEW27 The getchar ( ) function l We can also use the getchar() function that is found in the stdio library l The getchar ( ) function reads one charcter from stdin and returns that character (value) l The value can then be stored in either a char variable or an integer variable

CMCS 104, Lecture 25 - REVIEW28 getchar () example #include main ( ) { char grade; printf (“Enter a letter grade: “); grade = getchar ( ); printf (“\nThe grade you entered was %c.\n”, grade); }

CMCS 104, Lecture 25 - REVIEW29 switch example switch (day) { case 0: printf (“Sunday\n”); break; case 1: printf (“Monday\n”); break; case 2: printf (“Tuesday\n”); break; case 3: printf (“Wednesday\n”); break; case 4: printf (“Thursday\n”); break; case 5: printf (“Friday\n”); break; case 6: printf (“Saturday\n”); break; default: printf (“Error -- unexpected value for day\”); break; }

CMCS 104, Lecture 25 - REVIEW30 break l The last statement of each ‘case’ in the switch should be (99 % of the time) break; l The break causes program control to jump to the right brace of the switch l Without the break, the code flows into the next case. This is almost never what you want.

CMCS 104, Lecture 25 - REVIEW31 Logical Operators l Logical operators are used for combining condition l &&is ANDif ( (x > 5) && (y < 6) ) l || is ORif ( (z == 0) || (x > 10) ) l ! is NOTif (! (bob >42) )

CMCS 104, Lecture 25 - REVIEW32 Operator Precedence & Associativity ( )left to right/inside-out ! + (unary) - (unary) (type)right to left * / %left to right + (addition) - (subtraction)left to right >=left ot right == !=left to right &&left to right ||left to right = += -= *= /= %=right to left, (comma)right to left

CMCS 104, Lecture 25 - REVIEW33 Examining PrintMessage function #include void PrintMessage (void);function Prototype main ( ) { PrintMessage ( );function call } void PrintMessage (void)function header { printf (“A message for you:\n\n”);function printf (“Have a Nice Day!\n”); body }

CMCS 104, Lecture 25 - REVIEW34 Another version of PrintMessage void PrintMessage (int counter); main ( ) { int num; printf (“Enter an integer: “); scanf (“%d”, &num); PrintMessage (num); one argumentmatches the one } of type intformal parameter of type int void PrintMessage (int counter) { int i; for (i = 0; i < counter; i++) { printf (“Have a nice day\n\n”); }

CMCS 104, Lecture 25 - REVIEW35 Local Variables l Functions only “see” their own local variable. This includes main ( ) l The variables that are passed to the function are matched with the formal parameters in the order they are passed l The parameters are declarations of local variables. The values passed are assigned to those variables l Other local variables can be declared within the function

CMCS 104, Lecture 25 - REVIEW36 Data Types and Conversion Specifiers Data Type printf scanf conversion conversion float%f%f double%f%lf long double%Lf%Lf int%d%d long int%ld%ld unsigned int%u%u unsigned long int%lu%lu shortint%hd%hd char%c%c

CMCS 104, Lecture 25 - REVIEW37 Commonly Used Header Files header fileContains function prototypes for the standard input/output library functions & information used by them the math library functions the conversion of number to text, text to number, memory allocation, random numbers and other utility functions maninpulating time and date functions that test characters for certain properties and that can convert case

CMCS 104, Lecture 25 - REVIEW38 Manipulating what rand() returns l Since rand() returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes l Suppose we want only random numbers in the range from 0 to 5 o num = rand () % 6 l How about 1 to 6? o num = 1 + rand( ) % 6; l How about 5 to 20? o num = 5 + rand ( ) % 16;

CMCS 104, Lecture 25 - REVIEW39 srand ( ) and rand ( ) l The pseudo-random number generator needs an unsigned int as it’s seed l Although it produces what appear to be random numbers, if we use the same seed, we get the same sequence of random numbers l To get different random numbers each time we run our program, we have to give a different seed each time

CMCS 104, Lecture 25 - REVIEW40 Array Declarations l int array [5] ; l This declaration sets aside a chunk of memory that’s big enough to hold 5 integers. l It does not initialize those memory locations to 0 or any other value. l Initializing an array may be done with an array initializer, as in : int array [5] = { 5, 2, 6, 9, 3 } ; array

CMCS 104, Lecture 25 - REVIEW41 Indexing Array Elements l Values of individual elements can be found by indexing into the array. In our example, array [0] is equal to 5 and array [3] is equal to 9. l The integer in square brackets is called the subscript. l The subscript could also be an expression that evaluates to an integer. l In our example, array is the name of the array.

CMCS 104, Lecture 25 - REVIEW42 Filling Arrays l Since many arrays are quite large, using an array initializer is impractical. l Large arrays are often filled using a for loop. for ( i = 0; i < 100; i++) { rolls [ i ] = 0 ; } would set every element of the 100 element array, rolls, to 0.

CMCS 104, Lecture 25 - REVIEW43 Arrays and Pointers l The array name alone (without [ ] ) is just a variable that contains the starting address of the block of memory where the array is held. l A pointer is just a variable that holds an address. l So the array name alone is a pointer to the array. l Pointers have types. If an array is an array of ints, then the name of that array has the type pointer to int or int pointer.

CMCS 104, Lecture 25 - REVIEW44 Passing Arrays to Functions l The function prototype : void FillArray ( int array[ ], int numElems); l The function definition header: void FillArray ( int array[ ], int numElems) l The function call: FillArray ( array, SIZE); l Notice that we are passing only the name of the array (an address) and that we aren’t returning anything (the function is void)

CMCS 104, Lecture 25 - REVIEW45 Passing Arrays to Functions l If an individual element of an array such as temper[3] is passed to a function, it is passed by value not address. So the array is not modified. l Not like when &temper[0] or temper is passed.  Example on page 230 of text.

CMCS 104, Lecture 25 - REVIEW46 Multiple Subscripted Arrays l A common use of multiple subscripted arrays is to represent tables of values consisting of information arranged in rows + columns. l For example: a [ 2 ] [ 3 ] row column

CMCS 104, Lecture 25 - REVIEW47 Multiple Subscripted Arrays l When we receive a single-subscripted array as an argument to a function, the array brackets are empty in the functions parameter list. l The first subscript of a multiple-subscripted array is not required either, but all subsequent subscripts are required void printArray ( int a [ ] [ 3 ], int SIZE_R, int SIZE_C ) {... }

CMCS 104, Lecture 25 - REVIEW48 Final Exam for CS 104, Spring 2002 l To study for final exam do: ostart studying for the final exam well before the exam date so you can ask questions oask questions in class or come to the office hours or us if you have questions ostudy textbook chapters 1-6 ostudy lecture slides ouse final exam master question list on class web site no answers provided not all questions have necessarily been covered in this course (what we didn't cover will also not show up in the final exam)

CMCS 104, Lecture 25 - REVIEW49 Final Exam for CS 104, Spring 2002 l Tips for final exam: omake sure you can follow what programs do - in the final you'll be required to read and understand what some small programs do and write down their output (I find this rather easy but you need to FOCUS + CONCENTRATE to make sure you don't mix things up) oyou will also be required to write some small programs and functions and arrays so make sure you know how to do that oALWAYS WRITE DOWN SOMETHING that shows me that you worked on the problem - you might get points for trying to solve the problems even if the solution is wrong. YOU WILL NEVER GET ANY POINTS IF YOU LEAVE QUESTIONS BLANK - that especially counts for multiple choice,... questions!

CMCS 104, Lecture 25 - REVIEW50 Final Exam for CS 104, Spring 2002 l Date, Time and Place of Final Exam: oWed, May 15th, 2002 at 7:00 - 9:00 pm in AC IV 150 usual time and place different date/time than listed in UMBC schedule!