Download presentation
Presentation is loading. Please wait.
1
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. 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() { int i, j; for (i=1; i<5; i++) for(j=1; j<=3; j++) cout<<i<<j; cout<“\n”; } Answer: 5/1/2019 CS3369 Real Time Control Software/DENG Xiaotie
2
CS3369 Real Time Control Software/DENG Xiaotie
Question 3. What are the outputs of the following program? #include<iostream.h> void main() { int i = 10; if(i<0) cout <<“negative”; if (i>100) cout<<“too large”; if (i>=75 && i<=100) cout<<“excellent”; } What if the initial value is 88? Answer of Q3: Nothing is output. If the initial value of I is 88, the output is : excellent Question 4. What are the outputs of the following program? #include<iostream.h> void main() { int i, j; for (i=1; i<=5; i++) { for (j=1; j<=i; j++) cout<<“ ”; /* There is one space*/ cout <<“**** \n”; } Answer of Q4: **** **** 5/1/2019 CS3369 Real Time Control Software/DENG Xiaotie
3
CS3369 Real Time Control Software/DENG Xiaotie
Question 5. What are the outputs of the following program? #include<iostream.h> void main() { int i; i=1; while(i<=5) cout<<i; i=i+1; } Answer of Q5: Question 6. What are the outputs of the following program? #include<iostream.h> int max (int x, int y); void main() { int x1=100, x2=102, x3=99; int temp_max, temp_max1; temp_max=max(x1,x2); temp_max1=max(temp_max, x3); cout<<“The maximum is”; cout << temp_max1; } int max (int x, int y) { int temp=x; if (y>temp) temp=y; return temp; Answer: The maximum is 102. 5/1/2019 CS3369 Real Time Control Software/DENG Xiaotie
4
CS3369 Real Time Control Software/DENG Xiaotie
Question 7. 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; int temp_max; temp_max=max(x1,x2,x3); cout<<“The maximum is”; cout << temp_max; } int max (int x, int y, int z) { int temp=x; if (y>temp) temp=y; if(z>temp) temp=z; return temp; Answer: The maximum is 102 Question 8. What are the outputs of the following program? #include<iostream.h> int max (int x, int y, int z); void main() { int temp_max; temp_max=max(99, 102, 166); cout<<“The maximum is”; cout << temp_max; } int max (int x, int y, int z) { int temp=x; cout<<“The input numbers are”<<x<<y<<z<<“\n”; if (y>temp) temp=y; if(z>temp) temp=z; return temp; Answer: The input numbers are The maximum is 166 5/1/2019 CS3369 Real Time Control Software/DENG Xiaotie
5
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 } 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 input is 50 input is 10 5/1/2019 CS3369 Real Time Control Software/DENG Xiaotie
6
CS3369 Real Time Control Software/DENG Xiaotie
Question 10. What are the outputs of the following program? #include<iostream.h> int x, int y, int z; int max (); void main() { int temp_max; x=90; y=91; z=92; cout<<z<<“\n”; temp_max=max(); cout<<“The maximum is”; cout << temp_max <<“\n”; cout<<z; } int max () { int temp=x; cout<<“The input numbers are”; cout <<x<<y<<z<<“\n”; if (y>temp) temp=y; if(z>temp) temp=z; z=z+1; return temp; } Answer: 92 The input numbers are The maximum is 92 93 5/1/2019 CS3369 Real Time Control Software/DENG Xiaotie
7
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 min (int x, int y, int z); Answer: int min (int x, int y, int z) { int temp =x; if (y<temp) temp =y; if (z<temp) temp=z; return temp; } 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. #include<iostream.h> void main() { int n; cout<<“Please enter an integer in the ranges [0, 100] or [200,300]\n”; cin>>n; while (n<0|| n>100&&n<200 ||n>300) { } 5/1/2019 CS3369 Real Time Control Software/DENG Xiaotie
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.