Question

Cant find what is wrong to fix the code. can you please fix the code. The...

Cant find what is wrong to fix the code. can you please fix the code.

The function is: sin^-1 (x/5)

#include <iostream>

#include <iomanip>

#include <math.h>

#include <cmath>

using namespace std;

double f(double x)

{

return pow(sin, -1)*(x / 5);

}

int main()

{

int i, n;

double a = 0.0, b =1.0, h, s = 0.0, x, xbar;

double s1, s2, s3;

cout << " Enter # of partitions n ";

cin >> n;

cout << " n = " << n << endl;

h = (b - a) / n;

for (i = 0; i < n; i++)

{

x = a + i * h;

s = s + f(x);

}

s = s * h;

cout << " Result Left End Points = " << s << endl;

s = 0.0;

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

{

x = a + i * h;

s = s + f(x);

}

s = s * h;

cout << " Result Right End Points = " << s << endl;

s = 0.0;

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

{

xbar = a + (2. * i - 1)*(h / 2.);

s = s + f(xbar);

}

s = s * h;

cout << " Result using Mid Point Rule = " << s << endl;

// Using Trpezoidal Rule

s = 0.0;

for (i = 1; i <= n - 1; i++)

{

x = a + i * h;

s = s + f(x);

}

s = (h / 2.) * (f(a) + f(b)) + h * s;

cout << " Result using Trapezoidal Rule = " << s << endl;

// Using Simpson's Rule

h = (b - a) / (2.0*n);

s1 = s2 = s3 = 0.0;

s1 = (f(a) + f(b));

for (i = 1; i < 2 * n; i = i + 2)

{

x = a + i * h;

s2 = s2 + f(x);

}

for (i = 2; i<2 * n; i = i + 2)

{

x = a + i * h;

s3 = s3 + f(x);

}

s = (h / 3.0)*(s1 + 4.0*s2 + 2.0*s3);

cout << " Result using Simpson's Rule = " << s << endl;

system("PAUSE");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
using namespace std;
double f(double x)
{
    return asin(x/5);
}

int main()
{
    int i, n;
    double a = 0.0, b =1.0, h, s = 0.0, x, xbar;
    double s1, s2, s3;
    cout << " Enter # of partitions n ";
    cin >> n;
    cout << " n = " << n << endl;
    h = (b - a) / n;
    for (i = 0; i < n; i++)
    {
        x = a + i * h;
        s = s + f(x);
    }
    s = s * h;
    cout << " Result Left End Points = " << s << endl;
    s = 0.0;
    for (i = 1; i <= n; i++)
    {
        x = a + i * h;
        s = s + f(x);
    }
    s = s * h;
    cout << " Result Right End Points = " << s << endl;
    s = 0.0;
    for (i = 1; i <= n; i++)
    {
        xbar = a + (2. * i - 1)*(h / 2.);
        s = s + f(xbar);
    }
    s = s * h;
    cout << " Result using Mid Point Rule = " << s << endl;
// Using Trpezoidal Rule
    s = 0.0;
    for (i = 1; i <= n - 1; i++)
    {
        x = a + i * h;
        s = s + f(x);
    }
    s = (h / 2.) * (f(a) + f(b)) + h * s;
    cout << " Result using Trapezoidal Rule = " << s << endl;
// Using Simpson's Rule
    h = (b - a) / (2.0*n);
    s1 = s2 = s3 = 0.0;
    s1 = (f(a) + f(b));
    for (i = 1; i < 2 * n; i = i + 2)
    {
        x = a + i * h;
        s2 = s2 + f(x);
    }
    for (i = 2; i<2 * n; i = i + 2)
    {
        x = a + i * h;
        s3 = s3 + f(x);
    }
    s = (h / 3.0)*(s1 + 4.0*s2 + 2.0*s3);
    cout << " Result using Simpson's Rule = " << s << endl;
    system("PAUSE");
    return 0;
}

Enter # of part itions n 100 n100 Result Left End Points0.0993307 Result Right End Points-0.101344 Result using Mid Point Rul

asin\;is\;used\;for\;sine\;inverse\;(sin^{-1})\;in\;c++

Add a comment
Know the answer?
Add Answer to:
Cant find what is wrong to fix the code. can you please fix the code. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • Have Corporate Sales for 6 divisions and their quarterly sales figures. The issue is the error...

    Have Corporate Sales for 6 divisions and their quarterly sales figures. The issue is the error function where I am unable to have user reinput the same quarterly amount without going to the next quarter input. This has affected the total sales for the company. /***************************************** This program gathers sales information for six divisions and displays the total sales for each division and total company. *****************************************/ #include<iostream> using namespace std; // Classs division sales class DivSales { private: // Variables...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • THE CODE CALCULATES THE MEAN MEDIAN AND MODE OF THE SET OF NUMBERS THE CODE IS...

    THE CODE CALCULATES THE MEAN MEDIAN AND MODE OF THE SET OF NUMBERS THE CODE IS WRITTEN IN C++ PLEASE FIX THE MODE FUNCTION ! SO THAT IT OUTPUTS 0 WHEN THERE ARE NO REPETITIONS IN THE DATASET eg. for 1,2,3,4,5 mode should be zero but it gives 1 ! #include<iostream> #include<math.h> using namespace std; class statistical{ protected: double *dataArray; int size; public: statistical(double a[], int s){ dataArray = new double[s]; for (int i = 0; i<s; i++){ dataArray[i] =...

  • Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as...

    Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as well as techniques for managing dynamically-allocated storage in C++. Assignment In this assignment, you will write a class called Stack that will encapsulate a dynamically-allocated array of elements of a generic data type. A driver program is provided for this assignment to test your implementation. You don't have to write the tests. Program You will need to write a single template class for this...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • Find and fix the errors in this C++ code: * This program illustrates a variety of...

    Find and fix the errors in this C++ code: * This program illustrates a variety of common loop errors. * Fix the errors in each section. */ #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout <<...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT