0% found this document useful (0 votes)
40 views

C++ Code Assessment

The document contains C++ code snippets for solving various math problems: 1) Solving quadratic equations by taking user input for coefficients a, b, c and calculating the discriminant and roots. 2) Calculating volume and surface area of a hemisphere by taking user input for diameter and calculating radius. 3) Calculating next 50 terms of a sequence by taking user input for first two terms and applying the recurrence relation formula. 4) Performing matrix addition and subtraction of 2x2 matrices by defining the matrices and iterating through them. 5) Evaluating functions like f(x)=xsin(x)-3 and f(x)=cos(x)-sin(x)-x at different points

Uploaded by

Suyash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

C++ Code Assessment

The document contains C++ code snippets for solving various math problems: 1) Solving quadratic equations by taking user input for coefficients a, b, c and calculating the discriminant and roots. 2) Calculating volume and surface area of a hemisphere by taking user input for diameter and calculating radius. 3) Calculating next 50 terms of a sequence by taking user input for first two terms and applying the recurrence relation formula. 4) Performing matrix addition and subtraction of 2x2 matrices by defining the matrices and iterating through them. 5) Evaluating functions like f(x)=xsin(x)-3 and f(x)=cos(x)-sin(x)-x at different points

Uploaded by

Suyash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Q1

(i)

#include <iostream>// This tells that we are using the C++ code

#include <math.h> // We are including the maths library

using namespace std; // We are using the standered names

// The following code can solve all the Quadratic equation

int main() {

// source code start from this line

double a,b,c,D,x1,x2; // We are defing the Values

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));

cout << "Vaue of x1 is = "<<x1<<endl;

