Lost on the Math, makes code writting harder

Hey I'm totally lost on the math behind this problem I'm trying to figure out.
The problem says to write a program to compute an area between y = sin(x) and y = 0 (x-axis) in a domain of 0 to pi and display the results on screen (use 0.001 as a computing step or interval). So far what I've come up with is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <math.h>
using namespace std;

int main()

{
	double pi = acos (-1.0);
	cout << "x\ty\n\n\n";
	for (int i=0;i<=pi;0.001++)
	{
		double x = i * pi / 180;
		double y = sin(x);
		double y = 0 (x-axis);
		cout << i << "\t" << y << "\n";
	}

	system ("pause");
	return 0;
}

The problem I saw coming was assigning y to be sin(x) and also = 0 (x-axis) what is the way around this? I cannot assign y to be two different things. And the other one that caught me off guard was that I cannot use 0.001++ the program says it can only be a 1++. I'm not sure where the 0.001 would go as im not familiar with what a computing step or interval is. Any help would be greatly appreciated.

It seems like you're trying to find the area by summing together tiny little x-width areas, like the summation version of an integral?

A for loop has more freedom than just i++.
I would change all of your variables to double instead of int because you are working with floating-point numbers.
for (double i = 0; i <= pi; i += 0.001)

There's multiple ways to estimate area, one way would be to add up little tiny rectangles of (dx)*(sin(x)) area. It looks like dx in your code is 0.001.

As for your problem with the naming of "y", you should ask yourself what your variables are actually doing.
What is the purpose of your second "double y" declaration? How are you trying to use it. There might not be a point of having it be a variable if it is always 0.
double y = 0 (x-axis); it also itself a compiler error because multiplication always needs to be denoted by a *, and "axis" is not a declared variable.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cmath>

int main(){
	
	const double pi=3.14159;
	double sum=0, av=0;
	
	for(double x=0; x<=pi; x+= 0.001){		
		sum += sin(x);
		av++;
	}	
	
	std::cout << "area = " << pi*sum/av << "\n";
	
return 0;
}
Analytically, the area is exactly 2. That math is in http://www.mathsisfun.com/calculus/integration-definite.html

Here, however, the purpose here is to calculate an approximation. Rectangles, as in http://www.intmath.com/integration/3-area-under-curve.php

At position x the bottom of a rectangle is at 0 and the top of the rectangle is at sin(x), so the height is sin(x)-0. The width of each rectangle is 0.001 and thus area of a rectangle is 0.001*sin(x).

There is a problem in using a double iterator; floating point math is not intuitive. It is better to iterate with integer and calculate the x from the integer. How many 0.001 wide rectangles are there between 0.0 and pi?
Thank you so much. My girlfriend somehow understood what you guys were saying more than me and explained it to me so I could understand. I guess I was not really grasping the concept of the math behind it. I edited the formula given to use what we have been taught in class and I ended up with

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

#include <iostream>
#include <math.h>
using namespace std;

int main()
#include <iostream>
#include <math.h>

int main()
{
	
	double pi= acos (-1.0);
	double sum=0, av=0;
	
	for(double x=0; x<=pi; x+= 0.001)
	{		
		sum += sin(x);
		av++;
	}	
	
	std::cout << "Area = " << pi*sum/av << "\n";
	system ("pause");

	return 0;
}


I have an example on Monday or Wensday any good you tube channels or videos to get me up to date and speed with c++. Im not really good at it and my professor has such a strong accent I do not understand him. Any suggestions and help are greatly appreciated. As im spending over 600 on the class at a University to get taught by a heavy accented person that can barley speak English. :(

Thank you guys.
Topic archived. No new replies allowed.