How do i declare a function in the command promp window (c++ question)

In summary, the person has written a script to find the definite integral of a function using Simpson's rule. However, they are looking for a way to define the function to be integrated within the command prompt window instead of having to edit the program each time. They are advised to use arguments to the main function and implement logic for parsing the string to understand different operators and functions.
  • #1
okkvlt
53
0
Hi. i wrote a script that finds the definite integral of a function using simpsons rule.
right now the function to be integrated is defined as 2*pow(9-pow(x,2),.5).
but this is very inconvenient to me because i must edit the program each time i want to find a new integral. my question is how do i define the function to be integrated inside the command prompt window? (in other words, i want the user to declare the function to be integrated, instead of the programmer declaring the function to be integrated.)




#include <stdio.h>
#include <math.h>
/*define f[x]*/
double f(double x)
{
double f=2*pow(9-pow(x,2),.5) ;
return f;
}

/*program main*/

int main()
{
double a,b,dx;
long double total1=0,total2=0;
long int n,i=1;
printf("b\nS f[x]dx\na\n\n");
printf("b=");
scanf("%lf",&b);
printf("a=");
scanf("%lf",&a);
printf("number of increments=");
scanf("%d",&n);
dx=(b-a)/n;
printf("%.13lf\n",dx);
/*calculations*/
while(i<=n-1)
{
total1=dx*f(a+i*dx)+total1;
printf("%d\r",i);
i=i+1;
}
i=0;
while(i<=n-1)
{
total2=dx*f(a+(i+.5)*dx)+total2;
printf("%d\r",i);
i=i+1;
}
double integral=total1/3+2*total2/3+dx*(f(a)+f(b))/6;

printf("definite integral=%.13lf", integral);
getchar();
getchar();
}
 
Technology news on Phys.org
  • #2
You can pass a string as an argument to your main function, and your program will need to be able to parse the string to figure out what the function is. Look up "arguments to main" to find out how that works. Parsing the string that is passed into main is quite a bit more complicated because your program will have to have logic so that it understands the arithmetic operators (+, -, *, /), powers of functions, trig functions, and whatever else your program needs to work with.
 
  • #3




In order to declare a function in the command prompt window in C++, you can use the following steps:

1. Begin by opening the command prompt window and navigating to the directory where your C++ program is located.

2. In your C++ program, define the function that you want to be integrated using the "double" data type and a variable name (in this case, we have used "f" as the variable name).

3. Within the function, use the "pow" function from the "math.h" library to define the function that needs to be integrated. In this example, we have used the function 2*pow(9-pow(x,2),.5).

4. Once the function is defined, you can call it within your main program by using the variable name followed by the input value (in this case, the input value is "a+i*dx").

5. Finally, in order to allow the user to declare the function to be integrated, you can use the "scanf" function to prompt the user to enter the input values for the function. This will allow the user to declare the function to be integrated without the need for the programmer to edit the program each time.

By following these steps, you can declare a function in the command prompt window in C++ and allow the user to input the function to be integrated, making the program more convenient and user-friendly.
 

1. How do I declare a function in the command prompt window in C++?

To declare a function in the command prompt window in C++, you must first start by creating a new C++ file. Then, use the "void" keyword to declare the function, followed by the function name and any necessary parameters within parentheses. Finally, use curly braces to enclose the code for the function.

2. Can I declare multiple functions in the command prompt window?

Yes, you can declare multiple functions in the command prompt window in C++. However, each function must have a unique name and any necessary parameters must be properly defined for each function.

3. How do I call a declared function in the command prompt window in C++?

To call a declared function in the command prompt window in C++, you must use the function name followed by parentheses and any necessary arguments. Make sure to properly define the function and its return type before calling it.

4. Do I need to declare a function before calling it in the command prompt window in C++?

Yes, you must declare a function before calling it in the command prompt window in C++. Otherwise, the compiler will not recognize the function and will result in an error.

5. How do I declare a function with a return value in the command prompt window in C++?

To declare a function with a return value in the command prompt window in C++, you must specify the return type before the function name. This can be any valid data type such as int, float, or string. In the function body, use the "return" keyword followed by the value you want to return.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
933
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
11
Views
1K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
750
Back
Top