Exercises Question 1: Which of the following words can be used as variable names? 1x11, x11, xx12, _abc, name_variable, for, if, for1, 1while. Answer: x11, xx12, abc, name_variable, for1 can be used for names of variables. Question 2. What are the outputs of the following program? #include<iostream.h> void main() Answer: 1 1 1 2 1 3 { 212223 int i, j; 313233 for (i=1; i<5; i++) 414243 { 515253 for(j=1; j<=3; j++) cout<<i<<j; cout<“\n”; } } Page 1 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie 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? Answer of Q4: **** Answer of Q3: Nothing is output. If the initial value of I is 88, the output is : **** **** **** excellent **** 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie Page 2 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; { int x1=100, x2=102, x3=99; i=1; int temp_max, temp_max1; while(i<=5) temp_max=max(x1,x2); { temp_max1=max(temp_max, x3); cout<<i; cout<<“The maximum is”; i=i+1; cout << temp_max1; } } } int max (int x, int y) { int temp=x; Answer of Q5: 1 2 3 4 5 if (y>temp) temp=y; return temp; } 5/29/2016 CS3369 Real Time Control Answer: The maximum is 102. Software/DENG Xiaotie Page 3 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) { if (y>temp) temp=y; if (y>temp) temp=y; if(z>temp) temp=z; if(z>temp) temp=z; } 5/29/2016 Answer: The maximum is 102 int temp=x; cout<<“The input numbers are”<<x<<y<<z<<“\n”; int temp=x; return temp; int temp_max; return temp; } Answer: The input numbers are 99 102 166 CS3369 Real Time TheControl maximum is 166 Software/DENG Xiaotie Page 4 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; } Answer: input is 5 CS3369 Real Time Control Software/DENG Xiaotie input is 50 input is 10 50 2525 125 Page 5 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 <<“\n”; Answer: 92 The input numbers are 90 91 92 cout<<z; The maximum is 92 } 5/29/2016 CS3369 Real Time Control 93 Software/DENG Xiaotie Page 6 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: #include<iostream.h> int min (int x, int y, int z); cout<<“Please enter an integer in the ranges [0, 100] or [200,300]\n”; Answer: int min (int x, int y, int z) void main() { int n; cin>>n; { int temp =x; while (n<0|| n>100&&n<200 ||n>300) if (y<temp) temp =y; { cout<<“Please enter an integer in the ranges [0, 100] or [200,300]\n”; if (z<temp) temp=z; return temp; cin>>n; } 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 7 5/29/2016 CS3369 Real Time Control Software/DENG Xiaotie