Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 15: Midterm Review Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Parameters

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs Functions and Parameter Passing § A function is a named block of code that must be explicitly called. § The purposes of using functions are twofold: abstraction and reuse: abstraction: statements that form a conceptual unit; reuse: statements that can be executed in more than one place in the program. § Functions communicate with the rest of the program by using either global variables or parameters / return value § Formal and actual parameters. In the declaration formal parameters. When calling a function, actual parameters are given.

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs Parameter Passing § Call-by-value: a formal parameter is a local variable in the function. It is initialized to the value of the actual parameter. It is a copy of the actual parameter. Advantage: no side-effects (safe, reliable). Drawback: less flexible/less powerful. § Call-by-address (pointer or reference): the formal parameter is a pointer to the actual parameter. There is only one variable with two names. Changing the formal parameter immediately changes the actual parameter. Reference in C++ only. Drawback : side-effects (programmer skills) Advantage: flexible/powerful.

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs Call-by-alias and call-by-address

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs Call-by-alias and call-by-address Use the C++ Compiler

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs Passing String Address Using Pointer

Slide 8

Slide 8 text

jgs Midterm Review

Slide 9

Slide 9 text

jgs The following slides shows some examples related to some topics This is NOT a comprehensive list of topics Topics in the exam can be found Weeks 1 to 8

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs Sample Question 1

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs Sample Question 2

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs Sample Question 3

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs Structure § Provide 3 examples of lexical errors in C. § Provide 3 examples of syntactic errors in C. § Provide 3 examples of semantic errors in C. § Provide 3 examples of lexical errors in C++. § Provide 3 examples of syntactic errors in C++. § Provide 3 examples of semantic errors in C++. § Provide 3 examples of lexical errors in Java § Provide 3 examples of syntactic errors in Java § Provide 3 examples of semantic errors in Java § Exemplify a semantic error in Java that is NOT a semantic error in C/C++ § Given char a [5] = {'a', 'b', 'c', '\0'}; Is there is an error in the following line? char * b = a; If so, is it lexical, syntactical, or semantical?

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 jgs Paradigms § Provide 3 characteristics/features of an imperative programming language § Provide 3 characteristics/features of a functional programming language § Provide 3 characteristics/features of a logic programming language § Provide 3 characteristics/features of an object-oriented programming language § Mention 3 differences (regarding features or capabilities) between C++ and Java (both are object-oriented programming languages).

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 jgs Recursion § Create a function that print the following. It should be recursive, i.e., DO NOT use for, do/while or while statements here. 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1,

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 jgs Arrays § Did you now the tic-tac-toe game? Create a 3x3 array to store a tic-tac-toe. Fill the array with ‘X’ or ‘O’. All the cells with the same value. Which data type should be used? Provide your source code using C § Use the language C. Create a struct named ball. Put 2 variables inside: an array of chars to store a color and an integer to store an id. § Ask the user for a number using scanf. § Use the number that the user provides to create an array of balls. The number represent how many elements the array should have. § Store in the first position of the array a ball with color= blue and id=1.

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 jgs Arrays § Write a C program that, given an array a[ ] with size n and another number x, determines whether there exist two elements in a [ ] whose sum is exactly x. It returns 0 (the two elements do not exist) or 1 (the two elements exist). § Write a C program that, given an array of ball (see question 5) named a[] of n items, prints the id of all the items with color==red.

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 jgs Pointers § In the following code: #include int main() { int var =10; int *p; p= &var; printf (“\n %p", &var); printf (“\n %p", p); printf (“\n %p", &p); printf (“\n %p", p); printf (“\n %d", var); printf ("\n %d", *p); printf (“\n %d", *( &var)); } Could you explain what is being printed for each printf instruction?

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 jgs Dynamic 2D Array

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 20 jgs Questions

Slide 21

Slide 21 text

jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.