CECS 130 Mid-term Test Review - University of Louisville

CECS 130 Mid-term Test Review - University of Louisville

CECS 130 Mid-term Exam Review Summer, CECS 130 Mid-term Test Review 2019 Provided by REACH Resources for Academic Achievement Presenter: Nikhil Paonikar REACH To download this presentation Go to reach.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/REACH4CECS Variable Types Data Type Description Declaration Example Integer int x = ; -3 , 0, 3 , 29 float x = ; -0.35543 , 0.00, 554433.33281 Whole numbers, positive or negative Floating- All numbers, positive or

point negative, decimals and number fractions 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 int 4 bytes a toz , A to Z, 0 to 9, space, tab, etc. -2,147,483,648 to 2,147,483,647 short 2 bytes -32,768 long 4 bytes

-2,147,483,648 float 4 bytes + - (1.2 x 10^-38 to double 8 bytes +- (2.3 x 10^-308 to to 32,767 to 2,147,483,647 3.4 x 10^38) -1.7 x 10^308) scanf( ) ; printf( ) int x = 0; printf( What number should I print out? \n );

scanf(%d, &x); //variables require an & sign before them for scanf()! printf(\n You chose: %d \n, x); //but not for printf()!! 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; printf( %.2f, %.3f, %.5f, fboat, fboat, fboat ); 12.12, 12.123, 12.12343 8 | Output:

Arithmetic, Logic and Order of Precedence Operators ( ), !( ) *, /, % +,<, <=, >, >= ==, != &&, || Description Parentheses: evaluated from innermost to outermost Multiplication: evaluated from left to right Addition: evaluated from left to right Inequalities: evaluated from left to right Equalities: evaluated from left to right Logical expressions: &&s first, then ||s. *This is a highly abbreviated list. See https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence for the full order of precedence. Predict the printout: User enters 2 and 4:

1 | #include 2 | 3 | 4 | 5 | 6 | int main() { int iNum_1= 0; int iNum_2= 0; 7 | printf( Please enter first number: ); scanf( %d, &iNum_1 ); // (user enters 2) printf( \nEnter second number: ); scanf( %d, &iNum_2 ); // (user enters 4) printf( \n\nThe result is %d. \n, 24 / (iNum_1 * iNum_2) + 8 |

9 | 10| 11| 12| 6 / 3); 13| return 0; 14| 15| } Predict the printout: User enters 2 and 4: 1 | #include 2 | 3 | 4 |

5 | 6 | int main() { int iNum_1= 0; int iNum_2= 0; Output: Please enter first number: 2 Enter second number: 4 The result is 5. 7 | printf( Please enter first number: ); scanf( %d, &iNum_1 ); printf( \nEnter second number: ); scanf( %d, &iNum_2 ); printf( \n\nThe result is %d. \n, 24 / (iNum_1 * iNum_2) + 8 | 9 |

10| 11| 12| 6 / 3); 13| return 0; 14| 15| } Can you predict the printout? 1 | #include 2 | 3 | 4 | 5 | 6 |

7 | int main() { int x = 9; int y = 4; int result1, result2; 8 | result1 = x/y; result2 = x%y; 9 | 10| 11| printf( \n\nThe result is %d.%d, result1, 25 * 12| result2); 13|

} Can you predict the printout? Output: 1 | #include 2 | 3 | 4 | 5 | 6 | 7 | The result is 2.25 int main() { int x = 4; int y = 9; int result1, result2; 8 |

result1 = y/x; result2 = y%x; 9 | 10| 11| printf( \n\nThe result is %d.%d \n, result1, 25 * 12| result2); 13| } Conditions and Operators Operato Description r == Equal to

!= Not Equal > Greater Than < Less Than >= Order of Precedence && || Greater Than or Equal to 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 && 0 ) C. !(( 1 | | 1>0 ) && 0) Boolean Operators

Answers: A. !(1||0) =!(1) =0 B. C. !(1||1<0&&0) !((1||1<0)&&0) =!(1||0&&0) =!((1||0)&&0) =!(1||0) =!(1&&0) =!(1) =!(0) =0 =1 Example Using if-statements, write a program that will ask a user to enter a number 1, 2, or 3, and print out the User Input Printoutfollowing:

1 Smitty Werbenjagermanjensen was Number 1. 2 Fool me 2 times, cant put the blame on you. 3 Gimme 3 steps. Example: #include 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 | if(a==2) 10| printf(\nFool me %d times, cant put the blame on you.\n, 2); 11| if(a==3) 12| printf(\dGimme %d steps.\n, 3); 10| } 11| else 12| printf(\nSorry, you entered an invalid value\n ); 1 | Switch-Case Statement 1 |

switch ( ) 2 | { case : 3 | 4 | code to execute if == this-value; 5 | break; case : 6 | 7 | code to execute if == that-value; 8 |

break; 9 | ... 10| default: code executed if does not equal any of the 11| values; break; 12| 13| } The while( ) loop while ( condition ) { Code to execute while the condition

is true } 1 | 2 | 3 | 4 | #include int main() { int x = 0; 5 | while ( x < 10 ) { printf( %d, x ); x++; printf(\nFool me %d times, cant put the blame on you.\ 6 | 7 | 8 | 9 | 10|

