nested loop problem

Sep 30, 2015 at 12:23pm
I have the function x^2 and want to find the area under the curve. I want to make it so that if I type in fx n=4 intervals I get the area for n=1 and then n=2 and then n=3... etc. I believe I am supposed to make a nested loop but I cant make it work. this is what I have

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
47
48
49
50
51
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

double f(double x);


int main(){

		double sum,x,dx,a,b;
		int n,j,k;

		cout<<"\n write a";
		cin>>a;

		cout<<"\n write b";
		cin>>b;

		cout<<"\n write n:";
		cin>>n;

		do
				{
					dx=(b-a)/n;
					sum=0;
					x=a-(dx/2);
					for(k=1; k<=n; k++)
					{
						for(j=1; j<=k; j++){
							x=x+dx;
							sum=sum+f(x)*dx;
						}
						cout<<sum<<endl;
					}


				}
				while(k<n);

				return 0;
}

double f(double x)

{
	double result;
				result=x*x;
				return result;
}
Sep 30, 2015 at 1:03pm
closed account (48T7M4Gy)
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
#include <iostream>

double f( double );

using std::cin;
using std::cout;

int main(){
    double sum, x, dx, a, b;
    int n,j,k;
    
    cout<<"Input x1:";
    cin >> a;
    
    cout<<"Input x2:";
    cin >> b;
    
    cout<<"Input no. of divisions:";
    cin>>n;
    
    dx = ( b - a ) / n;
    x = a + dx/2;
    sum = 0;
    
    do
	{
	    sum += dx * f( x);
	    x += dx;
	} while( x <= b );
	
	std::cout << "Area is: " << sum;
	
	return 0;
}

double f(double x)
{
	double result;
	result = x * x;
	return result;
}
Sep 30, 2015 at 5:15pm
But I still get it only for the number of intervals I type in. How do I get it so that it shows fx if i type n=4 I get in one output the area for n=1, the area for n=2 and n=3. This is what I can't figure out.

Sep 30, 2015 at 5:32pm
closed account (48T7M4Gy)
yeah, I missed that bit of your op.

for int k = 1; k < n; k++)
{
// modify the code in the while loop to take into account k
display area for current k
}

That's the basic idea. It is the reverse of what u had, more or less.
Last edited on Oct 1, 2015 at 2:38am
Sep 30, 2015 at 5:58pm
Without sounding like a freeloader I really need to get this point. Does that mean that the do while loop needs to be inside the brackets of the for loop. I did the following and get

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
#include <iostream>

double f( double );

using std::cin;
using std::cout;

int main(){
    double sum, x, dx, a, b;
    int n,j,k;

    cout<<"Input x1:";
    cin >> a;

    cout<<"Input x2:";
    cin >> b;

    cout<<"Input no. of divisions:";
    cin>>n;

    dx = ( b - a ) / n;
    x = a + dx/2;
    sum = 0;

    for( k = 0; k < n; k++){
    do
	{
	    sum += dx * f( x);
	    x += dx;
	} while( x <= b );

	std::cout << "\n Area is: " << sum;
    }
	return 0;
}

double f(double x)
{
	double result;
	result = x * x;
	return result;
}


but then I get the following

Input x1:0
Input x2:3
Input no. of divisions:3

Area is: 8.75
Area is: 21
Area is: 41.25

which is obviously not what I intended
Sep 30, 2015 at 6:30pm
closed account (48T7M4Gy)
you have to move the for .. line up a bit so that dx will change in accordance with the current value of k.
Last edited on Sep 30, 2015 at 6:33pm
Sep 30, 2015 at 6:35pm
Not quite sure what you mean. Could you elaborate?
Sep 30, 2015 at 6:41pm
closed account (48T7M4Gy)
move line 25 up
Sep 30, 2015 at 6:43pm
I cant see why I get 21 and 41.25. By the way this is not my actual assignment but hopefully I can get this method straight I can build on it and complete my assignment
Sep 30, 2015 at 6:46pm
So i created space between the two but I still get the same output

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
47
48
49
50
51
#include <iostream>

double f( double );

using std::cin;
using std::cout;

int main(){
    double sum, x, dx, a, b;
    int n,j,k;

    cout<<"Input x1:";
    cin >> a;

    cout<<"Input x2:";
    cin >> b;

    cout<<"Input no. of divisions:";
    cin>>n;

    dx = ( b - a ) / n;
    x = a + dx/2;
    sum = 0;

    for (k = 0; k < n; k++){









    	do
	{
	    sum += dx * f( x);
	    x += dx;
	} while( x <= b );

	std::cout << "\n Area is: " << sum;
    }
	return 0;
}

double f(double x)
{
	double result;
	result = x * x;
	return result;
}
Sep 30, 2015 at 6:47pm
closed account (48T7M4Gy)
choose n = 150, a = 2, b=25 and as k approaches n you should see the values of sum converges to 39
Last edited on Oct 1, 2015 at 2:38am
Sep 30, 2015 at 6:51pm
closed account (48T7M4Gy)
my last comment is you have to adjust dx so it varies in accordance with k and k varies from 1 to n. it's easy.
Last edited on Sep 30, 2015 at 6:52pm
Sep 30, 2015 at 6:54pm
when I do this I get the following

Area is: 5205.62
Area is: 5302.04
Area is: 5399.65
Area is: 5498.44
Area is: 5598.44
Area is: 5699.63
Area is: 5802.04
Area is: 5905.67
Area is: 6010.52
Area is: 6116.6
Area is: 6223.93
Area is: 6332.51
Area is: 6442.33
Area is: 6553.43
Area is: 6665.79
Area is: 6779.42
Area is: 6894.34
Area is: 7010.55
Area is: 7128.06
Area is: 7246.88
Area is: 7367.01
Area is: 7488.45
Area is: 7611.23
Area is: 7735.34
Area is: 7860.79
Area is: 7987.58
Area is: 8115.74
Area is: 8245.26
Area is: 8376.14
Area is: 8508.41
Area is: 8642.06
Area is: 8777.1
Area is: 8913.54
Area is: 9051.39
Area is: 9190.65
Area is: 9331.33
Area is: 9473.44
Area is: 9616.98
Area is: 9761.97
Area is: 9908.4
Area is: 10056.3
Area is: 10205.7
Area is: 10356.5
Area is: 10508.8
Area is: 10662.6
Area is: 10817.9
Area is: 10974.6
Area is: 11132.9
Area is: 11292.7
Area is: 11454.1
Area is: 11616.9
Area is: 11781.3
Area is: 11947.3
Area is: 12114.7
Area is: 12283.8
Area is: 12454.4
Area is: 12626.6
Area is: 12800.3
Area is: 12975.7
Area is: 13152.6
Area is: 13331.2
Area is: 13511.3
Area is: 13693.1
Area is: 13876.5
Area is: 14061.5
Area is: 14248.1
Area is: 14436.4
Area is: 14626.4
Area is: 14818
Area is: 15011.3
Area is: 15206.2
Area is: 15402.8
Area is: 15601.2
Area is: 15801.2
Area is: 16002.9
Area is: 16206.3
Area is: 16411.4
Area is: 16618.3
Area is: 16826.9
Area is: 17037.2
Area is: 17249.3
Area is: 17463.1
Area is: 17678.7
Area is: 17896.1
Area is: 18115.2
Area is: 18336.1
Area is: 18558.8
Area is: 18783.3
Area is: 19009.6
Area is: 19237.7
Area is: 19467.6
Area is: 19699.4
Area is: 19932.9
Area is: 20168.4
Area is: 20405.6
Area is: 20644.7
Area is: 20885.7
Area is: 21128.6
Area is: 21373.3
Area is: 21619.9
Area is: 21868.4
Area is: 22118.8
Area is: 22371.1
Area is: 22625.3
Area is: 22881.4
Area is: 23139.4
Area is: 23399.4
Area is: 23661.3
Area is: 23925.2
Area is: 24191
Area is: 24458.8
Area is: 24728.6
Area is: 25000.3
Area is: 25274
Area is: 25549.7
Area is: 25827.4
Area is: 26107.1
Area is: 26388.8
Area is: 26672.5
Area is: 26958.3
Area is: 27246.1
Area is: 27535.9
Area is: 27827.8
Area is: 28121.7
Area is: 28417.7
Area is: 28715.8
Area is: 29016
Area is: 29318.2
Area is: 29622.5
Area is: 29928.9
Area is: 30237.4
Area is: 30548.1
Area is: 30860.8
Area is: 31175.7
Area is: 31492.8
Area is: 31811.9
Area is: 32133.2
Area is: 32456.7
Area is: 32782.3
Area is: 33110.1
Area is: 33440.1
Area is: 33772.3
Area is: 34106.6
Area is: 34443.2
Area is: 34781.9
Area is: 35122.9
Area is: 35466.1
Area is: 35811.5
Area is: 36159.2
Area is: 36509.1

I just want to show in 1 output that the more intervals under the graph for the function the closer the approximation will be. Besides isn't there a way to make it work for whatever input you write?
Sep 30, 2015 at 7:04pm
closed account (48T7M4Gy)
take a break. I'll get back in a few hours.
Sep 30, 2015 at 7:07pm
tnx. I hope I don't come off pushy or annoying, but again I'm really stuck with this one
Sep 30, 2015 at 11:20pm
closed account (48T7M4Gy)
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
#include <iostream>

double f( double );

using std::cin;
using std::cout;

int main(){
    double sum, x, dx, a, b, n;
    
    cout<<"Input x1: ";
    cin >> a;
    
    cout<<"Input x2: ";
    cin >> b;
    
    cout<<"Input no. of divisions: ";
    cin>>n;
    
    for (int k = 1; k <=n; k++ )
    {
        dx = ( b - a ) / k;
        x = a + dx/2;
        sum = 0;
    
        do
	    {
	        sum += dx * f( x);
	        x += dx;
	    } while( x <= b );
	
	    std::cout << "k: " << k << '\t' << "Area is: " << sum << std::endl;
    }
	
	return 0;
}

double f(double x)
{
	return x*x;
}


Input x1: 2
Input x2: 5
Input no. of divisions: 25
k: 1	Area is: 36.75
k: 2	Area is: 38.4375
k: 3	Area is: 38.75
k: 4	Area is: 38.8594
k: 5	Area is: 38.91
k: 6	Area is: 38.9375
k: 7	Area is: 38.9541
k: 8	Area is: 38.9648
k: 9	Area is: 38.9722
k: 10	Area is: 38.9775
k: 11	Area is: 38.9814
k: 12	Area is: 38.9844
k: 13	Area is: 38.9867
k: 14	Area is: 38.9885
k: 15	Area is: 38.99
k: 16	Area is: 38.9912
k: 17	Area is: 38.9922
k: 18	Area is: 38.9931
k: 19	Area is: 38.9938
k: 20	Area is: 38.9944
k: 21	Area is: 38.9949
k: 22	Area is: 38.9954
k: 23	Area is: 38.9957
k: 24	Area is: 38.9961
k: 25	Area is: 38.9964
 
Exit code: 0 (normal program termination)
Last edited on Oct 1, 2015 at 3:01am
Topic archived. No new replies allowed.