190 likes | 218 Views
Lecture 0: Review by Dr. Ziad Kobti. 03-60-141-01 Introduction to Algorithms and Programming II. Outline. Overview of Course outline Review Exercises. 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;
E N D
Lecture 0: Reviewby 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
Outline • Overview of Course outline • Review Exercises (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
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=%d\tc=%d\n", a+b, a+=b, b=++c); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
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=%d\tc=%d\n", a+b, a=a+b, b=++c); // \t means tab and \” displays " // Assignment expressions are evaluated first // in L2R order, then printing is done return 0; } (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
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 replicated without permission
Ex-1: Answer a is "17" b=12 c=5 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
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 replicated without permission
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 replicated without permission
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 replicated without permission
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 replicated without permission
Ex-2: Answer c5 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
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 replicated without permission
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 replicated without permission
Ex-3: Answer 112223 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
Ex-4: What is the output if any? #include <stdio.h> int main() { int i=1, j=5; while(i+j) { printf("%d,%d\n", i+1, j--); } return 0; } (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
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 replicated without permission
Ex-4: Answer 2,5 2,4 2,3 2,2 2,1 2,0 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
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 %d\n", x+y-z+w); return 0; } (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission
Quiz: Answer Magic Number is 20 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission