Lecture 0 Review by Dr Ziad Kobti 03
Lecture 0: Review by Dr. Ziad Kobti 03 -60 -141 -01 Introduction to Algorithms and Programming II (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission 1
Outline • Overview of Course outline • Review Exercises (c) 2006 by Dr. Ziad Kobti - May not be 2
Ex-1: What is the output if any? #include <stdio. h> int main() { int a, b=2, c=3; a= ++b + ++c; a+=c%b*2; printf("a is "%d"nb=%dtc=%dn", a+b, a+=b, b=++c); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 3
Ex-1: simplified version #include <stdio. h> int main() { int a=0, b=2, c=3; // initializes to 0 b= b+1; // ++b is done first c= c+1; // ++c is done next a= b + c; a=a+(c%b)*2; // a+=x means a=a+x printf("a is "%d"nb=%dtc=%dn", a+b, a=a+b, b=++c); // t means tab and ” displays " // Assignment expressions are evaluated first // in L 2 R order, then printing is done return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 4
Ex-1: Notes (+=, -=, ++, --) a += b <==> a=a+b a = b++ <==> a = b b = b+1 a = ++b <==> b = b+1 a = b (c) 2006 by Dr. Ziad Kobti - May not be 5
Ex-1: Answer a is "17" b=12 c=5 (c) 2006 by Dr. Ziad Kobti - May not be 6
Ex-2: What is the output if any? #include <stdio. h> int main() { int a=1, b=5, c=3, d=0; if (a>=b && a>=c) d=a; else if (b>=a && b>=c) d=b; else d=c; printf("c"); printf("%d", d); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 7
Ex-2: What is the output if any? #include <stdio. h> int main() { int a=1, b=5, c=3, d=0; if (a>=b && a>=c) { d=a; } else { if (b>=a && b>=c) { d=b; } else { d=c; } } printf("c"); printf("%d", d); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 8
Ex-2: Notes • Line of code • Vs. statement • Vs. logical statement // single statement printf(“hello world %d”, a); Or printf( “hello world %d”, a ); (c) 2006 by Dr. Ziad Kobti - May not be 9
Ex-2: Notes // Logical statement: { printf(“hello world”); printf(“%d”, a); printf(“another statement”); printf(“all inside one block”); } (c) 2006 by Dr. Ziad Kobti - May not be 10
Ex-2: Answer c 5 (c) 2006 by Dr. Ziad Kobti - May not be 11
Ex-3: What is the output if any? #include <stdio. h> int main() { int i=1, j=5; for(; i<j; i++, j--) for(int k=0; k<=i || k>=j; k+=1) printf("%d", i); printf("%d", j); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 12
Ex-3: What is the output if any? #include <stdio. h> int main() { int i, j, k; for(i=1, j=5; i<j; i++, j--) { for(k=0; k<=i || k>=j; k+=1) { printf("%d", i); } } printf("%d", j); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 13
Ex-3: Answer 112223 (c) 2006 by Dr. Ziad Kobti - May not be 14
Ex-4: What is the output if any? #include <stdio. h> int main() { int i=1, j=5; while(i+j) { printf("%d, %dn", i+1, j--); } return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 15
Ex-4: Notes • A conditional statement in C is an expression that evaluates to 0 (false) or non-zero (true). • if (1) true • if (0) false • if (5) true (c) 2006 by Dr. Ziad Kobti - May not be 16
Ex-4: Answer 2, 5 2, 4 2, 3 2, 2 2, 1 2, 0 (c) 2006 by Dr. Ziad Kobti - May not be 17
Quiz: What is the output if any? #include <stdio. h> int main() { int w, x, y, z=2; w=++z+2; x=w--; y=w+x-(x%w)/w++; z=(w>x)? w: x; for(z=x; z<w; z++); while(z--); printf("Magic Number is %dn", x+y-z+w); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be 18
Quiz: Answer Magic Number is 20 (c) 2006 by Dr. Ziad Kobti - May not be 19
- Slides: 19