Function and loop

I want to run a simple function represent my X-axis, and here is my code,however, the result is wrong.The result should be 0,0.25,0.5,0.75,..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
#include <iomanip>
#include <conio.h>
#define M 10


using namespace std;
void main()
{
	int i;
	double X_axis[M];
	for (i = 1; i <= M; i++)
	{
		X_axis[i]=(i - 1/4);
		cout << "   " << X_axis[i]<<endl ;
	}
	_getch();
}.
Last edited on
1 is an integer. 4 is an integer. 1/4 is calculated using integer division, the result is zero (with a remainder which is ignored).

You could use 1.0/4.0 to ensure floating-point division is done, or use 0.25 directly instead. In order to achieve the required sequence, the first value should be zero, each following value is found by adding 0.25.

By the way, void main() is not standard C++, many compilers will reject it. It should be int main().
Thank!problem solved:)
Topic archived. No new replies allowed.