Lab

I have to create a lab and I'm having some trouble... Here is the question

You need to write a program to calculate the area under a curve f(x) = 5x3 + 7x2 – 3x + 4 for a <= x <= b where a and b are greater than or equal to 0.

You will be given a file containing values of a and b. For each pair of values:

Check whether a and b are larger than 0, and whether a is smaller than or equal to b. If NOT, error message must be displayed and your program should go on to the next set of input values. If YES, your program approximates the area under the curve between a and b by partitioning the area into small segments where each segment has width w.

For each area calculation, your program should calculate areas for w, w/2, w/4, …, starting with w=(b-a)/10. The calculation should be done using a function findarea(a,b,w) which takes as input a, b, and w, and returns the area.

Your program should keep calculating more accurate areas until the difference between the last two of your calculations is less than .0001. Then the program should output the last area value calculated to a file output.txt.

Example data file input.txt:

2 3
-1 4
2 1
3.5 7

Example output.txt:

Areas under a curve defined by f(x) = 5x^3 + 7x^2 – 3x + 4:
For a = 2 and b = 3, the calculated area is [value your program calculated]
For a = -1 and b = 4, a is less than zero, calculation not done
For a = 2 and b = 1, b is less than a, calculation not done
For a = 3.5 and b = 7, the calculated area is [value your program calculated]
Please post what you have
i have this so far thanks for the help...i think my main problem is with the file reading portion.

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
double a;
double b;
double w;
double curve=0;

ifstream inputFile;
inputFile.open("values.txt");
cin >> a;
cin >> b;

while (inputFile >> a >> b)
{
if (a < 0 || b < 0)
{
cout << "Error!" << endl;
cin >> a;
cin >> b;
}
else
{
while(curve-curve>.001)
{
w=(b-a)/10;
curve += (10*pow(w, 3.0)+(7*pow(w, 2.0))-(3*w)+4);
cout << "The final area of the curve is: " << curve << endl;
}
}

}
return 0;
}
closed account (18hRX9L8)
For powers, use the pow() function from <cmath>. Warning: It returns floats and takes in floats.
Thats what I used
There are quite a few things wrong here, both logically and the way you have interpreted the assignment.

i think my main problem is with the file reading portion.


I'm not trying to be rude, but your post seems to be a "find the important data" problem. If you think you have a file reading problem, then make a program that only reads the data from your file.

For instance, start your program as such:
1
2
3
4
5
6
7
8
9
10
int main(void)
{
  fstream file("values.txt");
  if (!file.good()) // Is the file not opened?
  {
    cout << "Could not open\n";
    return 0;
  }
  cout << "File opened\n";
  return 0;


What happens with that? Did you file open? If not, why? The file must be in the wrong folder or not exist at all (or perhaps being used by another program).

Then move on to reading from the file:
1
2
3
4
int a, b;
while (file >> a >> b)
  cout << "A: " << a << " B: " << b << endl;
file.close();


Did all of the numbers show up correctly?

Then you can move onto logic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (file >> a >> b)
{
  cout  "For a = " <<  a << " and b = " << b << ", ";
  if (a < 0)
  {
    cout << "a is less than zero, calculation not done\n";
  }
  else if (b < a)
  {
    cout << "b is less than a, calculation not done\n"; 
  }
  else
  {
    cout << "a and b are fine numbers\n";
  }       
}


Then keep adding more logic until your program is done. You need to make the area function, as well as update w.
Topic archived. No new replies allowed.