cout << "Value of x2 is = " <<x2<<endl;

}
else if (D==0){ // we will use this formula when the value of D is zero

x1 = x2 = ((-b)/(2*a));

cout <<"Value of both the x are same that is = "<<x1<<endl;

else { // we will use this formula when the value of D is negative

x1 = ((-b + sqrt(-D))/(2*a));

x2 = ((b + sqrt(-D))/(2*a));

cout << "Vaue of x1 is = "<<x1<<endl;

cout << "Value of x2 is = " <<x2<<endl;

return 0;// The code ends hare

Q2

#include <iostream>// This tells that we are using the C++ code

#include <math.h> // We are including the maths library

using namespace std; // We are using the standered names

// The following code can calculate the surface area and the vloume of the hamisphere

int main() {

// source code start from this line

double r, v, SA, d; // We are defing the Values

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

v = (2*3.14*r*r*r/3); // We are calculating the volume

SA = 3*3.14*r*r;// We are calculating the surface area

cout<<"Volume of the hamisphere is "<< v << " unit cube"<<endl;

cout<<"Surface area of the hamisphere is "<< SA <<" unit sq."<<endl;

return 0;// The code ends hare

Q3

(i)

#include <iostream>// This tells that we are using the C++ code

#include <math.h> // We are including the maths library

using namespace std; // We are using the standered names

// The following code can calculate the next 50 terms of the sequence

int main() {

// source code start from this line

double a1, a2, a3, i; // We are deffing the Values

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;

i = 1;// Initializing the value of i

while (i<50)// Implimenting the loop for which the iteration will go on
{

cout <<a1<<endl;// Printing the vlaues of the the sequence

a3 = 2*a1 + 4*a2;// Appling the formula to get the next element of the sequence

a1 = a2;// moving the a1 element

a2 = a3;// movinf the a2 element

i++;// increasing the value of the itt for the further vlaue of the sq

return 0;// The code ends hare

(ii)

#include <iostream>// This tells that we are using the C++ code

#include <math.h> // We are including the maths library

using namespace std; // We are using the standered names

// The following code can calculate the next 50 terms of the sequence

int main() {

// source code start from this line

double a1, a2, a3, a4, i; // We are deffing the Values

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

cout <<a1<<endl;// Printing the elements of the the sequence

a4 = a1 - a2 + 4*a3 ;// Appling the formula to get the next element of the sequence

a1 = a2;// moving the a1 element

a2 = a3;// movinf the a2 element

a3 = a4;// movinf the a2 element

i++;// increasing the value of the itt for the further vlaue of the sq

return 0;// The code ends hare

Q4

(i)

#include <iostream>// This means that we are using the C++ code

using namespace std;// We are taking the name std

int main() { // Source code start from hare

// We are define the matrices A and B

int A[2][2] = {{1, 2}, {3, 4}};// Matrix A is defined

int B[2][2] = {{5, 6}, {7, 8}};// Matrix B is defined

// We are perform the matrix addition

int sum[2][2];

for (int i = 0; i < 2; ++i) {


for (int j = 0; j < 2; ++j) {

sum[i][j] = A[i][j] + B[i][j];

// We are perform the matrix subtraction

int difference[2][2];

for (int i = 0; i < 2; ++i) {

for (int j = 0; j < 2; ++j) {

difference[i][j] = A[i][j] - B[i][j];

// We are display the results of the matrix addition

cout << "A + B:" << endl;

for (int i = 0; i < 2; ++i) {

for (int j = 0; j < 2; ++j) {

cout << sum[i][j] << " ";

cout << endl;

// We are display the results of the matrix subtraction

cout << "\nA - B:" << endl;

for (int i = 0; i < 2; ++i) {


for (int j = 0; j < 2; ++j) {

cout << difference[i][j] << " ";

cout << endl;

return 0;

(ii)

#include <iostream>// This tells that we are using the C++ code

#include <math.h> // We are including the maths library

using namespace std; // We are using the standered names

// We are showing the symmetric matrix of order 4 × 4.

int main() {

int m,n; // m is the value of the row and n is the value of the column

cout << "Defint the aray of the matrix A "<<endl;

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;

for (i=1 ; i< (m+1); i++)

for (j=1; j< (n+1); j++)

cout<<"A["<<i<<"] "<<"["<<j<<"] ";

cin >> A[i][j];

cout <<"Matrix is "<<endl;// we are showing the resultent matrix to the user

for (i=1 ; i< (m+1); i++)

for (j=1; j< (n+1); j++){

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

using namespace std;

double f(double x) // we are defining a function to the program as default

double a;

a = x*sin(x) - 3;// This is the function we are defining

return a; // This is the result of the function

int main() { // Source code starts from hare

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;

v1 = f(p1); // We are calculating the value at the first point

v2 = f(p2); // We are calculationg the value at the second point

v3 = f(p3); // We are calculationg the value at the second point

cout <<"Value at the first point "<<v1<<endl;

cout <<"Value at the first point "<<v2<<endl;

cout <<"Value at the first point "<<v3<<endl;


// We have got our answer

return 0; // Source code has ended hare

(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

using namespace std;

double f(double x) // we are defining a function to the program as default

double a;

a = cos(x)- sin(x) - x ;// This is the function we are defining

return a; // This is the result of the function

int main() { // Source code starts from hare

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;

v1 = f(p1); // We are calculating the value at the first point

v2 = f(p2); // We are calculationg the value at the second point

v3 = f(p3); // We are calculationg the value at the second point


cout <<"Value at the first point "<<v1<<endl;

cout <<"Value at the first point "<<v2<<endl;

cout <<"Value at the first point "<<v3<<endl;

// We have got our answer

return 0; // Source code has ended hare

(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

using namespace std;

double f(double x) // we are defining a function to the program as default

double a;

a = 2*x*x + 5*x - 9 ;// This is the function we are defining

return a; // This is the result of the function

int main() { // Source code starts from hare

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;

v1 = f(p1); // We are calculating the value at the first point


v2 = f(p2); // We are calculationg the value at the second point

v3 = f(p3); // We are calculationg the value at the second point

cout <<"Value at the first point "<<v1<<endl;

cout <<"Value at the first point "<<v2<<endl;

cout <<"Value at the first point "<<v3<<endl;

// We have got our answer

return 0; // Source code has ended hare

(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

using namespace std;

double f(double x) // we are defining a function to the program as default

double a;

a = 2*x*x*x - 9*x - 15 ;// This is the function we are defining

return a; // This is the result of the function

int main() { // Source code starts from hare

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;

v1 = f(p1); // We are calculating the value at the first point

v2 = f(p2); // We are calculationg the value at the second point

v3 = f(p3); // We are calculationg the value at the second point

cout <<"Value at the first point "<<v1<<endl;

cout <<"Value at the first point "<<v2<<endl;

cout <<"Value at the first point "<<v3<<endl;

// We have got our answer

return 0; // Source code has ended hare

(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

using namespace std;

double f(double x) // we are defining a function to the program as default

double a;

a = tanh(x) - tan(x) ;// This is the function we are defining

return a; // This is the result of the function

int main() { // Source code starts from hare

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;

v1 = f(p1); // We are calculating the value at the first point

v2 = f(p2); // We are calculationg the value at the second point

v3 = f(p3); // We are calculationg the value at the second point

cout <<"Value at the first point "<<v1<<endl;

cout <<"Value at the first point "<<v2<<endl;

cout <<"Value at the first point "<<v3<<endl;

// We have got our answer

return 0; // Source code has ended hare

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++

// This code is in 3 pats

// 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;

int iteration = 1;//We are assigning the variable fir itt 1

double c;// We are initialzing the value c

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

} else if (func(c) * func(a) < 0) {

b = c;

} else {
a = c;

iteration++; // 1 is added to the iterrion for the next iteration

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() {

double lowerBound, upperBound, tolerance;

cout << "Enter lower bound: ";// We are asking the value of the upper bound

cin >> lowerBound; // We get the value of the upper bound

cout << "Enter upper bound: ";// We are asking the value of the upper bound

cin >> upperBound; // We get the value of the upper bound

tolerance = 0.0001; // The default value of the tolerance is set

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

return 0; }// Code ends hare

(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++

// This code is in 3 pats

// 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 - 7*x*x + 14*x - 16 ;// 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;

int iteration = 1;//We are assigning the variable fir itt 1

double c;// We are initialzing the value c

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

} else if (func(c) * func(a) < 0) {

b = c;

} else {

a = c;

iteration++; // 1 is added to the iterrion for the next iteration

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() {

double lowerBound, upperBound, tolerance;

cout << "Enter lower bound: ";// We are asking the value of the upper bound

cin >> lowerBound; // We get the value of the upper bound

cout << "Enter upper bound: ";// We are asking the value of the upper bound

cin >> upperBound; // We get the value of the upper bound

tolerance = 0.0001; // The default value of the tolerance is set

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

return 0; }// Code ends hare


RESULT OF THE CODES
Q1(i)

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)

You might also like