trying to integrate 2 functions of trype x^a by parts

I admit this is a little ott for a 3rd program but it nearly works it's just the final lines the compiler has a problem 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
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
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cmath>

void xpowint1();
void xpowint2();
void menu1();
void result();


int main()
{
menu1();

return 0;
}

void menu1()
{
std::cout << "integrating by parts in the form <integral> (function) X (function)" << std::endl;
std::cout << "select an option for first function" << std::endl;
std::cout << "1. x^(a number)" << std::endl;

int menuselect1;
std::cin >> menuselect1;

if(menuselect1==1){ xpowint1();}
                 
}


void xpowint1()
{
     std::cout << "Enter the power of x" << std::endl;
     int power1oru;
     std::cin >> power1oru;
     
     int du1pt1 = power1oru -1;
     xpowint2();
}
    
      
      void xpowint2()
      {
         std::cout << "Enter the next x's power" << std::endl;  
          int power2ordv;
     std::cin >> power2ordv;
     
     int v = power2ordv +1;
     result();
     }
                 


void result()
{ 
     std::cout << "the result is ";
     std::cout << "x^" << power1oru << " * " << "1/" << v << "x" << " - " << "intergral " << du1pt1 << "x" << " * " << "x^" << power2ordv << std::endl;
     std::cout << std::endl;
     main()
}
The program will not compile due to scope issues. Variables power1oru, v, du1pt1, and power2ordv do not exist within the scope of result(), only within the functions where they were declared.

The way you've nested your functions here is awkward. They should rather all be called from main(), and use return values. I'm not sure that these blocks of code warrant being their own functions anyway.

Also, I'm assuming your intention calling main() at the end of result() is to create a loop. There are control structures for that. A do while loop would probably be most appropriate here.

Finally, I'm not sure what you mean to accomplish with this piece of code. Integration by parts when both functions are polynomials doesn't make sense.
That would be poor math and poor organisation on my part, i think it would be possible to add things like cos, tan, 1/x ect if i used a series of if statements. I can't believe i could not see that using 2 polynomials is pointless before.
i think im getting closer with this.... it's a start anyway..... it just don't know how to return a char from a function.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <windows.h>



char sinx1();
char x();
char sinx2();




main ()
{
	std::cout << "integrating by parts in the form <integral> (function u)*(function dv)" << std::endl;
	std::cout << "Enter function <space> function" << std::endl;
	char funct1[10];
	char funct2[10];
	std::cin >> funct1 >> funct2;
	
	
	std::cout << "the result is ";
	
	
	
	if(stricmp ("sin(x)",funct1) == 0)
	 {
	 std::cout << "sin(x)";
	 }
	if(stricmp ("x",funct1) == 0) 
	{
	std::cout << "x";
	}
	if(stricmp ("sin(x)",funct2) == 0)
	{
	std::cout << " * " << sinx2() << " ";
	}
	
	std::cout << "- <integral> ";
	
	if(stricmp ("sin(x)",funct2) == 0)
	{
	std::cout << sinx2() << " * ";
    }
	
	if(stricmp ("x",funct1) == 0) 
	{
	std::cout << x();
	}
	
	
	return 0;
	
	
	
}


char x()
{
	int xdiff = 1;
	return xdiff;
}

char sinx1()
{
	char sinxdiff[10] = "cos(x)";
	return sinxdiff[10];
}
char sinx2()
{
	char sinxint[10] = "-cos(x)";
	return sinxint[10];
	
}
Last edited on
Topic archived. No new replies allowed.