370 likes | 516 Views
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
E N D
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 • 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
Q1 • Trace program execution • Mental model
#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; }
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; }
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
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
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);
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);
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
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 ?
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
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
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
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;
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;
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;
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
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
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
#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; }
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
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); }
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; }
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; }
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); }
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; }
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; }
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.
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; }
Q5 • intreverse(int x); • int diff(int x, int y); • intremoveLastDigit(int z); • Fido dido puzzle http://www.digicc.com/fido/ intfindMissingNumber(int x);
Q5 Ans int reverse(int x) { return (x%10 * 1000) + ((x/10)%10 * 100) + ((x/100)%10 * 10) + x/1000; }
Q5 Ans int diff(int x, int y) { return abs(x - y); } intremoveLastDigit(int z) { return z/10; }
Q5 Ans intfindMissingNumber(int x) { return 9 - (x%9); }