CECS 130 Mid-term Test Review Provided by REACH Resources for Academic Achievement Presenter: A REACH Tutor REACH
Content on the Mid-term Exam ochapters 1-10 from C Programming for the Absolute Beginner, Second Edition by Michael Vine
So, we will be covering: Data types Pointers Conditions Strings Loops Data Structures Functions Dynamic Memory Arrays
Variable Types Data Type Description Declaration Example Integer Floatingpoint number Whole numbers, positive or negative All numbers, positive or negative, decimals and fractions int x = ; -3, 0, 3, 29 float x = ; -0.35543, 0.00, 554433.33281 Character Representations 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 TYPE SIZE VALUES bool 1 byte true (1) or false (0) char 1 byte a to z, A to Z, 0 to 9, space, tab, etc. int 4 bytes -2,147,483,648 to 2,147,483,647 short 2 bytes -32,768 to 32,767 long 4 bytes -2,147,483,648 to 2,147,483,647 float 4 bytes + - (1.2 x 10^-38 to 3.4 x 10^38) double 8 bytes +- (2.3 x 10^-308 to -1.7 x 10^308)
Printf( ) ; Scanf( ) ; Can you explain what the code is doing? 1 #include <stdio.h> 2 3 int main() 4 { 5 int this_is_a_number = 0; 6 7 printf( Please enter a number: ); 8 scanf( %d, &this_is_a_number ); 9 printf( \nyou entered %d, this_is_a_number ); 10 getchar(); 11 12 return 0; 13
Conversion Specifiers Character - %c Integer - %d Float (decimal)- %f String - %s Printf Format Tags: Format: %[flags][width][.precision][length]specifier Example: %[.precision]specifier 7 float fboat = 12.123432; 8 printf( %.2f, %.3f, %.5f, boat, boat, boat ); Output: 12.12, 12.123, 12.12343
Can you predict the printout? 1 int main() 2 { 3 printf( %c%c\n, a,65); 4 printf( %10d\n,1997); 5 printf( %010d\n, 1997); 6 printf( floats: %4.2f \n, 3.1416); 7 printf( %s\n, A string ); 8 printf( %f\n, 55.55); 9 return 0; 10
Can you predict the printout? 1 int main() 2 { 3 printf( %c%c\n, a,65); 4 printf( %10d\n,1997); 5 printf( %010d\n, 1997); 6 printf( floats: %4.2f \n, 3.1416); 7 printf( %s\n, A string ); 8 printf( %f, 55.55); Output: aa 1977 0000001997 floats: 3.14 A string 55.55 9 return 0; 10
Scanf( ); Conversion Specifies Description %d Receives integer value %f Receives floating-point number %c Receives character
Predict the printout: User enters 2 and 4 : 1 #include <stdio.h> 2 3 int main() 4 { Output: Please enter first number: 2 Enter second number: 4 The result is 5. 5 int inum_1= 0; 6 int inum_2= 0; 7 8 printf( Please enter first number: ); 9 scanf( %d, &inum_1 ); 10 printf( \nenter second number: ); 11 scanf( %d, &inum_2 ); 12 printf( \n\nthe result is %d. \n, 24 / (inum_1 * inum_2) + 6 / 3); 13 14 return 0; 15
Predict the printout: User enters 2 and 4 : 1 #include <stdio.h> 2 3 int main() 4 { Output: Please enter first number: 2 Enter second number: 4 The result is 5. 5 int inum_1= 0; 6 int inum_2= 0; 7 8 printf( Please enter first number: ); 9 scanf( %d, &inum_1 ); 10 printf( \nenter second number: ); 11 scanf( %d, &inum_2 ); 12 printf( \n\nthe result is %d. \n, 24 / (inum_1 * inum_2) + 6 / 3); 13 14 return 0; 15
Arithmetic and Order of Precedence Operator Description * 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? Output: 1 #include <stdio.h> 2 3 int main() 4 { 5 int x = 0; 6 int y = 0; 7 int result1, result2; The result is 2.25 8 9 result1 = y/x; 10 result2 = y%x; 11 12 printf( \n\nthe result is %d.%d \n, result1, 25 * result2); 13
Can you predict the printout? Output: 1 #include <stdio.h> 2 3 int main() 4 { 5 int x = 0; 6 int y = 0; 7 int result1, result2; The result is 2.25 8 9 result1 = y/x; 10 result2 = y%x; 11 12 printf( \n\nthe result is %d.%d \n, result1, 25 * result2); 13
Conditions and Operators Operator Description == Equal to!= Not Equal > Greater Than < Less Than >= Greater Than or Equal to Order of Precedence Description && AND condition OR condition
Comparisons > greater than 5 > 4 is TRUE < less than 4 < 5 is TRUE >= greater than or equal 4 >= 4 is TRUE <= less than or equal 3 <= 4 is TRUE == equal to 5 == 5 is TRUE!= not equal to 5!= 4 is TRUE
Boolean Operators Do you know the answer to these? A.!( 1 0 ) B.!( 1 1&& 0 ) C.!(( 1 0 ) && 0) =!( 1 ) = 0 =!( 1 0 ) = 0 =!(1 && 0) =!0 = 1
Boolean Operators Do you know the answer to these? A.!( 1 0 ) B.!( 1 1&& 0 ) C.!(( 1 0 ) && 0) =!( 1 ) = 0 =!( 1 0 ) = 0 =!(1 && 0) =!0 = 1
Quiz Using if-statements, write a program that will ask a user to enter a number 1, 2, or 3, and print out the following: User Input Printout 1 Smitty Werbenjagermanjensen was Number 1. 2 Fool me 2 times, can t put the blame on you. 3 Gimme 3 steps.
Possible Answer : 1 #include <stdio.h> 2 int main() { 4 printf( Enter one of the following: %d, %d, or %d\n,1,2,3 ); 5 scanf( %d, &a ); 6 if(a==1 a==2 a==3) { 7 if(a==1){ 8 printf( \nsmitty Werbenjagermanjensen was Number %d.\n, 1); 9 10 if(a==2){ 11 printf( \nfool me %d times, can t put the blame on you.\n, 2); 12 10 if(a==3){ 11 printf( \ngimme %d steps.\n, 3); 12 16 else 17 printf( Sorry, you entered an invalid value\n ); 18 return 0;
Switch-Case Statement 1 switch ( <var> ) 2 { 3 case <this-value>: 4 code to execute if <var> == this-value; 5 break; 6 case <that-value>: 7 code to execute if <var> == that-value; 8 break; 9... 10 default: 11 code executed if <var> does not equal any of the values; 12 break; 13
Loops while ( condition ) { Code to execute while the condition is true Quiz: Can you write a program that prints x while x increments from 0 to 10?
Possible Answer : 1 #include <stdio.h> 2 int main() 3 { 4 int x = 0; 5 6 while ( x < 10 ) 7 { 8 printf( %d, x ); 9 x++; 10 printf( \nfool me %d times, can t put the blame on you.\n, 2); 11 12 getchar(); 13
For Loop Often used when the # of iterations is already known. Contains 3 separate expressions: 1. Variable initialization 2. Conditional expression 3. Increment/Decrement Write a program with a for loop that counts down from 10.
Possible Answer : 1 #include <stdio.h> 2 3 int main() 4 { 5 int x = 0; 6 for( x=10; x>=0; x-- ) 7 { 8 printf( %d\n, x ); 9 10 11 system( pause ); 12
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?
Function Prototypes & Definitions Where are function prototypes located in the program? Answer: before the main( ) { function. Where do you find function definitions? Answer: function definitions are self-contained outside of the main( ) { function, usually written below it.
Calling Functions #include <stdio.h> int mult ( int x, int y ); // function prototype int main() { int x; int y; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n", mult( x, y ) ); getchar(); return 0; int mult (int x, int y) //function definition { return x * y;
Declaring a 1-D Array Can you declare a one-dimensional array made up of 10 integers? Answer: int iarray[10] How to declare an Array: int iarray[10]; float faverages[30]; double dresults[3]; short ssalaries [9]; char cname[19]; // 18 characters and 1 null character
Declaring 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( )
Initializing a 1-D Array with a for( ) loop #include <stdio.h> main() { int x; int iarray[5]; for( x=0; x < 5 ; x++) { iarray[x] = 0;
Initializing a 1-D Array with a for( ) loop Now add printing: #include <stdio.h> main() { int x; int iarray[5]; for( x=0; x < 5 ; x++ ) { iarray[x] = 0; for( x=0 ; x<5; x++ ) { printf( \n iarray[%d] = %d \n, x, iarray[x] );
Searching an array #include <stdio.h> main() { int x; int ivalue; int ifound = -1; int iarray[5]; // initialize the array for( x=0; x < 5 ; x++) { iarray[x] = (x+x); printf( \n Enter value to search for: ); scanf( %d, &ivalue); // search for number 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 );
Pointers Pointer variables, simply called pointers, are designed to hold memory addresses as their values. Normally, a variable contains a specific value, e.g., an integer, a floating-point value, or a character. However, a pointer contains the memory address of a variable that in turn contains a specific value.
Pointers int val = 5; int *val_ptr = &val; val *val_ptr 5 0x3F 0x3F 0x83
Pointer Syntax datatype *pointer_name = &variable_name; OR datatype *pointer_name = NULL; OR datatype *pointer_name = 0; It s important to initialize pointers to prevent fatal runtime errors or accidentally modifying important data.
Pointers When an ampersand (&) is prefixed to a variable name, it refers to the memory address of this variable. val *val_ptr 5 0x3F &val = 0x3F &val_ptr = 0x83
Ampersand example #include <stdio.h> int main() { char somechar = 'x'; printf( %p\n", &somechar); system("pause"); return 0; Output: 0022FF47
Passing variables by value void exchange(int x, int y); main() { int a = 5; int b = 3; exchange(a,b); [print a and b] void exchange(int x, int y); { [print x and y] int temp = x; int x = y; int y = temp; [print x and y]
Passing variables by value void exchange(int x, int y); main() { int a = 5; int b = 3; exchange(a,b); [print a and b] void exchange(int x, int y); { [print x and y] int temp = x; int x = y; int y = temp; [print x and y] Output: x = 5, y = 3 x = 3, y = 5 a = 5, b = 3
Passing variables with pointers void exchange(int*, int*); main() { int a = 5; int b = 3; exchange(&a,&b); [print a and b] void exchange(int *x, int *y); { [print *x and *y] int temp = *i; int *x = *y; int *y = temp; [print *x and *y]
Passing variables with pointers void exchange(int*, int*); main() { int a = 5; int b = 3; exchange(&a,&b); [print a and b] void exchange(int *x, int *y); { [print *x and *y] int temp = *i; int *x = *y; int *y = temp; [print *x and *y] Output: *x = 5, *y = 3 *x = 3, *y = 5 a = 3, b = 5
Pointers Pointing to Pointers int val = 5; int *val_ptr = &val; int **val_ptr_ptr = &val_ptr; val *val_ptr **val_ptr_ptr 5 0x3F 0x83 &val = 0x3F &val_ptr = 0x83 &val_ptr_ptr = 0xF5
Pointers Pointing to Pointers val = 5 *val_ptr = 5 **val_ptr_ptr = 5 **val_ptr_ptr = *val_ptr = val = 5 *val_ptr_ptr = val_ptr = &val = 0x3F val_ptr_ptr = &val_ptr = 0x83 &val_ptr_ptr = 0xF5 val *val_ptr **val_ptr_ptr 5 0x3F 0x83 &val = 0x3F &val_ptr = 0x83 &val_ptr_ptr = 0xF5
Pointers to Arrays An array variable without a bracket and a subscript actually represents the starting address of the array. An array variable is essentially a pointer. Suppose you declare an array of int value as follows: int list[6] = {11, 12, 13, 14, 15, 16; *(list + 1) is different from *list + 1. The dereference operator (*) has precedence over +. So, *list + 1 adds 1 to the value of the first element in the array, while *(list + 1) dereferences the element at address (list + 1) in the array.
Pointers to Arrays main() { int list[3] = {1, 0, 5; int k = 0; main() { int list[3] = {1, 0, 5; int k = 0; k = *list + 1; k = *(list + 1); [print k] [print k]
Pointers to Arrays main() { int list[3] = {1, 0, 5; int k = 0; main() { int list[3] = {1, 0, 5; int k = 0; k = *list + 1; k = *(list + 1); [print k] [print k] Output: k = 2 Output: k = 0
Strings Function strlen() Description Returns numeric string length up to, but not including null character tolower() and toupper() Converts a single character to upper or lower case strcpy() strcat() strcmp() strstr() Copies the contents of one string into another string Appends one string onto the end of another Compares two strings for equality Searches the first string for the first occurrence of the second string
Strings #include <stdio.h> #include <string.h> main() { char *str1 = REACH ; char str2[] = Tutoring ; printf( \nthe length of string 1 is %d \n, strlen(str1)); printf( The length of string 2 is %d\n,strlen(str2));
Strings #include <stdio.h> #include <string.h> main() { char *str1 = REACH ; char str2[] = Tutoring ; printf( \nthe length of string 1 is %d \n, strlen(str1)); printf( The length of string 2 is %d\n,strlen(str2)); Output: The length of string 1 is 5 The length of string 2 is 8
Strings #include <stdio.h> #include <string.h> void convertl(char *); main() { char name1[] = Barbara Bush ; convertl(name1); void convertl(char *str) { int x; for ( x = 0; x <=strlen(str) ; x++) str[x] = tolower(str[x]); printf( \nthe name in l.c. is %s\n, str);
Strings #include <stdio.h> #include <string.h> void convertl(char *); Output: main() { char name1[] = Barbara Bush ; convertl(name1); The name in l.c. is barbarabush void convertl(char *str) { int x; for ( x = 0; x <=strlen(str) ; x++) str[x] = tolower(str[x]); printf( \nthe name in l.c. is %s\n, str);
Data Structures Arrays require that all elements be of the same data type. Many times it is necessary to group information of different data types. An example is a list of materials for a product. The list typically includes a name for each item, a part number, dimensions, weight, and cost. C and C++ support data structures that can store combinations of character, integer floating point and enumerated type data. They are called - structs.
Structure Syntax struct my_example { int assignment; char grade; char name[20]; mystruct; mystruct students[3]; stu1.assignment = 3; stu1.grade = A ; stu1.name = Patrick ;
Thank you! This presentation was provided by REACH Resources for Academic Achievement