Presentation is loading. Please wait.

Presentation is loading. Please wait.

CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)

Similar presentations


Presentation on theme: "CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)"— Presentation transcript:

1 CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)

2 To download this presentation… Go to reach.louisville.edureach.louisville.edu Click “Tutoring” at the top left of the page. Click “Computer Tutoring” under the heading “TUTORING SERVICES FOR ALL STUDENTS.” Click “CECS Test Reviews” on the right-hand column. Scroll down to find your class. Or, just go to tiny.cc/REACH4CECStiny.cc/REACH4CECS

3 Parts of the Program #include main() { printf(“\nC you later\n”); system(“pause”); }

4 Parts of the Program

5 Program Statements Serve to control program execution and functionality. Must end with a semicolon(;) with the exception of:  Comments: /* */  Preprocessor Directives: #include or #define  Begin and end program identifiers: { }  Function definition beginnings: main()

6 The main{} function  Functions allow you to group program statements under one name  C is case-sensitive so main(), MAIN(), and Main() are all different  The main function is special because the values it returns are returned to the operating system  Most main functions in this course do not take or pass information to the operating system

7 Escape Sequences  Definition: Escape sequences are specially sequenced characters used to format output  \” Ex: printf(“\“This is quoted text \“ “); -> “This is quoted text”  \’ Ex: printf(“A single quote looks like \’ ”); -> A single quote looks like ‘  \* This is a comment block *\

8 Directives  #include  Using a directive to include a header file  stdio.h = st an d ard i nput o utput header file  stdlib.h = st an d ard lib rary of ‘system’ commands

9 Memory  A computer’s long-term memory is called nonvolatile memory and is generally associated with mass storage devices, such as hard disk drives (HDDs) or solid state drives (SSDs).  A computer’s short term memory is usually random-access memory (RAM), which is volatile memory. It clears all of its data when power is removed from the computer.

10 Data Types Data TypeDescriptionDeclarationExample IntegerWhole numbers, positive or negative int x =__;-3, 0, 3, 29 Floating- point number All numbers, positive or negative, decimals and fractions float x =__;-0.35543, 0.00, 554433.33281 CharacterRepresentations of integer values known as character codes char x = ‘__’; m, M, $ To declare a constant (read only) value: const int x = 20 const float PI = 3.14

11 Variable Types TYPESIZEVALUES bool1 bytetrue (1) or false (0) char1 byte‘a’ to‘z’, ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on int4 bytes-2,147,483,648 to 2,147,483,647 short2 bytes-32,768 to 32,767 long4 bytes-2,147,483,648 to 2,147,483,647 float4 bytes+ - (1.2 x 10^-38 to 3.4 x 10^38) double8 bytes+- (2.3 x 10^-308 to -1.7 x 10^308)

12 Printf() ; Scanf()  Can you explain what each line of the code is doing?

13 Conversion Specifiers  Character - %c  Integer - %d  Float (decimal)- %f  String - %s  Printf Format Tags: %[flags][width][.precision][length]specifier e.g. -> %[.precision]specifer -> %.2f

14 Conversion Specifier Examples: int main() { printf (“%c %c \n", 'a', 65); -> aA printf ("%010d \n", 1977); -> 0000001997 printf ("floats: %.2f \n", 3.1416); -> 3.14 printf ("floats: %.4f \n", 3.1416); -> 3.1415 printf ("%s \n", "A string"); -> A string printf(“%f \n”, 55.55); -> 55.550000 return 0; }

15 Review Printing with Precision ExamplePrintout printf(“%.1f”,3.123456);3.1 printf(“\n%.2f”,3.123456);3.12 printf(“\n%.3f”,3.123456);3.123 printf(“\n%.4f”,3.123456);3.1234 printf(“\n%.5f”,3.123456);3.12345 printf(“\n%.6f”,3.123456);3.123456

16 scanf()  Syntax scanf(“conversion specifier”, &variable); Conversion Specifiers Description %dReceives integer value %fReceives floating-point number %cReceives character Missing this is a common error!

17 Can you predict the printout when the user enters 2 and 4? #include main() { int iOperand1 = 0; int iOperand2 = 0; printf(“\n Enter first operand: “); scanf(“%d”, &iOperand1); printf(“\n Enter second operand: “); scanf(“%d”, &iOperand2); printf(“The result is %d \n”, 24/(iOperand1 * iOperand2)+6/3); }

18 Arithmetic and Order of Precedence OperatorDescription *Multiplication /Division %Modulus (remainder) +Addition -Subtraction Order of Precedence Description ( )Parentheses are evaluated first, from innermost to outermost *, /, %Evaluated second, from Left to Right +, -Evaluated last, from Left to Right

19 Can you predict the printout? #include main() { int x = 4; int y = 9; int result1, result2; result1 = y/x; result2 = y%x; printf(“The result is %d.%d \n”, result1, 25*result2); }

20 Conditions and Operators OperatorDescription ==Equal to !=Not Equal >Greater Than <Less Than >=Greater Than or Equal to <=Less Than or Equal to Order of Precedence Description &&AND condition ||OR condition

21 Boolean Operators Do you know the answers to these?  A. !( 1 || 0 )  B. !( 1 || 1 && 0 )  C. !( ( 1 || 0 ) && 0 )

22 Boolean Operators Quiz Answers  A. !( 1 || 0 ) ANSWER: 0  B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)  C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful!)

23 Conditional Statements

24 How Does This Code Work? #include int main() { int a; printf (“Enter one of the following: %d, %d, or %d\n”, 1, 2, 3); scanf(“%d”, &a); if(a==1|| a==2|| a ==3) { if(a==1) { printf(“\n %d is the loneliest number \n“, 1); } if(a==2) { printf(“\n%d is better than %d \n”,2,1); } if(a==3) { printf(“\n%d \’ s a crowd \n”,3); } else printf(“Sorry, you entered an invalid value\n”); }

25 Switch-Case Statements

26 Example Switch-Case Statement

