1 / 19

Lecture 0: Review by Dr. Ziad Kobti

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;

rdyer
Download Presentation

Lecture 0: Review by Dr. Ziad Kobti

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline • Overview of Course outline • Review Exercises (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission

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

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

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

  6. Ex-1: Answer a is "17" b=12 c=5 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission

  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 replicated without permission

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

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

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

  11. Ex-2: Answer c5 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission

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

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

  14. Ex-3: Answer 112223 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission

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

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

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

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

  19. Quiz: Answer Magic Number is 20 (c) 2006 by Dr. Ziad Kobti - May not be replicated without permission

More Related