473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with function that calls another function

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

✓ answered by Bassem

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

8 1493
Banfa
9,065 Expert Mod 8TB
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 Expert Mod 8TB
BTW line 53 is nothing like C syntax and lene 73 references an undeclared variable m.
Feb 24 '10 #3
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
or actually g = 1+K*pow(cos(n*x),n);
Feb 24 '10 #5
Bassem
344 100+
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 100+
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 Expert Mod 8TB
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
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
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...
8
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...
3
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 +=...
4
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...
22
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
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...
2
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 ...
0
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...
22
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...
3
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.