1 / 37

CS1010

CS1010. Week 03 Discussion. Introduction. Li Yi liyi@comp.nus.edu.sg. Review. Function call and activation Pass-by-value A copy of each argument value is passed to the function and stored in each input parameter. Lexical scoping

hal
Download Presentation

CS1010

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. CS1010 Week 03 Discussion

  2. Introduction Li Yi liyi@comp.nus.edu.sg

  3. Review • Function call and activation • Pass-by-value • A copy of each argument value is passed to the function and stored in each input parameter. • Lexical scoping • The range of accessibility of a variable is within the function in which the variable is declared; a function cannot refer to variables of another function. • Function return value • Function termination

  4. Q1 • Trace program execution • Mental model

  5. #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; }

  6. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; }

  7. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0

  8. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0

  9. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(a,b);

  10. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4);

  11. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4); swap b a 3 4

  12. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4); swap b a t 3 4 ?

  13. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4); swap b a t 3 4 4

  14. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4); swap b a t 3 3 4

  15. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4); swap b a t 4 3 4

  16. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4); swap b a t 3 4 4 return t;

  17. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = swap(3,4); swap b a t 3 4 4 return 4;

  18. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 0 t = 4 ; swap b a t 3 4 4 return4;

  19. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = 4 ; printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 4

  20. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 4

  21. main #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; } a b t 3 4 4

  22. #include <stdio.h> intswap( int x, int y ); intmain(void) { inta=3, b=4, t=0; printf(″%d %d %d\n″, a, b, t); t = swap(a,b); printf(″%d %d %d\n″, a, b, t); return 0; } void swap(int b, int a) { intt; t = a; a = b; b = t; return t; }

  23. Q2 (a) Write a function roundInt that takes in a double value x, and returns x rounded to the nearest integer. introundInt(double x); (b) Write a function round2 that takes in a double value x, and returns x rounded to two decimal places. double round2(double x); (c) Write a function roundK that takes in a double value x and int value k. The function returns x rounded to k decimal places. double roundK(double x, int k); http://www.acm.uiuc.edu/webmonkeys/book/c_guide

  24. Q2 Ans /* Function roundInt performs rounding to the nearest integer. Parameter: x -- floating point value to be rounded. Returns: x rounded to the nearest integer. */ introundInt(double x) { return (int)(x + 0.5); }

  25. Q2 Ans /* Function round2 performs rounding to two decimal places. Parameter: x -- floating point value to be rounded. Returns: x rounded to two decimal places. */ double round2(double x) { return ((int)((x * 100) + 0.5))/100.0; }

  26. Q2 Ans /* Function roundK performs rounding to a specifoed number of decimal places. Parameters: x -- floating point value to be rounded. k -- the number of decimal places. Returns: x rounded to k decimal places. */ double roundK(double x, int k) { double factor = pow(10,k); return ((int)((x * factor) + 0.5))/factor; }

  27. Q3

  28. Q3 Ans /* Function computeRadius computes the radius of the circle given the side of biggest square inscribed within the circle. Parameter: a -- floating-value side of the square. Returns: the radius of the circle. */ double computeRadius(double a) { double halfside = a/2; return sqrt(2 * halfside * halfside); }

  29. Q3 Ans /* Function computeAreaCircle computes the area of the circle of a given radius. Parameter: r -- floating-point radius Returns: area for the circle of radius r */ double computeAreaCircle(double r) { return PI * r * r; }

  30. Q3 Ans /* Function computeAreaCircle2 computes the area of the circle given the side of the biggest square inscribed within the circle. Parameter: floating-point side of the square. Returns: the area of the circle. */ double computeAreaCircle2(double a) { double halfSide, radiusSq, circleArea; halfSide= a/2; radiusSq= 2 * halfSide * halfSide; circleArea= PI * radiusSq; return circleArea; }

  31. Q4 • Write a function getDistance to compute and return the euclidean distance between two locations given by their Cartesian coordinates (x1, y1) and (x2, y2). double getDistance(...); • Everyday, Mr. Getalife will drive from home to office in the morning. After work, he makes a trip to the market for groceries before returning home. Suppose all three locations (home, office and market) are represented as Cartesian coordinates.Usingthe function developed in 4a, write a program to calculate the total distance traveled by Mr. Getalife.

  32. Q4 Ans double getDistance(double xStart, double yStart, double xEnd, double yEnd ) { double dist, xDiff, yDiff; //compute the difference between x and y coordinates xDiff= xStart - xEnd; yDiff= yStart - yEnd; //You can make use of another function in a function dist= sqrt(xDiff*xDiff + yDiff*yDiff); return dist; }

  33. Q5 • intreverse(int x); • int diff(int x, int y); • intremoveLastDigit(int z); • Fido dido puzzle http://www.digicc.com/fido/ intfindMissingNumber(int x);

  34. Q5 Ans int reverse(int x) { return (x%10 * 1000) + ((x/10)%10 * 100) + ((x/100)%10 * 10) + x/1000; }

  35. Q5 Ans int diff(int x, int y) { return abs(x - y); } intremoveLastDigit(int z) { return z/10; }

  36. Q5 Ans intfindMissingNumber(int x) { return 9 - (x%9); }

  37. Happy Chinese new year!

More Related