jubilantrich.com-questions-and-answers

    Some questions and answers in c++ programming language to help you understand some concepts.


a.      I. State the purpose of the #include pre-processor directive in C++.

ANS: The #include directive causes a copy of a specified file to be included in place of the directive. The two forms of the #include directive are; #include <filename> and #include "filename"
The #include directive is used to include standard header files such as <iostream> and <iomanip>.The #include directive is also used with programs consisting of several source files that are to be compiled together

 II. What is the purpose of default case in the switch control structure in C++?
Answer; default case is an optional case used in c++ program under switch case to execute an expression if case has no match between the controlling expression or a case label.

   III. With examples, distinguish between keyword and identifiers indicating their key characteristics.

Answer; A c++ identifier is a name used to identify a variable function, class, module, or any other user-defined item. It starts with a letter or an underscore followed by zero or more letters but must not contain punctuation characters. e.g jubilant_rich,  A_23. V etc
Keywords are reserved words in c++ which may not be used as constant or variable or any other identifier name e.g Auto, Bool, Break, case , typedef , export etc

 IV. Use a single C++ statement to demonstrate and explain how a conditional operator is used.

Answer;  (age > 18 && age < 65)?” An adult” : ”an old citizen”
“an adult” is printed as an output if the condition (age > 18 && age < 65) is true but will print “an old citizen” if the condition is false.

V. State three key motivations for modularizing a program with a function.

Answers;
·         The divide-and-conquer approach
·         Help in software reuse
·         To avoid repeating of codes
·         Easier to debug and maintain

b. I Examine the following block of if….else statements and indicate if there is any problem with its structure. Give suggestions on how to solve the problem if any exists.
If(agelower>12)
 If(ageupper<20)
           Cout<<”You are a teenager”;
else
        Cout<<”You are below 13 years”;
ANSWER; The block statement supposed to be if …else statement but not nested if statement. So we can use this instead of that
If(agelower>12 && ageupper <20)
Cout<<”you are a teenager”;
else
Cout<<”You are below 13 year”;

II. Examine the following program and
i.                    Identify syntactic errors
ii.                  Write down the output of the program in the order in which they appear if all errors are corrected.

#include <iostream>
#include<string>
 Using namespace std.

Int main()
String s=”I’m see plus-plus.”
String s1 =”and you are?”;

Int c =7,q=6;
   
    cout <<”\t”<<c++<<”\t”<<--c<<”\t<<q--<<”\t”<<c%q<<end;
                   cout<<endl;
               if(s. length=19)
   cout<<s1.earase(9,4)<<endl;
cout<<s1. Insert(8,” java)<<endl;
return 0;
}
Answer; (i)
#include <iostream>
#include<string>
 Using namespace std;

Int main(){
String s=”I’m see plus-plus.”;
String s1 =”and you are?”;

Int c =7,q=6;
   
    cout <<”\t”<<c++<<”\t”<<--c<<”\t<<q--<<”\t”<<c%q<<end;
                   cout<<endl;
               if(s. length()==19)
   cout<<s1.erase(9,4)<<endl;
cout<<s1. Insert(8,” java)<<endl;
return 0;
}

The syntaxes marked color read were omitted in the question creating syntax error

Output of the program after errors correctly checked.
  And your?
And you javar?

III. Rewrite the following expression as a C++ arithmetic expression and state the order in which it would be executed by a c++ compiler.

() function call is considered or executed first always considered first
Brackets are also considered or computed after function
Within each brackets / (division), *(multiplication) are considered from left to right
 + (addition) is considered after / and *
= (equal) Is executed last

2.      a. I. Using a simple C++ program explain, “Function overloading” and indicate all conditions required.


Answer
jubilantrich.com-overloading-in-c++
C++ enables several function of the same name like sum() in the example program above to be defined and executed as long as they both have diff data types and diff order of arguments in the call.
Conditions required are;
·         The functions must have different data types
·         Even though they have the same function name, its arguments must not be the same. 

        II. At what point in C++programming are logical operators mostly used.

ANSWER  At a point where our c++ program involves Boolean expression
a.           Write a program to compute the factorial of an integer n using the do…while control structure. As part of the program, write a function that accepts the factorial f as an argument if f is less than or equal to 50 or otherwise, the function should accept n as the argument and compute the Fibonacci value of or using the recursion technique.
Answer
jubilantrich.com-logical-operators


1.      a. I. Use a simple C++ program to illustrate how to pass an argument by reference to a function and state one merit and demerit of passing argument by reference.

1.      #include <iostream>
2.      using namespace std;
3.      int main ()
4.      {
5.      // declare simple variables
6.      int  i;
7.      double d;
8.      // declare reference variables
9.      int&  r = i;
10.  double& s = d;
11.  i = 5;
12.  cout << "Value of i : " << i << endl;
13.  cout << "Value of i reference : " << r  << endl;
14.  d = 11.7;
15.  cout << "Value of d : " << d << endl;
16.  cout << "Value of d reference : " << s  << endl;
17.  return 0;
18.  }
19.   

