Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 15

CSE240 Lecture 15

Introduction to Programming Languages
Midterm review
(202110)

Javier Gonzalez-Sanchez
PRO

January 15, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    View Slide

  2. jgs
    Parameters

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  8. jgs
    Midterm Review

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  13. 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?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  18. 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?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide