1 / 7

Exercises

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?

abertrand
Download Presentation

Exercises

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. 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: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 4 1 4 2 4 3 5 1 5 2 5 3 CS3369 Real Time Control Software/DENG Xiaotie

  2. 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: **** **** **** **** **** CS3369 Real Time Control Software/DENG Xiaotie

  3. 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: 1 2 3 4 5 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. CS3369 Real Time Control Software/DENG Xiaotie

  4. 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 99 102 166 The maximum is 166 CS3369 Real Time Control Software/DENG Xiaotie

  5. 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 50 2525 125 CS3369 Real Time Control Software/DENG Xiaotie

  6. 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 90 91 92 The maximum is 92 93 CS3369 Real Time Control Software/DENG Xiaotie

  7. 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) { cout<<“Please enter an integer in the ranges [0, 100] or [200,300]\n”; cin>>n; } } CS3369 Real Time Control Software/DENG Xiaotie

More Related