Evaluation of the Course (Modified) Assignments: 40% Assignment: My part 20% and Dr. Lam’s part 20% MY PART: (2 assignments done in lab, 10% each) A final exam: 60%; Page 1 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Use of return statement If the type of the function is not void, at least one return statement must be used. The syntax of a return statement is: return value; Here value can be a number, variable or math expression. Examples: return 0; return x; return (x+1-2*y); Purposes: 1. Terminates the function. 2. Return a value to the parent function. Page 2 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie A tricky example of return statement #include<iostream.h> int f(int i); int f(int i) { if (i>3) void main() { { cout<<“good” int x; return i; } else x=f(5); {cout<<“bad”; } return -999; } } Page 3 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Local Variables Variables declared within the body of a function are its local variables (we also say the scope of these variables is the body of this function.) (Look at an example) Variables declared within the body of the main() function of a program is local to the main() function. A local variable is completely unknown outside its scope. Two local variables of different scopes may have the same name. Page 4 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie An Example for Local Variables #include<iostream.h> int speed(int i) int speed(int i) { int main() int y; { int x, y=100; y=2.1*i-0.15*i*i; x=speed(10); cout<< “The value of y inside speed()”<<y; cout<< “The value of y inside main”<<y; cout<<“\n”; return 0; } return y; } Output: The value of y inside speed() 6 The value of y inside main 100 Page 5 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Variables in parameter list Variables declared in the parameter list of a function are called formal variables. Their scope is the body of this function. call-by-value mechanism: by default, when a function is called, the value of the arguments are plugged in the formal parameters. More specifically, if the arguments are variables, their values not the variables themselves are plugged in. call-by-reference mechanism (will not covered in this course) Page 6 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie An Example for Local Variables #include<iostream.h> int speed(int i) int speed(int i) { int main() int y; { int x, y=100, j=10; y=2.1*i-0.15*i*i; x=speed(j); cout<< “The value of y inside speed()”<<y; cout<< “The value of y inside main”<<y; cout<<“\n”; return 0; return y; } } Output: The value of y inside speed() 6 The value of j is passed to speed(), not the variable j. The value of y inside main 100 Page 7 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Global Variables Global variable: universal for the program, declared in the program outside all functions. It can be used and changed everywhere (by any function). Try to avoid using global variables. #include<iostream.h> int g; void main() { int x,y; x=11; g=80; y= f(5); cout<<“The values of g , x and y in main()” cout<<g<<“ ”<< x<<“ ”<<y; } int f(int i) {int x=10; cout<<“The values of g and x in side f()”; cout<<g<<“ ” <<x<<“\n”; g=g+i; return g; } OUTPUT: The values of g and x in side f() 80 10 The values of g , x and y in main() 85 11 85 Page 8 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Another Example for Global Variables The roots of a quadric equation ax2+bx+c=0 are x1= (-b+( b2-4ac))/2ac x2=x1= (-b- ( b2-4ac))/2ac void roots(float a, float b, float c) { if (b*b-4*a*c>=0) { x1=(-b+sqrt(b*b-4*a*c))/(2*a*c); x2 =(-b-sqrt(b*b-4*a*c))/(2*a*c); } else flag=1; } include<iostream.h> float x1, x2; int flag=0; void roots(float a, float b, float c) void main(void) { x1, x2 and flag are used as global roots(1.0,2.0, 1.0); variables. Otherwise, roots must if(flag == 0) return two values that we do not cout<<“The roots are”<<x1<<x2; know how to do it. else cout<<“No real root”; Page 9 5/29/2016 } CS3369 Real Time Control Software/DENG Xiaotie Global Variables vs Local Variables If global variables and local variables have the same name, the local variables are valid within their scopes. #include<iostream.h> int g=30; void main() { int x,y, g; /*g is a local variable */ x=11; g=80; y= f(5); cout<<“The values of g , x and y in main()” cout<<g<<“ ”<< x<<“ ”<<y; } int f(int i) {int x=10; cout<<“The values of g and x in side f()”; cout<<g<<“ ” <<x<<“\n”; /*g is a global variable */ g=g+i; return g; } OUTPUT: The values of g and x in side f() 30 10 The values of g , x and y in main() 80 11 35 Page 10 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie while statement #include<iostream.h> void main() Syntax of while statement: { int a; cout<<“please enter an integer in [1,9]\n”; cin>>a; while (logic_expression) while (a<1 ||a>9) { { cout<<“please enter an integer in [1,9]\n”; statement1; cin>>a; } statement 2; } …. } (Demo the program in the lecture.The question was given in Lab2 as extra exercise since you have not learned while. It can be done using for in a dirty way) Page 11 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Review and Exercises variables for statement while statement if statement switch statement (optional) while statement functions Page 12 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Exercises Question 1: Which of the following words can be used as variable names? 1x11, x11, xx12, _abc, name_variable, for, if, for1, 1while. Question 2. What are the outputs of the following program? for (i=1; i<5; i++) #include<iostream.h> { void main() for(j=1; j<=3; j++) { cout<<i<<j; int i, j; cout<“\n”; } } 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Page 13 Question 3. What are the outputs of the following program? Question 4. What are the outputs of the following program? #include<iostream.h> #include<iostream.h> void main() void main() { { int i = 10; int i, j; for (i=1; i<=5; i++) if(i<0) cout <<“negative”; { if (i>100) cout<<“too large”; for (j=1; j<=i; j++) if (i>=75 && i<=100) cout<<“ ”; /* There is one space*/ cout<<“excellent”; cout <<“**** \n”; } } } What if the initial value is 88? Page 14 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Question 5. What are the outputs of the following program? Question 6. What are the outputs of the following program? #include<iostream.h> #include<iostream.h> void main() int max (int x, int y); { void main() int i; { i=1; int temp_max, temp_max1; while(i<=5) temp_max=max(x1,x2); { temp_max1=max(temp_max, x3); } } int x1=100, x2=102, x3=99; cout<<i; cout<<“The maximum is”; i=i+1; cout << temp_max1; } int max (int x, int y) { int temp=x; if (y>temp) temp=y; return temp; } 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Page 15 Question 7. What are the outputs of the following program? #include<iostream.h> int max (int x, int y, int z); void main() { Question 8. What are the outputs of the following program? #include<iostream.h> int max (int x, int y, int z); void main() { int x1=100, x2=102, x3=99; temp_max=max(99, 102, 166); int temp_max; cout<<“The maximum is”; temp_max=max(x1,x2,x3); cout<<“The maximum is”; cout << temp_max; } cout << temp_max; } int max (int x, int y, int z) { int max (int x, int y, int z) { int temp=x; cout<<“The input numbers are”<<x<<y<<z<<“\n”; int temp=x; if (y>temp) temp=y; if (y>temp) temp=y; if(z>temp) temp=z; if(z>temp) temp=z; return temp; int temp_max; return temp; } } 5/29/2016 Page 16 CS3369 Real Time Control Software/DENG Xiaotie Question 9. What are the outputs of the following program? #include<iostream.h> int computation(int x); void main() { int z, w, w1,x=10; /* x he is a variable that can be used in main() */ z=computation(5); w=computation(z); w1=computation(x); cout<<z<<w<<w1 } 5/29/2016 int computation(int x) /*x here is a formal parameter */ { int y; y=x*x+25; cout<<“input is”<<x<<“\n”; return y; } CS3369 Real Time Control Software/DENG Xiaotie Page 17 Question 10. What are the outputs of the following program? int max () { int temp=x; #include<iostream.h> cout<<“The input numbers are”; int x, int y, int z; cout <<x<<y<<z<<“\n”; int max (); if (y>temp) temp=y; void main() { if(z>temp) temp=z; int temp_max; x=90; y=91; z=z+1; z=92; return temp; cout<<z<<“\n”; temp_max=max(); } cout<<“The maximum is”; cout << temp_max; cout<<z; } 5/29/2016 Page 18 CS3369 Real Time Control Software/DENG Xiaotie Question 11. (Moderate) Write a function that takes three integers as its input parameters and outputs the smallest number among the three integers. The prototype of the function is as follows: int minmum(int x, int y, int z); Question 12. (Hard) Write a program that (1) asks the user to input an integer from the screen, (2) the input integer should be in the range [0, 100] or [200, 300], and (3) if the input integer is not in the required ranges, ask the user to re-enter the integer until the integer is in the required ranges. Page 19 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie