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

--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
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 <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
#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...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
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++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT