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

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

 #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
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.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
CECS 130 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
CECS 130 Mid-term Test Review Provided by REACH – Resources for Academic Achievement Presenter: [REACH Tutor]
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 2 - Introduction to C Programming
Lecture 7: Repeating a Known Number of Times
Quiz 11/15/16 – C functions, arrays and strings
CECS 130 Mid-term Test Review
Week 4 – Repetition Structures / Loops
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 4 - Program Control
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
Lecture3.
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
CECS 130 Mid-term Test Review
Presentation transcript:

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

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

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

Parts of the Program

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()

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

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 *\

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

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.

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.00, 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

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)

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

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

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

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

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

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); }

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

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); }

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

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

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!)

Conditional Statements

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”); }

Switch-Case Statements

Example Switch-Case Statement

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

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?

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;

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++){ }

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; }

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

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?

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; }

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

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.

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

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); }

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; }

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)

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()

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).

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; }

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]); }

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”); }

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.

 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

 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

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