CSE240 – Introduction to
Programming Languages
Lecture 26:
Final Review
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Final Grades
• 25% Midterm exam
• 25% Final exam
• 25% Quizzes. 9 quizzes then 2.777 per quiz.
• 25% Homework. 6 homework then 4.166 per homework.
• 4% Extra points (3 + 1).
• 97 A+ |93 A- | 89 A| 85 B+ | 81 B| 77 B-| 73 C+|69 C| 65 D | E
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Current Grades
0
2
4
6
8
10
12
A+ A A- B+ B B- C+ C D E
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Topics
• Programming Languages Concepts
• C
• C++
• LISP
• Prolog
2 or 3 questions per topic. Most of them involve programming
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Quiz
# Topic %
1 Prolog Fact 2
2a Prolog Recursive 0
2b Prolog Recursive 2
3 Lisp 0
4 Lisp 3
5 C/C++ pointers 5
6 Introduction *
7 Introduction *
8 C++ 2
9 C recursion 5
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
C recursion
#include <stdio.h>
void fun(int x) {
if (x>0) {
printf("%d", x);
fun(x-1);
printf("%d", x);
}
}
int main() {
fun(5);
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
C/C++ pointers
• What delete expression correspond to the expression
ptr=new int[100]
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
LISP
• What is the output of the following code?
(let ((x '(1 2)) (y '(9 10)))
(print (+ (first x) (first y)) )
)
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Prolog
Facts
• mother_child(trude, sally).
• father_child(tom, sally).
• father_child(tom, erica).
• father_child(mike, tom).
Rules
• sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
• parent_child(X, Y) :- father_child(X, Y).
• parent_child(X, Y) :- mother_child(X, Y).
Query
• ?- sibling(sally, erica).
Yes
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Prolog
fib(0, 0).
fib(1, 1).
fib(X, Y) :- X > 1,
X2 is X – 2, fib(X2, Y2),
X1 is X – 1, fib(X1, Y1),
Y is Y1 + Y2.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Fibonacci
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12
Recursion
% this is fact
factorial(0, 1).
% this is a rule
% the factorial of F is N*F1
% if N>0 and
% N1 is N-1 and
% the factorial of N1 is F1
factorial(N, F) :-
N>0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
?- factorial (3, W).
W=6
1. factorial(3, W)
apply rule 2
2. 3>0, N1 is 3-1, factorial (N1, F1), F is 3 *F1
solve the individual parts
true, 2 is 3-1, factorial (2, F1), F is 3*F1
apply rule 2 again
3. 2>0, N2 is 2-1, factorial (N2, F2), F1 is 2 * F2
solve the individual parts
true, 1 is 2-1, factorial (1, F1), F1 is 2*F2
apply rule 2 again
4. 1>0, N3 is 1-1, factorial (N3, F3), F2 is 2 * F3
solve the individual parts
true, 0 is 1-1, factorial (0, F3), F2 is 1*F3
apply rule 1
5. factorial (0, 1)
F3 is 1
6. Go back, replace F3 to get F2, then F2 to get F1,
then F1 to get F.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
Recursion
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14
Prolog
• doSomething(A,B,P) :- P is A + B.
?-doSomething(3,2,P).
?-doSomething(3,2,10).
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

201801 CSE240 Lecture 26

  • 1.
    CSE240 – Introductionto Programming Languages Lecture 26: Final Review Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu Office Hours: By appointment
  • 2.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 2 Final Grades • 25% Midterm exam • 25% Final exam • 25% Quizzes. 9 quizzes then 2.777 per quiz. • 25% Homework. 6 homework then 4.166 per homework. • 4% Extra points (3 + 1). • 97 A+ |93 A- | 89 A| 85 B+ | 81 B| 77 B-| 73 C+|69 C| 65 D | E
  • 3.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 3 Current Grades 0 2 4 6 8 10 12 A+ A A- B+ B B- C+ C D E
  • 4.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 4 Topics • Programming Languages Concepts • C • C++ • LISP • Prolog 2 or 3 questions per topic. Most of them involve programming
  • 5.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 5 Quiz # Topic % 1 Prolog Fact 2 2a Prolog Recursive 0 2b Prolog Recursive 2 3 Lisp 0 4 Lisp 3 5 C/C++ pointers 5 6 Introduction * 7 Introduction * 8 C++ 2 9 C recursion 5
  • 6.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 6 C recursion #include <stdio.h> void fun(int x) { if (x>0) { printf("%d", x); fun(x-1); printf("%d", x); } } int main() { fun(5); }
  • 7.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 7 C/C++ pointers • What delete expression correspond to the expression ptr=new int[100]
  • 8.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 8 LISP • What is the output of the following code? (let ((x '(1 2)) (y '(9 10))) (print (+ (first x) (first y)) ) )
  • 9.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 9 Prolog Facts • mother_child(trude, sally). • father_child(tom, sally). • father_child(tom, erica). • father_child(mike, tom). Rules • sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). • parent_child(X, Y) :- father_child(X, Y). • parent_child(X, Y) :- mother_child(X, Y). Query • ?- sibling(sally, erica). Yes
  • 10.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 10 Prolog fib(0, 0). fib(1, 1). fib(X, Y) :- X > 1, X2 is X – 2, fib(X2, Y2), X1 is X – 1, fib(X1, Y1), Y is Y1 + Y2.
  • 11.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 11 Fibonacci
  • 12.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 12 Recursion % this is fact factorial(0, 1). % this is a rule % the factorial of F is N*F1 % if N>0 and % N1 is N-1 and % the factorial of N1 is F1 factorial(N, F) :- N>0, N1 is N - 1, factorial(N1, F1), F is N * F1. ?- factorial (3, W). W=6 1. factorial(3, W) apply rule 2 2. 3>0, N1 is 3-1, factorial (N1, F1), F is 3 *F1 solve the individual parts true, 2 is 3-1, factorial (2, F1), F is 3*F1 apply rule 2 again 3. 2>0, N2 is 2-1, factorial (N2, F2), F1 is 2 * F2 solve the individual parts true, 1 is 2-1, factorial (1, F1), F1 is 2*F2 apply rule 2 again 4. 1>0, N3 is 1-1, factorial (N3, F3), F2 is 2 * F3 solve the individual parts true, 0 is 1-1, factorial (0, F3), F2 is 1*F3 apply rule 1 5. factorial (0, 1) F3 is 1 6. Go back, replace F3 to get F2, then F2 to get F1, then F1 to get F.
  • 13.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 13 Recursion
  • 14.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 14 Prolog • doSomething(A,B,P) :- P is A + B. ?-doSomething(3,2,P). ?-doSomething(3,2,10).
  • 15.
    CSE240 – Introductionto Programming Languages Javier Gonzalez-Sanchez [email protected] Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.