473,786 Members | 2,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with function that calls another function

11 New Member
Write a program which calculates the integral of the function

f(x)=g(x)/n! , where g(x)=1+K*cos^n( n*x)

on the interval from a to b (a<b). In the main program, scanf a
non-negative integer value of n and double values of K, a, and b.
Call the function Integ() to evaluate the integral.

Your main program should be followed by four functions:

double Integ(int n, double K, double a, double b)

double func(double x, int n, double K) to evaluate f(x)

double gunc(double x, int n, double K) to evaluate g(x)

double nfact(int n) to evaluate n!

integrate an arbitrary function f(x) from a to b (letting the
program ask you for n_trap only once). Within the Integ() function
call another function func() to specify f(x). Within the function
func(), call the function nfact() to evaluate n!, and to
to evaluate g(x) call the function gunc().

This is my code so far, but ssh keeps prompting for errors that I can't find. Could someone tell me what's wrong with the code?


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<math.h>
  3. double Intgr(int n, double K, double a, double b);
  4. double func(double x, int n, double K;
  5. double gunc(double x, int n, double K);
  6. double mfact(int n);
  7. main()
  8. {
  9. printf("\n\n");
  10.  
  11. int n, n_trap;
  12. double K, a, b, integral;
  13.  
  14. printf("Enter the non-negative integer: ");
  15. scanf("%d", &n);
  16. printf("Enter the coefficient K in g(x): ");
  17. scanf("%lf", &K);
  18. printf("Enter the bounds for the integration interval, a < b : ");
  19. scanf("%lf %lf", &a, &b);
  20. printf("\n");
  21. printf("Integrate f(x)on [a,b]\n");
  22.  
  23. integral = Intgr(n, K, a, b);
  24. printf("\nThe value of the integral is %.5f\n", integral);
  25. printf("\n\n");
  26. }
  27.  
  28. double Intgr(int n, double K, double a, double b)
  29. {
  30.         int k, n_trap, n1;
  31.         double f, del_x, x, sum;
  32.         printf("Enter the number of trapezoids: ");
  33.         scanf("%d", &n_trap);
  34.         n1 = n_trap + 1;
  35.         del_x = (b - a)/n_trap;
  36.         x = a;
  37.         f = func(x,n,K);
  38.         sum = -0.5 * f;
  39.         for(k=0; k<n1; k++)
  40.         {
  41.          x = a + k * del_x;
  42.          f = func(x,n,K);
  43.          sum += f;
  44.         }
  45.         sum -= 0.5 * f;
  46.         sum *= del_x;
  47.         return sum;
  48. }
  49.  
  50. double gunc(double x, int n, double K)
  51. {
  52.         double g;
  53.         g = 1+K*cos^n(n*x);
  54.         return g;
  55. }
  56.  
  57. double func(double x, int n, double K)
  58. {
  59.         double factorial, y;
  60.         if(n <= 0) y = K;
  61.         else
  62.         {
  63.           factorial = mfact(n);
  64.           y = gunc(x,n,K)/factorial;
  65.         }
  66.         return y;
  67. }
  68.  
  69.  
  70. double mfact(int n)
  71. {
  72.         int i=1, n_fact=1;
  73.         for(i=m; i>0; i--)
  74.         {
  75.          n_fact *= i;
  76.         }
  77.         return n_fact;
  78. }
Feb 24 '10 #1
8 1512
Banfa
9,065 Recognized Expert Moderator Expert
When you say "ssh keeps prompting for errors" do you actually mean that you get errors from the compiler when you compile the code (an operation that you might be doing via ssh but that in fact has absolutely nothing to do with ssh).

If this is the case then post the errors because those error message pretty much say exactly what is wrong.
Feb 24 '10 #2
Banfa
9,065 Recognized Expert Moderator Expert
BTW line 53 is nothing like C syntax and lene 73 references an undeclared variable m.
Feb 24 '10 #3
arperidot
11 New Member
thanks for the quick reply
so i realized that i was supposed to use pow() for line 73
g = 1+K*pow(cos,n)* (n*x);
and changed m to n but i still get this error so i cant execute the program

error: expected declaration specifiers or '...' before 'main'
>>referencing to line 7
Feb 24 '10 #4
arperidot
11 New Member
or actually g = 1+K*pow(cos(n*x ),n);
Feb 24 '10 #5
Bassem
344 Contributor
OK, I left C++ more than 2 years ago.

But I'm asking if your function mfact should explicitly cast the int to double when it returns?

You have declared mfact like this:
double mfact(int n)
But you return int not double at:
int i=1, n_fact=1;
return n_fact;
Try declare n_fact as double and see if it works.

Thanks,
Bassem
Feb 24 '10 #6
Bassem
344 Contributor
Hey,

Plus what Banfa has reviewed, I see line #4 the declaration of the function not completed with ")" before semicolon.
double func(double x, int n, double K;
Thanks,
Bassem
Feb 24 '10 #7
Banfa
9,065 Recognized Expert Moderator Expert
And at line 7 you have not declared the return type (int) of main.

main should always be declared either as

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. }
or
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argp[])
  2. {
  3. }
Feb 24 '10 #8
arperidot
11 New Member
thanks alot guys! that helped alot.
turns out my gunc() wasn't properly defined so i ended up computing g(x) directly on f(x)

double func(double x, int n, double K)
{ double factorial, y;
factorial = mfact(n);
y = (1+K*pow(cos(n* x),n))/factorial;
return y; }
Feb 25 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3396
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another function call funcRet()is made which returns the expected argument type for the function call by reference funcByRef(class A&); The only way to get around this probelm is to first call the funcRet(), assign its value to a variable and pass that...
8
3394
by: DKM | last post by:
Here are the source code files to a Java applet that utilizes LiveConnect to communicate with Javascript, and the HTML file. The thing works both in IE 6.0 and FireFox 1.4. but with some problems. IE crashes when one refreshes the page or leave the page. This happens only after calling the Java method more than once. It does not crash if the Java method is called just once and then the page is refreshed. FireFox does not crash at all...
3
2666
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; } #define in_uint32_le(s,v) { in_uint16_le(s,v) \ v += *((s)->p++) << 16; v += *((s)->p++) << 24; } I'm personally not fond of function-like macros and wanted to turn these into static inline functions, but I'm having trouble doing...
4
1700
by: Chronologic | last post by:
All, I have an issue I would like some expert help on. I understand, or so I believe, that C# does not support the concept of a "compile time macro". At least not in the sense I'm looking for. While many users contend that macros are inherently evil, I would argue that no - they are not, they have a function. That function is sometimes -- perhaps much too often -- abused, but they do have a
22
4071
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
13
4927
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
2
1923
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function The question may be silly or even meaning less but please............
0
5576
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
22
3279
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
3
2284
by: dupuis2387 | last post by:
Hi, I'm having trouble getting an activeXDLL component that I created to work in asp. It works fine in a VB6 windows app, but for some reason, it won't work in my asp page. I compiled the dll, and registered it on the server. Here is the DLL Code
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.