Merits
·         It allows us to have the function change the value of the argument
·         Because of copy of the argument is not made, it is fast, even when used with large classes
·         We can pass by const reference to avoid unintentional changes.
·         We can return multiple values from a function
Demerit
  • ·         Reference arguments cannot be any variable apart from normal variable
  • ·         Programmer might not release if the function will change the value of the argument, since arguments passed by value and by reference looks alike


b. Write a program that reads a given pair (x,y) of real numbers and computes the least-square regression line for the data. Given that ymx+b, where m=
m=
b=-m,  and  are the averages respectively
As part of the above program, write a function that accepts all the values of as one (1) dimensional array and compute the average of the values.

1  #include <iostream>
2
3  using namespace std;
4  double  average_X, mean_Y, m, b, y,arr,sum,sumx,a,ave ,sum_X, sum_Y, sum_XX, sum_XY;
5  int i;
6  inline double averagex(double x[], int a){
7
8    sum=0;
9    sumx+=x[i];
10    ave=sumx/a;
11    return ave;
12    }
13  int main()
14  {  // BSc COMPUTER SCIENCE
15  double x, arr1;
16  sum_X=0;
17  sum_Y=0;
18  sum_XX=0;
19  sum_XY=0;
20  cout<< "Enter the number of paired values: ";
21  cin >>a;
22  cout  <<"Enter "<<a << " pairs:\n ";
23  for (int  i = 1; i <= a; i++)
24  {
25
26  cout<<"pair "<<i<<": ";
27  cin>> x  >>y;
28
29  sum_X += x;
30  sum_Y += y;
31  sum_XX += x*x;
32  sum_XY += x*y;
33
34  arr=( [i],  a);
35  }
36  average_X = sum_X/a;
37  mean_Y = sum_Y/a;
38  m = (sum_XY - mean_Y*sum_X)/(sum_XX -average_X*sum_X);
39  b =  mean_Y- average_X*m;
40  cout << "The least squares regression line is:\n";
41  cout<<"Y = "<< m <<" X + "<<"("<< b<<")" <<endl;
42  cout<<endl;
43
44  cout<<"average of x ="<<arr<<endl;
45  }
46

If it doesn’t run remove line 34 and from line 6 to 12 (the function)

4. a. I. Briefly outline three limitations of structured procedural programming when used for larger or complex programs and state how objects-oriented programming concepts such as encapsulation, inheritance and polymorphism addresses some of these limitations.

        II. C++ is case sensitive.  Discuss.     
        III. State two (2) similarities and dissimilarities of iteration and recursion as used in C++ programming.
b. Write a C++ program to find the roots of a quadratic equation of the form  The program should be able to return all possible roots, including imaginary roots of the equation. Given that the absolute values of the roots of the equation denotes, the radius (r) of a circle is if the roots are non-zero, real and equal or the vertical height (h) and radius (r) of a cone if they are non-zero, real and distinct. As part of the program write two functions that accept the absolute values of the roots and as an argument and computes
i.        The circumference of the circle (only pass one root as arguments, since the roots are equal). The circumference of circle is given as; C=2πr and π=3.142

ii.      The surface area of the cone (pass both roots for h and r as arguments). The surface area of a cone is SA=π+πrl, where l=.
Answer to question 4 b.

1  #include <iostream>
2  #include <cmath>
3
4  using namespace std;
5  double l,SA,radius,height;
6  double pi=3.142;
7  long double circircle(double r,double pi);
8  long double surfaceArea(double h, double r);
9  main(){
10  double x1,x2,a,b,c,d;
11  cout<<"enter the values for a ";
12  cin>>a;
13  cout<<"enter the values for b ";
14  cin>>b;
15  cout<<"enter the values for c ";
16  cin>>c;
17  d=(b*b)-(4*a*c);
18  x1=(-b+sqrt(d))/2*a;
19  x2=(-b-sqrt(d))/2*a;
20  if (d==0){
21  if (x1!=0&&x2!=0)
22    cout<<"roots are real and equal\n";
23  cout<<"x1= "<<x1<<endl;
24  cout<<"x2= "<<x2<<endl;
25
26  radius=abs(x1);
27  cout<<"circumference of a circle =";
28
29  cout<<circircle( radius, pi);
30  }
31  else if (d>0){
32  if (x1!=0&&x2!=0)
33  cout<<"roots are real and distinct\n";
34  cout<<"x1= "<<x1<<endl;
35  cout<<"x2= "<<x2<<endl;
36
37  height=abs(x1);
38  radius=abs(x2);
39  cout<<"surface area =";
40  cout<<surfaceArea( height,  radius);
41
42  }
43  else if (d<0){
44  cout<<"imaginary roots\n";
45  cout<<"x1= "<<x1<<endl;
46  cout<<"x2= "<<x2<<endl;
47  }
48  }
49  long double circircle(double r,double pi){
50  return 2*pi*r;
51  }
52  long double surfaceArea(double r, double h){
53  l=sqrt(r*r+h*h);
54  SA=(pi*r*r)+(pi*r*l);
55  return SA;
56  }
                                                                                            

Thank you for reading.