C++ Code Assessment
C++ Code Assessment
(i)
#include <iostream>// This tells that we are using the C++ code
int main() {
cout<< "Enter the value of a,b,c, accoring to ax^2 + bx + c"<<endl; // we are telling the user to submit
the vlaues of a,b and c according to the Quadratic Equation
cout<<"Enter the first coficient ";// We are asking the user to submit the vlaue of a According to the
Quadratic Equation
cin>>a;
cout<<endl<<"Enter the second coficient ";// We are asking the user to submit the vlaue of a
According to the Quadratic Equation
cin>>b;
cout<<"Enter the third coficient ";// We are asking the user to submit the vlaue of a According to the
Quadratic Equation
cin>>c;
D = ((b*b)-(4*a*c)); // we are finding the value of D to find the solution of the equation
if (D > 0){ // we will use this formula when the value of D is positive
x1 = ((-b + sqrt(D))/(2*a));
x2 = ((b + sqrt(D))/(2*a));
}
else if (D==0){ // we will use this formula when the value of D is zero
x1 = x2 = ((-b)/(2*a));
x1 = ((-b + sqrt(-D))/(2*a));
x2 = ((b + sqrt(-D))/(2*a));
Q2
#include <iostream>// This tells that we are using the C++ code
// The following code can calculate the surface area and the vloume of the hamisphere
int main() {
cout<<"Enter the diameter of the hamisphere ";// We are asking the user to submit the vlaue of the
radius
cin>>d;
r = d/2;// We are calculating the radius
Q3
(i)
#include <iostream>// This tells that we are using the C++ code
// The following code can calculate the next 50 terms of the sequence
int main() {
cout<<"Enter the first value of the sequence ";// We are asking the user to submit the first vlaue of
the sequence
cin>>a1;
cout<<"Enter the second value of the sequence ";// We are asking the user to submit the second
vlaue of the sequence
cin>>a2;
while (i<50)// Implimenting the loop for which the iteration will go on
{
a3 = 2*a1 + 4*a2;// Appling the formula to get the next element of the sequence
i++;// increasing the value of the itt for the further vlaue of the sq
(ii)
#include <iostream>// This tells that we are using the C++ code
// The following code can calculate the next 50 terms of the sequence
int main() {
cout<<"Enter the first element of the sequence ";// We are asking the user to submit the first element
of the sequence
cin>>a1;
cout<<"Enter the second element of the sequence ";// We are asking the user to submit the second
element of the sequence
cin>>a2;
cout<<"Enter the third element of the sequence ";// We are asking the user to submit the third
element of the sequence
cin>>a3;
i = 1;// Initializing the value of i
while (i<50)// Implimenting the loop for which the iteration will go on
a4 = a1 - a2 + 4*a3 ;// Appling the formula to get the next element of the sequence
i++;// increasing the value of the itt for the further vlaue of the sq
Q4
(i)
#include <iostream>// This means that we are using the C++ code
int sum[2][2];
int difference[2][2];
return 0;
(ii)
#include <iostream>// This tells that we are using the C++ code
int main() {
int m,n; // m is the value of the row and n is the value of the column
cout <<"Value of the row ";// Asking for enter the no. of rows that has to be shown
cin >>m;
cout <<"Value of the column ";// Asking for enter the no. of column that has to be shown
cin>>n;
cout << "Enter the elemnts of the matrix"<<endl;//// Asking for enter the elements of the matrix
int A[m][n],i,j;
cout <<"Matrix is "<<endl;// we are showing the resultent matrix to the user
cout<<A[i][j]<<" ";
cout<<endl;
return 0;
}
Q5
(i)
#include <iostream> // This shows that we are using the C++ program
#include <math.h> // This includes all the functions of the maths into the program
double a;
double p1, p2, p3, v1, v2, v3; // We are defining 4 values which is 2 point and there solution
cout<<"Enter the first point "<<endl; // We are asking for the first vaule
cin>>p1;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p2;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p3;
(ii)
#include <iostream> // This shows that we are using the C++ program
#include <math.h> // This includes all the functions of the maths into the program
double a;
double p1, p2, p3, v1, v2, v3; // We are defining 4 values which is 2 point and there solution
cout<<"Enter the first point "<<endl; // We are asking for the first vaule
cin>>p1;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p2;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p3;
(iii)
#include <iostream> // This shows that we are using the C++ program
#include <math.h> // This includes all the functions of the maths into the program
double a;
double p1, p2, p3, v1, v2, v3; // We are defining 4 values which is 2 point and there solution
cout<<"Enter the first point "<<endl; // We are asking for the first vaule
cin>>p1;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p2;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p3;
(iv)
#include <iostream> // This shows that we are using the C++ program
#include <math.h> // This includes all the functions of the maths into the program
double a;
double p1, p2, p3, v1, v2, v3; // We are defining 4 values which is 2 point and there solution
cout<<"Enter the first point "<<endl; // We are asking for the first vaule
cin>>p1;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p2;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p3;
(v)
#include <iostream> // This shows that we are using the C++ program
#include <math.h> // This includes all the functions of the maths into the program
double a;
double p1, p2, p3, v1, v2, v3; // We are defining 4 values which is 2 point and there solution
cout<<"Enter the first point "<<endl; // We are asking for the first vaule
cin>>p1;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p2;
cout<<"Enter the second point "<<endl;// We are asking for the Second vaule
cin>>p3;
Q6
(i)
#include <iostream> // This shows that we are using the C++ code
#include <math.h> // This statement helps by including the additional maths library
using namespace std; // We are using the standered name for all the default functions of the C++
// The first part defines the function for which we are finding the solution in the given interval it is from
statement 7 to 9
double func(double x /* We have defined the variable x for which we are findind the solution*/ ) {
return x * x * x + 4*x*x - 10;// This is the function
// The second part defins the type of meathod we will use to find the solution of the equation for this
we are using the bisection meathod
void bisection(double a/* this is the lower bound*/, double b/*this is the upper bound*/, double tol/*
This is the tol lvel for which the iteration will go on */) {
if (func(a) * func(b) >= 0) { /* This is the first condition for which the roots dont lie in the given
interval*/
cout << "Bisection method cannot be applied on this interval." << endl;// We will tell the user to
infut the correct intervsl
return;
while ((b - a) >= tol) { // We are defining the condition hare for which the code will start running
c = (a + b) / 2;// we will find the middle value of a and b and assign it to c at every iteration
cout << "Iteration " << iteration << ": x = " << c << ", f(x) = " << func(c) << endl;//for each iteration
the value of x and f(x) will be shown to the user
// The following part will assign the new interval for the next iteration
if (func(c) == 0.0) {
break;// But if the value of f(x) is 0 then it means that we ahve find the solution of the equation
b = c;
} else {
a = c;
cout << "Approximate root is: " << c << endl; } // After the tol is hit the itaration stops and the
approximate root is shown
// This is the third and the final part in which wr ask the values from the user
int main() {
cout << "Enter lower bound: ";// We are asking the value of the upper bound
cout << "Enter upper bound: ";// We are asking the value of the upper bound
bisection(lowerBound, upperBound, tolerance); // We have assigned the values that we have got from
the user to the function that we have defined in the second part the the certain format
(ii)
#include <iostream> // This shows that we are using the C++ code
#include <math.h> // This statement helps by including the additional maths library
using namespace std; // We are using the standered name for all the default functions of the C++
// The first part defines the function for which we are finding the solution in the given interval it is from
statement 7 to 9
double func(double x /* We have defined the variable x for which we are findind the solution*/ ) {
// The second part defins the type of meathod we will use to find the solution of the equation for this
we are using the bisection meathod
void bisection(double a/* this is the lower bound*/, double b/*this is the upper bound*/, double tol/*
This is the tol lvel for which the iteration will go on */) {
if (func(a) * func(b) >= 0) { /* This is the first condition for which the roots dont lie in the given
interval*/
cout << "Bisection method cannot be applied on this interval." << endl;// We will tell the user to
infut the correct intervsl
return;
while ((b - a) >= tol) { // We are defining the condition hare for which the code will start running
c = (a + b) / 2;// we will find the middle value of a and b and assign it to c at every iteration
cout << "Iteration " << iteration << ": x = " << c << ", f(x) = " << func(c) << endl;//for each iteration
the value of x and f(x) will be shown to the user
// The following part will assign the new interval for the next iteration
if (func(c) == 0.0) {
break;// But if the value of f(x) is 0 then it means that we ahve find the solution of the equation
b = c;
} else {
a = c;
cout << "Approximate root is: " << c << endl; } // After the tol is hit the itaration stops and the
approximate root is shown
// This is the third and the final part in which wr ask the values from the user
int main() {
cout << "Enter lower bound: ";// We are asking the value of the upper bound
cout << "Enter upper bound: ";// We are asking the value of the upper bound
bisection(lowerBound, upperBound, tolerance); // We have assigned the values that we have got from
the user to the function that we have defined in the second part the the certain format
Q1(ii)
Q1(iii)
Q2
Q3(i)
Q3(ii)
Q4(i)
Q4(ii)
Q5(i)
Q5(ii)
Q5(iii)
Q5(iv)
Q5(v)
Q6(i)
Q6(ii)