Q1. Write sum a program/function for the calculation of the sum 1 1 1 1 using do…while loop structure. 11 2 2 3 3 10 10 Ans: /*Sum of series using do…while loop */ void main() { int i=1; float sum=0; do { sum = sum + 1/(i+i); i++; } while(i<=10); cout<<“Sum = ” << sum; } Q2. Rewrite the function in correct form. Void menu(void) { int a=5, b=3, ch; cout<<“1-2: Add”<<endl; cout<<“3-4: Sub”<<endl; cout<<“Enter choice :: ”<<endl; do{ ch = getche(); switch(ch) { case 1: case 2: cout << “Addition: ” << a+b<< “\n”; case 3: case 4: cout << “Subtraction: ” << a-b<< “\n”; } }while(ch!=’1’ && ch!=’2’); } Ans: void menu(void) { int a=5, b=3, ch; cout<<“1-2. Add”<<endl; cout<<“3-4. Subtract”<<endl; cout<<“Enter choice :: ”<<endl; do{ ch = getche(); switch(ch) { case 1: case 2: cout << “Addition: ” << a+b<< “\n”; break; case 3: case 4: cout << “Subtraction: ” << a-b<< “\n”; break; of the series } }while(ch!=1 && ch!=2); } Q3. Write a program to initialise the binary pattern described by the matrix P(i,j) below. 1 0 P(i, j ) 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 Ans: void main() { int i,j, P[5][5]; /* Initialise binary image matrix */ for(i=0;i<5;i++) { for(j=0;j<5;j++) if(i==j) G[i][j]=1; else G[i][j]=0 cout<< “\n”; } }//main Q4. Rewrite the function using two formal parameters. The function should also return a value of appropriate type. void sum(void) { float a, b; cout << “Enter value for a and b:”; cin>>a>>b; float sum = a+b; cout << “Sum is: ”<<sum<<endl;; } Ans: float sum(float a, float b) { float s = a+b; return s; } Q5. Write a function for the expression F s as bs c , where s 1 a b c 2 with a, b and c as formal parameters. Define the function using pointers. Ans: float Fun(float *a, float *b, float *c) { float s=(*a+*b+*c)/2; float F= sqrt((s-*a)*(s-*b)*(s-*c)); return(F); } Q6. Call the function in Q5 with actual values x=3.3, y=4.4, and z=5.5 in the function result(), Rewrite the function result() in its correct form. void result(void) { int R=Fun(x,y,z); cout<<R<<endl; } Ans: void result(void) { float x=3.4, y=4.5, z=5.6; float R=area(&x, &y, &z); cout<<R<<endl; } Q7. A class called ‘sample’ is defined below. The method min() computes the smaller value. The method output() displays the smaller value by calling the method min() inside it. class sample { int a,b; public: void input(void) { cout<<”Input a & b here”<<endl; cin>>a >> b; } int min(void); void output(void); }; Define the method min() outside the class sample. Ans: int sample::min(void) { if(a<b) return(a); else return(b); } Q8. Define the method output() outside the class and call the function min() inside it. Ans: void sample::output(void) { cout<<”The smaller value is: ” <<min()<<endl; //calling member function } Q9. What will be the output? Explain it! char x[]=’LondoN’; i=0; while(i<3) { cout<<toupper(x[i]); i++; } cout<<”\n”; This program will convert the first 3 characters in lower case into upper case letters ‘LON’ by going from 1st character to 3rd character only. Q10. What will be the output? Explain it! /*Sum of series using while loop */ void main() { int i=9, n=10, sum=0; while (i<n){ sum = sum + i; i++; } cout<<“Sum = ” << sum; } Ans: Program will run only one iteration: i=9, sum=0+9=9, and i=9+1=10 It will quit loop as while (10<10) is false and will output 9. Q11. Write a constructor for the class below. class set { int x, y; public: void input(void) { x=1; y=2; } void output(void) {cout<<x <<y <<endl; } }; Ans class set { int x; int y; public: set(void) { x=1; y=2; } void output(void) {cout<<”-------------”<<endl; cout<<x <<y <<endl; cout<<”--------------”<<endl } }; Q12. Write a constructor that initialises x and y to zero for the class below. class set { int x, y; public: void input(void) { x=1; y=2; } void output(void) {cout<<x <<y <<endl; } }; Ans: class set { int x, y; public: void input(void) { x=1; y=2; } set(void) { x=0; y=0; } void output(void) { cout<<x <<y <<endl; } }; Q13. Create an object for the class below and display the two elements of a pair. class pair { int x; int y; public: void input(int a, int b) { x=a; y=b; } void output(void) {cout<<”-------------”<<endl; cout<<x <<y <<endl; cout<<”--------------”<<endl } }; Ans: void main() { pair A; A.input(1,2); A.output(); }