n, 2); } getchar(); 11| 12| 13| } The for( ) loop Often used when the # of iterations is already known. Contains 3 separate expressions: 1 | 1. Variable initialization 2.

Conditional expression 3. Increment/Decrement #include 2 | int main() | { | int x = 0; | for( x=10; x>=0; x-- ) { | printf( %d\n, x ); | } |} 3 |

4 5 6 7 8 9 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 the functions parameters, how many parameters it takes, 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. Function Example #include int mult (int,int); // function prototype int main() { int x; int y;

int z; printf( printf( printf( } \n \n \n x y z = = = "); scanf( ); scanf( ); scanf( "%d",

"%d", "%d", &x &y &z ); ); ); printf( \n x*y = %d\n", mult( x, y ) ); printf( \n z^2 = %d\n", mult( z, z ) ); int mult (int a, int b) { //function definition return a * b; } Function Example #include int mult (int,int); // function prototype int main() { int x;

int y; int z; printf( printf( printf( } x \n \n = y z Output: x = 2 y = 3 z = 4 x*y = 6 z^2 = 16 "

= = ); scanf( "%d", &x ); ); scanf( "%d", &y ); ); scanf( "%d", &z ); printf( \n\n x*y = %d\n", mult( x, y ) ); printf( \n z^2 = %d\n", mult( z, z ) ); int mult { return a } (int a, int b) //function definition * b; Declaring a 1-D Array How do you declare a one-dimensional array made up of 10 integers?

Answer: int iArray[10] Other array declarations: 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 variables? Because memory spaces may not be cleared from previous values when arrays are created. Can initialize an array directly.

E.g.: int iArray[5]={0,1,2,3,4}; Can also initialize an array with a loop such as FOR( ) #include main() { int x; int iArray[5]; } for( x=0; x < 5 ; x++) { iArray[x] = x; } Example: searching an array #include int main() { int x, iValue; int iFound = -1; int iArray[5]; // initialize the array for( x=0; x < 5 ; x++) {

iArray[x] = ( x + x ); // array will = { 0, 2, 4, 6, 8 } } 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); return 0; 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 another variable. Pointer Syntax dataType *pointer_name = &variable_name; (You can also initialize it = NULL; or = 0;) Its important to initialize pointers to prevent fatal runtime errors or accidentally modifying important data. Pointers int val = 5; //variable declaration name:val name:*val_ptr 5

0x3 F location:0x3F Value Value int *val_ptr = &val; //pointer declaration location 0x83 : Pointers When an ampersand (&) is prefixed to a variable name, it refers to the memory address of this variable. 5 name:*val_ptr Value

Value name:val 0x3 F location&val = 0x3F location &val_ptr = 0x83 : : Ampersand example #include int main() { char someChar = 'x'; printf(%p\n", &someChar); } return 0; Output: ?????

Passing variables with pointers void exchange(int*, int*); void exchange(int, int); main() { int a = 5; int b = 3; exchange(&a,&b); [(3)print a and b] } //pass by reference void exchange(int *x, int *y) { [(1)print *x and *y] int temp = *i; int *x = *y; int *y = temp; [(2)print *x and *y] Output: } Output:

main() { int a = 5; int b = 3; exchange(a,b); [(3)print a and } //pass by value void exchange(int [(1)print x and int temp = x; int x = y; int y = temp; [(2)print x and } (1) *x xx = 5, *y (1) *x = =

= 5, 5, 5, *yyy= == =3 3 3 *x 3 *x = = 3, 3, *y *y = =5 5 (2) a = (2) a= =xx3, 3, =b

b3, 3,= =yy5 5= = b] x, int y) { y] y] Output: Output: (1) (1) xx = = 5, 5, yy = = 3 3 (2) (2) xx = = 3,

3, yy = = Pointers to Arrays An array variable without a bracket and a subscript represents the starting address of the array. An array variable is essentially a pointer. Suppose you declare an array of integer values 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] = {10, 3, 5}; int k = 0; main() { int list[3] = {10, 3, 5}; int k = 0;

k = *list + 1; printf(k = %d, k); } k = *(list + 1); printf(k = %d, k); } Output: k = 11 Output: k = 3 Strings Function Description strlen() Returns numeric string length up to, but not including null character

tolower() and toupper() strcpy() Converts a single character to upper or lower case Copies the contents of one string into another string strcat() Appends one string onto the end of another Compares two strings for equality strcmp() strstr() Searches the first string for the first occurrence of the second string Strings #include #include

int main() { char *str1 = REACH; char str2[] = Tutoring; printf(\nThe length of string 1 is %d \n, strlen(str1)); printf(\nThe length of string 2 is %d \n,strlen(str2)); } return 0; Output: The length of string 1 is 5 Strings #include #include void convertL(char *); int main() { char name1[] = Barbara Bush; convertL(name1); }