27 The while(){} loop: while ( condition ) { //Code to execute while the condition is true } Example:

28 Do-While Loop  Syntax:  do { //Code executed while condition is true } while ( condition );  Question: What is the main difference between the “do-while” and “while” functions?

29 Operators Cont. x++;Tells the function to use the current value of x and increment it by 1. ++x;Increment the value of x by 1 and use the new value for calculations. x--; Tells the function to use the current value of x and decrease its value by 1. --x; Decrease the value of x by 1 and use the new value for calculations. e.g. x=0; printf(“The Value of x is: %d”, x++); printf(“\n The Value of x is: %d”,++x); Would results in:The Value of x is: 0 The Value of x is: 2 Application of: += & -=Equivalent Equation x += y; x = x + y; x -= y; x = x – y;

30 For Loop  Often used when the # of iterations is already known.  Contains 3 separate expressions: 1. Variable initialization 2. Conditional expression 3. Increment/Decrement Don’t forget: When writing a for loop, the “arguments” are separated by semicolons, not commas! i.e.… Do this: for(x=0 ; x<10 ; x++){ } Not this: for(x=0, x<10, x++){ }

31 For Loop example: This program uses a for(){} loop to count down from 10 to 0: #include main() { int x=0; for(x=10; x>=0; x--) { printf("%d \n", x); } return 0; }

32 Break/Continue Statements  break; Used to exit a loop. Once this statement is executed, the program will execute the statement immediately following the end of the loop.  continue; Used to manipulate program flow in a loop. When executed, any remaining statements in the loop will be skipped and the next iteration of the loop will begin

33 Function Prototypes & Definitions  Function Prototype Syntax: return-type function_name ( arg_type arg1,..., arg_type argN )  Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters  Function definitions implement the function prototype  Where are function prototypes located in the program?  Where do you find function definitions?

34 Calling Functions #include int mult(int, int); int main() { int x; int y; printf( "Please input the first number to be multiplied: " ); scanf( "%d", &x ); printf( “\nNow the second one: “); scanf( "%d", &y ); printf( “\nThe product of your two numbers is %d\n", mult( x, y ) ); getchar(); } int mult (int a, int b) { return a * b; }

35 Can you spot the errors? #include printNumber(); main() { int x; printNumber(x); } printNumber() { printf(“\n The number is %d \n”, x) }

36 Answer #include void printNumber( int x); main() { int x; printNumber(x); } void printNumber(int y) { printf(“\n The number is %d \n”, y); } *Note: it’s not absolutely necessary to write VOID, but it’s a good practice.

37 Variable Scope  Variable scope defines the lifetime of a variable  Local Scope: defined within functions and loses scope after function is finished. Can reuse in other functions  Global Scope: defined outside of functions and can be accessed by multiple functions

38 Can You Pick Out the Local and Global Variables? #include void printNumbers(); int iNumber; main() { int x; for(x=0, x<10,x++){ printf(“\n Enter a number:”); scanf(“%d”, &iNumber); printNumbers(); } void printNumbers() { printf(“\n Your number is: %d \n”, iNumber); }

39 More example code: #include int addNumbers(int, int, int, int); int multNumbers(int, int, int, int); main() { int a, b, c, d, counter=0; while( counter<6 ) { printf(“\n Please enter 4 integers separated by spaces:\n”); scanf(“%d %d %d %d”, &a, &b, &c, &d); printf(“\n %d \n”, addNumbers(a,b,c,d)-multNumbers(a,b,c,d)); count++; } int addNumbers(int f, int g, int h, int y) { return f+g+h+y; } int multNumbers(int f, int g, int h, int y) { return f*g*h*y; }

40 How to Declare a One- Dimensional Array  Can you declare a one-dimensional array made up of 10 integers?  Answer: int iArray[10]  Example array declarations -  int iArray[10];  float fAverages[30];  double dResults[3];  short sSalaries [9];  char cName[19]; (this makes 18 characters plus 1 null character)

41 Initializing a 1-D Array  Why do we initialize? Because memory spaces may not be cleared from previous values when arrays are created.  Can initialize an array directly  Example int iArray[5]={0,1,2,3,4};  Can initialize an array with a loop such as FOR()

42 Example of Initializing an Array Using a For() Loop #include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; } ***N.B. to initialize the array, you give its size (in this case, five), but to access a specifc element, you use its index number, which always ascends from zero (In this case, the available cells are 0, 1, 2, 3, and 4).

43 Printing Arrays  Can you add code to this program so that it prints out the values of the array? #include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; }

44 Answer #include main( ) { int x; int iArray[5]; for( x=0; x<5 ; x++) { iArray[x] = 0; } for(x=0 ; x<5 ; x++) { printf(“\n The value of iArray index %d is %d \n”, x, iArray[x]); }

45 Searching an Array: #include main() { int x, iValue, iFound = -1; int iArray[5]; for( x=0; x < 5 ; x++) iArray[x] = (x+x); printf(“\n Enter value to search for: ”); scanf(“%d”, &iValue); for(x=0 ; x<5; x++) { if( iArray[x] == iValue ) { iFound = x; break; } if(iFound >-1) printf(“\n I found your search value in element %d \n”, iFound); else printf(“\n Sorry, your search value was not found \n”); }

46 On the test… Some types of problems you will encounter:  True/False questions  Fill-in-the-blanks questions  Finding errors in programs Common mistakes on this test:  Not appending lines of code with a semicolon (“;”)  Forgetting to use semicolons when writing a FOR loop  Forgetting to add an ampersand (“&”) to your variable parameter in SCANF functions (and mistakenly using the ampersand when using PRINTF)  Off-by-one errors when accessing an array, especially when using a FOR loop.

47  Avoid Extensive Study  Only Do a Brief Review  Do Something Relaxing  Get Plenty of Sleep  Avoid Stress-Producing Situations  Maintain a Positive Attitude Test Success: The Night Before The Test

48  Get Off to a Good Start  Arrive on Time  Compose Yourself  Do a Final Scan of Your Notes  Maintain a Confident Attitude Test Success: The Day of the Test

49 Good luck from REACH on your test! (If you have not signed in, please do so before you leave!) Any Questions?


Download ppt "CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)"

Similar presentations


Ads by Google