math & printif

Hi im trying to calculate and show this equation y(x)= 3*x2+3*x-1 for following x
the formula x = a+i*h where i=0,1,2, ..., N.
data: a - starting point x, h - step, N - number of steps >0

Something is working, but It never ends and I don't know how to stop it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
double a,h,x=a;
int N;
cout <<"Podaj punkt poczatkowy"<<endl;
cout <<"a: ";
cin >>a;
cout <<"Podaj krok"<<endl;
cout <<"h: ";
cin >>h;
cout <<"Podaj liczbe krokow"<<endl;
cout <<"N: ";
cin >>a;


printf("%.5s\n %5s\n", "x", "y(x)");
for (auto i=0; i<=N; i++)
{
    printf ("%5.3f\n %5.3f\n", x,x*(3*x+3)-1);
    x = x+h;
cout<<x<<endl;
}



return 0;
}
You do realize C++ now can do formatted output, right? C++ added the <format> header.
https://en.cppreference.com/w/cpp/utility/format/format
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <format>

int main()
{
   std::cout << std::format("Hello {}!\n", "world");

   std::cout << std::format("The answer is {}.\n", 42);
}
Hello world!
The answer is 42.


Besides, you forgot to include <cstdio> to PROPERLY use printf.

Your source could use a bit of better formatting, indenting and such.

Mixing C and C++ output streams is not recommended.
Line 8: a has random value and so does x
Line 18. Shouldn't the value go into N ?
i have change some of it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<cmath>
#include<cstdio>

using namespace std;

int main()
{
double a=0,h=0,x=a;
int N;
cout <<"starting point"<<endl;
cout <<"a: ";
cin >>a;
cout <<"step"<<endl;
cout <<"h: ";
cin >>h;
cout <<"number of steps"<<endl;
cout <<"N: ";
cin >>N;


printf("%.5s\n %5s\n", "x", "y(x)");
for (auto i=0; i<=N; i++)
{
    printf ("%5.3f\n %5.3f\n", x,x*(3*x+3)-1);
    x = x+h;

}



return 0;
}
People are stuck on printf, and so now C++ also has its own: format.

The stream manipulators look stupid, but are actually a good idea. And pretty darn easy to use:

std::cout << x << (x * (3*x+3) - 1) << "\n";
it looks much better!
I should clarify: if you wish to have formatting controls on the output, that makes things a little long-winded and clunky-ish. But to get the %5.3f output you need to use some manipulators found in <iomanip>.

 
std::cout << std::fixed << std::setw(5) << std::setprecision(3) << my_float_var;

This is one of the main reasons people like the old printf-style stuff: it is really short to write.

I would personally just use an overloaded manipulator function instead and avoid the overhead of parsing a string. It gets a little long-winded, though:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iomanip>
#include <iostream>

namespace alanfromeurope
{
  struct fixed
  {
    int w, p;
  };
}

std::ostream & operator << ( std::ostream & outs, const alanfromeurope::fixed & fixed )
{
  return outs << std::fixed << std::setw( fixed.w ) << std::setprecision( fixed.p );
}

alanfromeurope::fixed fixed( int width, int precision )
{
  return { width, precision };
}

//// And now that that is out of the way, using it == short and pretty! ////

int main()
{
  std::cout << fixed(5,3) << 3.14159265 << "\n";
}

That is kind of how C++ works: bury the messy stuff in a library somewhere so that things can be short and pretty when you use them.
Last edited on
But to get the %5.3f output you need to use some manipulators found in <iomanip>.
<format> can do that:
1
2
3
4
5
6
7
#include <iostream>
#include <format>

int main()
{
   std::cout << std::format("{:7.3f}\n", 3.14159f);
}
  3.142

I chose 7.3 to pad right-justified for a total of seven "spaces".

https://en.cppreference.com/w/cpp/utility/format/formatter
As an aside, I ran into an math expression evaluator library: https://github.com/ArashPartow/exprtk

I've hacked example-1 to reimplement @alanfromeurope's program above. It needs exprtk.hpp from the project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <exprtk/exprtk.hpp>

#include <cstdio>
#include <iostream>
#include <string>
#include <tuple>

template <typename T>
void evaluate(std::tuple<T, T, T> range, const std::string& expression_string)
{
	typedef exprtk::symbol_table<T> symbol_table_t;
	typedef exprtk::expression<T>	expression_t;
	typedef exprtk::parser<T>	parser_t;

	T x;

	symbol_table_t symbol_table;
	symbol_table.add_variable("x", x);
	symbol_table.add_constants();

	expression_t expression;
	expression.register_symbol_table(symbol_table);

	parser_t parser;
	parser.compile(expression_string, expression);

	for (x = std::get<0>(range); x <= std::get<1>(range); x += std::get<2>(range))
	{
		const T y = expression.value();
		printf("%19.15f\t%19.15f\n", x, y);
	}
}

int main()
{
	// almost any expression here - see project readme
	const std::string expression_string = "x*(3*x+3) - 1";

	double min{}, max{}, step{};
	std::cout << "min: "; std::cin >> min;
	std::cout << "max: "; std::cin >> max;
	std::cout << "step: "; std::cin >> step;

	evaluate<double>({min, max, step}, expression_string);
	return 0;
}
Last edited on
Topic archived. No new replies allowed.