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

  • Thread starter Thread starter okkvlt
  • Start date Start date
  • Tags Tags
    Function Window
AI Thread Summary
The discussion centers around enhancing a script that calculates the definite integral of a function using Simpson's rule. The current implementation requires hardcoding the function to be integrated, which is inconvenient for users who want to evaluate different functions without modifying the code. The main question posed is how to allow users to define the function directly via the command prompt. To achieve this, it is suggested that the program should accept a string argument in the main function, which would require parsing to interpret the mathematical expression. This parsing would involve recognizing various arithmetic operators and functions, making it a complex task. The discussion emphasizes the need for the program to handle user-defined functions dynamically, enhancing usability and flexibility.
okkvlt
Messages
53
Reaction score
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
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top