how do i display the min value from output table and print three asterisk?

Here is question:

rite a program that will calculate the functional values of the function f(x)on some selected points from a domain, where f(x)=e^-x*(x+3)*cos(2x),and domain of f(x) is[-3, 3].
Print all values in a 2-column format in the range of [-3, 3] with increment of 0.5. If the functional value is less than zero, your program should print three asterisk(***) beside the value. Finally, the program should display the minimum functional value(from table)along with the value of x.

Here is what i did:

#include "stdafx.h"
#include "iostream"
#include "cmath"
#include "iomanip"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
double x = 0;
double f = 0;
double initialvalue = -3;
double increment = 0.5;
double endingvalue = 3;
const double e = 2.718281828;



if (initialvalue <= endingvalue){
cout << " x functional value" << endl;
cout << "============================================" << endl;

}for (x = initialvalue; x <= endingvalue; x += increment){
f = pow(e, -x)*(x + 3)*cos(2*x);

cout << setw(5) << fixed << setprecision(1) << x << setw(30) << fixed << setprecision(2) << f << endl;

}
if (f < 0){
cout << f << "***" << endl;

}


cin.get();
return 0;
}
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post.
Pay attention to whitespace and indentation. When done intuitively, the various scopes are easier to see.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  if (initialvalue <= endingvalue)
  {
    cout << " x functional value" << endl;
    cout << "============================================" << endl;
  }

  for (x = initialvalue; x <= endingvalue; x += increment)
  {
    f = pow(e, -x)*(x + 3)*cos(2*x);

    cout << setw(5) << fixed << setprecision(1) << x << setw(30)
	 << fixed << setprecision(2) << f << endl;
  }

  if (f < 0)
  {
    cout << f << "***" << endl;
  }

You do have a (conditional) header of the table. Nice. However, you do know that initialvalue <= endingvalue, so there is not much uncertainty.

Your loop counter is a double. That may or may not work, for floating point math in computer is not quite as intuitive as theoretical math. As a guideline prefer integral loop counter and compute a double value from it.

You have a table.
One line should have x, f(x) and *** (if f(x) is negative).

Your table does not have those. It has only x and f(x).
Then you print one separate line of ***, if the last f(x) was negative.
Not quite what was requested.

Minimum value. You have to keep track (i.e. store) of a x,f(x) pair that was small. On every iteration, if the current f(x) is less, then update that stored pair.
What is are good initial values for x and f(x) before the loop starts?
Topic archived. No new replies allowed.