return 0; void convertL(char *str) { int x; for ( x = 0; x <=strlen(str) ; x++) str[x] = tolower(str[x]); } Output: The name in lowercase is barbara bush printf(\nThe name in lowercase is %s\n, str); Data File Hierarchy Entity Description Bit Binary digit, 0 or Smallest value in a data file 1 Byte

Eight bits Field Grouping of bytes Record Grouping of fields File Grouping of records Stores a single character i.e a word, social security number a single row of information, student name, age, ID, GPA separate fields in a record using spaces, tabs, or commas Data Structures Arrays require that all elements be of the same data type. It is often 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. Struct Syntax struct oneCar //elements in a list of cars { int year; char make[10]; char model[10]; char tag[8]; }fleet; //for short well call it a fleet fleet rentals[5]; // five cars that we rent out to clients rentals[0].year = 2008; rentals[0].make = Honda; fleet fleet vans[3]; vans[3]; //company //company vans

vans rentals[0].model = Element; rentals[0].tag = AS2395; vans[0].year vans[0].year == 2012; 2012; [] vans[0].make vans[0].make == Ford; Ford; rentals[4].year = 2015; vans[0].model vans[0].model == EconoVan; EconoVan; rentals[4].make = Tesla Vans[0].tag Vans[0].tag == MV1NUP; MV1NUP; rentals[4].model = Model S; rentals[4].tag = 2FST4U; Dynamic memory allocation These functions are defined in the header file.

1.void *calloc(int num, int size) Allocates an array of num elements each of whose size in bytes will be size. 2.void free(void *address) Releases a block of memory block specified by address. 3.void *malloc(int num) Allocates an array of num bytes and leave them uninitialised. 4.void *realloc(void *address, int newsize) Re-allocates memory extending it up to newsize. Implementing malloc() Implementing calloc() Resizing and releasing memory using realloc() and free() On the test Some types of problems you will likely 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) Mixing up * and & in general. Off-by-one errors when accessing an array, especially when using a FOR loop. Test Success: The Night Before Avoid extensive study Conduct a brief review Do something relaxing Get plenty of sleep Avoid stress-inducing situations Maintain a positive attitude Test Success: Day of the Test Get off to a good start. Arrive on time or early.

Compose yourself. Do a final revision of your notes. Maintain a confident attitude. comic by KC Green Hope for the best, prepare for the worst. Cheers and good luck! (If you have not signed in, please do so before you leave!) This presentation was provided by REACH Resources for Academic Achievement To download this presentation Go to reach.louisville.edu bit.ly/31JDZL5

Recently Viewed Presentations

  • Evolution Review - anderson.k12.ky.us
  • Functional Group Nomenclature Organic Chemistry Lesson # 3

    Functional Group Nomenclature Organic Chemistry Lesson # 3

    The part attached to the C=O is from the acid and is named using the suffix "oate". a) pentyl butanoate b) propyl ethanoate Drawing Practice c) methyl benzoate d) ethyl 3-methylbutanoate e) 2-methylbutyl ethanoate Naming Practice . Title: Slide 1...
  • 22 - Riverside City College

    22 - Riverside City College

    Alveolar Air. RULE: Intuitively, one knows which way O2 moves, so remember CO2 always goes the opposite way. Figure 11.10: Gas exchange between blood in pulmonary capillaries and air in alveoli. External Respiration- AT THE LUNGS. 1. In LUNGS. a)...
  • Reading Vernier Scales - Missouri University of Science and ...

    Reading Vernier Scales - Missouri University of Science and ...

    The reading on the Vernier scale is added to the reading on the primary scale to increase the precision. A typical Vernier adds one or two significant figures to a given measurement. In Physics 1135 and 2135 labs, we use...
  • 90&#x27;s Fashion: Proudly presented by N.K.O.T.B. - Drama

    90's Fashion: Proudly presented by N.K.O.T.B. - Drama

    90's Fashion: Proudly presented by N.K.O.T.B. 90' hair for women. 90's Hair for men. What all the cool chicks were wearing! Guys hip, cool threads. ... Created Date: 04/20/2015 17:41:20 Title: PowerPoint Presentation Last modified by: Tahanyth Webster-Otto Company:
  • 22C:19 Discrete Math

    22C:19 Discrete Math

    A cut vertex (or articulation point ) is a vertex, by removing. which one can partition the graph. A cut edge is an edge by removing which one can partition the graph. If multiple edges need to be remove to...
  • Gradual Release of Control and Complex Text

    Gradual Release of Control and Complex Text

    Reading instruction has been dominated by the idea of the "instructional level" ... had spoken to me as a boy while I was in Mama, and as he approached the box that contained the question, NAME OF CHILD, he wrote...
  • Fifth Business - PBworks

    Fifth Business - PBworks

    Liesl asks Dunny "Are you Fifth Business? You had better find out." Definition Neither hero, heroine, confidante nor villain Essential to bring about the recognition, the revelation, the understanding of the story's outcome The baritone The odd man out, the...