In: Computer Science
Can someone covert the code into C language
#include<iostream>
#include<iomanip>
#include<ios>
using namespace std;
/********************************************************************************
Function name: main
Purpose:
main function
In parameters: b,r,i
Out paramters: trun,error,total,value
Version:
1.0
Author:
********************************************************************************/
void main()
{
int i;//declaring this variable to get value for
quitting or calaculating series
do {//do while loop to calaculate series until user
quits
cout << "Enter 1 to evaluate
the series." << endl;
cout << "Enter 2 to quit"
<< endl << endl;
cin >> i;
if (i == 2)
{
cout <<
"Exiting..." << endl;
exit(0);
}
else if (i < 1 || i> 2 )//if
the user enters any value except 1 or 2
{
cout <<
"Please enter a valid input." << endl;
}
else
{
double
b;//declared so user will enter proper number of terms
do {//do while
loop to ensure user enters proper number of terms
cout << "Please enter the number of
(non-zero) terms in the series (1,2,3,4,5,6)." << endl;
cin >> b;
cout << endl;
if (b < 1 || b>6)//if the user entered a
number that was not between 1-6
cout << "Incorrect
number of terms." << endl;
} while (b <
1 || b>6);
double
r;//declared to get range in between 0 and 0.9.
do {
cout << "Please enter the range of B to
evaluate in increments (0.0 < B >=0.9)." << endl;
cin >> r;
if (r < 0.1 || r>0.9)//if entered number
was smaller than 0 or greater than 0.9
cout << "Incorrect
range." << endl;
} while (r <
0.1 || r>0.9);
cout <<
endl;
cout <<
"MACLAURIN SERIES " << endl;
cout << "
B V(B) Series V(B) Exact Relative Error %RSerE" <<
endl;
cout << "0
1 1 0 0" << endl;
for (double x =
r / 10; x <= r; x = x + r / 10)//this loop while calculate
series in 10 increments
{
double value = 1 / sqrt(1 - (x *
x));//calculating exact value of series
double total = 1;//variable to hold total of
series,adding 1 because first value of series will always be
1
for (int j = 2; j <= b; j++)//calculating
each term in the series
{
if (j == 2)
{
total +=
(x * x) / 2;//factorials have already been calcuated and each term
has been simplified,here we are adding value of each term
}
else if (j == 3)
{
total +=
(x * x * x * x * 3) / 8;
}
else if (j == 4)
{
total +=
(x * x * x * x * x * x * 5) / 16;
}
else if (j == 5)
{
total +=
(x * x * x * x * x * x * x * x * 35) / 128;
}
else if (j == 6)
{
total +=
(x * x * x * x * x * x * x * x * x * x * 63) / 256;
}
}
double error = (value - total) /
value;//calcuating relative error
double trun = 0;//variable to hold truncative
error
if (b + 1 == 2)//these if statements will sue
the first truncative term to find truncative error
{
trun = (x * x) / 2;
trun = trun * 100 /
value;
}
else if (b + 1 == 3)
{
trun = (x * x * x * x * 3) /
8;
trun = trun * 100 /
value;
}
else if (b + 1 == 4)
{
trun = (x * x * x * x * x * x
* 5) / 16;
trun = trun * 100 /
value;
}
else if (b + 1 == 5)
{
trun = (x * x * x * x * x * x
* x * x * 35) / 128;
trun = trun * 100 /
value;
}
else if (b + 1 == 6)
{
trun = (x * x * x * x * x * x
* x * x * x * x * 63) / 256;
trun = trun * 100 /
value;
}
else
{
trun = (x * x * x * x * x * x
* x * x * x * x * x * x * 10395) / 46080;
trun = trun * 100 /
value;
}
printf("%g", x);//print each increment
std::cout << std::fixed;//so the values
wont be rounded
std::cout << std::setprecision(10);//so
the output of each double will be to 10 decimal place
cout << " " << total << " "
<< value << " ";
std::cout << std::defaultfloat;//to get
output in scientific as well as non scientific if required
cout << error << " ";
cout << trun << endl;
}
cout <<
endl;
}
} while (i != 2);//this loop will end when user
presses 1
}
The answer to the above problem is as follows:
C CODE-
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
/********************************************************************************
Function name: main
Purpose: main function
In parameters: b,r,i
Out paramters: trun,error,total,value
Version: 1.0
Author:
********************************************************************************/
void main()
{
int i;//declaring this variable to get value for quitting or
calaculating series
do {//do while loop to calaculate series until user quits
printf("Enter 1 to evaluate the series.\n" );
printf("Enter 2 to quit\n\n");
scanf("%d",&i);
if (i == 2)
{
printf("Exiting...\n");
exit(0);
}
else if (i < 1 || i> 2 )//if the user enters any value except
1 or 2
{
printf("Please enter a valid input.\n");
}
else
{
double b;//declared so user will enter proper number of terms
do {//do while loop to ensure user enters proper number of
terms
printf("Please enter the number of (non-zero) terms in the series
(1,2,3,4,5,6).\n");
scanf("%lf",&b);
printf("\n");
if (b < 1 || b>6)//if the user entered a number that was not
between 1-6
printf("Incorrect number of terms.\n");
} while (b < 1 || b>6);
double r;//declared to get range in between 0 and 0.9.
do {
printf("Please enter the range of B to evaluate in increments (0.0
< B >=0.9).\n");
scanf("%lf",&r);
if (r < 0.1 || r>0.9)//if entered number was smaller than 0
or greater than 0.9
printf("Incorrect range.\n");
} while (r < 0.1 || r>0.9);
printf("\n");
printf("MACLAURIN SERIES \n");
//use %% in printf() statements to display a '%' sign
printf(" B V(B) Series V(B) Exact Relative Error %%RSerE\n");
printf("0 1 1 0 0\n");
for (double x = r / 10; x <= r; x = x + r / 10)//this loop while
calculate series in 10 increments
{
double value = 1 / sqrt(1 - (x * x));//calculating exact value of
series
double total = 1;//variable to hold total of series,adding 1
because first value of series will always be 1
for (int j = 2; j <= b; j++)//calculating each term in the
series
{
if (j == 2)
{
total += (x * x) / 2;//factorials have already been calcuated and
each term has been simplified,here we are adding value of each
term
}
else if (j == 3)
{
total += (x * x * x * x * 3) / 8;
}
else if (j == 4)
{
total += (x * x * x * x * x * x * 5) / 16;
}
else if (j == 5)
{
total += (x * x * x * x * x * x * x * x * 35) / 128;
}
else if (j == 6)
{
total += (x * x * x * x * x * x * x * x * x * x * 63) / 256;
}
}
double error = (value - total) / value;//calcuating relative
error
double trun = 0;//variable to hold truncative error
if (b + 1 == 2)//these if statements will sue the first truncative
term to find truncative error
{
trun = (x * x) / 2;
trun = trun * 100 / value;
}
else if (b + 1 == 3)
{
trun = (x * x * x * x * 3) / 8;
trun = trun * 100 / value;
}
else if (b + 1 == 4)
{
trun = (x * x * x * x * x * x * 5) / 16;
trun = trun * 100 / value;
}
else if (b + 1 == 5)
{
trun = (x * x * x * x * x * x * x * x * 35) / 128;
trun = trun * 100 / value;
}
else if (b + 1 == 6)
{
trun = (x * x * x * x * x * x * x * x * x * x * 63) / 256;
trun = trun * 100 / value;
}
else
{
trun = (x * x * x * x * x * x * x * x * x * x * x * x * 10395) /
46080;
trun = trun * 100 / value;
}
printf("%g", x);//print each increment
//to print values upto 10 decimal places we use %.10f as format
specifier
//and to ensure that value is not rounded, we use the trunc()
method of math class for 10 decimal places
printf(" %.10lf %.10lf
",trunc(total*10000000000.0)/10000000000.0,trunc(value*10000000000.0)/10000000000.0);
//display values in scientific notation if required by using %.10e
format specifier
printf("%.10e %.10e\n",error,trun);
}
printf("\n");
}
} while (i != 2);//this loop will end when user presses 1
}
IMAGE OF CODE-
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
/********************************************************************************
Function name: main
Purpose: main function
In parameters: b,r,i
Out paramters: trun,error,total,value
Version: 1.0
Author:
********************************************************************************/
void main()
{
int i;//declaring this variable to get value for quitting or calaculating series
do {//do while loop to calaculate series until user quits
printf("Enter 1 to evaluate the series.\n" );
printf("Enter 2 to quit\n\n");
scanf("%d",&i);
if (i == 2)
{
printf("Exiting...\n");
exit(0);
}
else if (i < 1 || i> 2 )//if the user enters any value except 1 or 2
{
printf("Please enter a valid input.\n");
}
else
{
double b;//declared so user will enter proper number of terms
do {//do while loop to ensure user enters proper number of terms
printf("Please enter the number of (non-zero) terms in the series (1,2,3,4,5,6).\n");
scanf("%lf",&b);
printf("\n");
if (b < 1 || b>6)//if the user entered a number that was not between 1-6
printf("Incorrect number of terms.\n");
} while (b < 1 || b>6);
double r;//declared to get range in between 0 and 0.9.
do {
printf("Please enter the range of B to evaluate in increments (0.0 < B >=0.9).\n");
scanf("%lf",&r);
if (r < 0.1 || r>0.9)//if entered number was smaller than 0 or greater than 0.9
printf("Incorrect range.\n");
} while (r < 0.1 || r>0.9);
printf("\n");
printf("MACLAURIN SERIES \n");
//use %% in printf() statements to display a '%' sign
printf(" B V(B) Series V(B) Exact Relative Error %%RSerE\n");
printf("0 1 1 0 0\n");
for (double x = r / 10; x <= r; x = x + r / 10)//this loop while calculate series in 10 increments
{
double value = 1 / sqrt(1 - (x * x));//calculating exact value of series
double total = 1;//variable to hold total of series,adding 1 because first value of series will always be 1
for (int j = 2; j <= b; j++)//calculating each term in the series
{
if (j == 2)
{
total += (x * x) / 2;//factorials have already been calcuated and each term has been simplified,here we are adding value of each term
}
else if (j == 3)
{
total += (x * x * x * x * 3) / 8;
}
else if (j == 4)
{
total += (x * x * x * x * x * x * 5) / 16;
}
else if (j == 5)
{
total += (x * x * x * x * x * x * x * x * 35) / 128;
}
else if (j == 6)
{
total += (x * x * x * x * x * x * x * x * x * x * 63) / 256;
}
}
double error = (value - total) / value;//calcuating relative error
double trun = 0;//variable to hold truncative error
if (b + 1 == 2)//these if statements will sue the first truncative term to find truncative error
{
trun = (x * x) / 2;
trun = trun * 100 / value;
}
else if (b + 1 == 3)
{
trun = (x * x * x * x * 3) / 8;
trun = trun * 100 / value;
}
else if (b + 1 == 4)
{
trun = (x * x * x * x * x * x * 5) / 16;
trun = trun * 100 / value;
}
else if (b + 1 == 5)
{
trun = (x * x * x * x * x * x * x * x * 35) / 128;
trun = trun * 100 / value;
}
else if (b + 1 == 6)
{
trun = (x * x * x * x * x * x * x * x * x * x * 63) / 256;
trun = trun * 100 / value;
}
else
{
trun = (x * x * x * x * x * x * x * x * x * x * x * x * 10395) / 46080;
trun = trun * 100 / value;
}
printf("%g", x);//print each increment
//to print values upto 10 decimal places we use %.10f as format specifier
//and to ensure that value is not rounded, we use the trunc() method of math class for 10 decimal places
printf(" %.10lf %.10lf ",trunc(total*10000000000.0)/10000000000.0,trunc(value*10000000000.0)/10000000000.0);
//display values in scientific notation if required by using %.10e format specifier
printf("%.10e %.10e\n",error,trun);
}
printf("\n");
}
} while (i != 2);//this loop will end when user presses 1
}
OUTPUT-
Feel free to comment for any query.