Question

In: Computer Science

Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...

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
}

Solutions

Expert Solution

The answer to the above problem is as follows:

  • to achieve the above result and conver the code in C,
  • change all the cout statements to printf statement
  • all the cin statements to scanf statements
  • the header files to be included changes as shown in below code
  • the using namespace std is no longer used, as its not required in C
  • and to print scientfic notation, we use %.10e as format specifier for scientific notation upto 10 decimal places
  • and to print upto 10 decimal places we use %.10lf as format specifier
  • and also, to print upto 10 decimal places without rounding off the value, we use trunc() method as shown in the code below.

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.


Related Solutions

What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left;...
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left; using std::right; using std::setw; using std::string; // The IntVariableTable constructor dynamically allocates the fixed size array of integer variables. IntVariableTable::IntVariableTable() { int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES]; } // The IntVariableTable destructor deallocates the integer variable array. IntVariableTable::~IntVariableTable() { delete[] int_variable_table; } // Returns the number of variables added to the integer variable table. int IntVariableTable::numVariables() const { return num_int_variables; } // Returns the index of...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT