473,398 Members | 2,380 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,398 software developers and data experts.

shadowing a parameter

Write a program which calculates the integral of the function

f(x)=(A*x^m)/n!

on the interval from a to b (0<a<b). In the main program, scanf
a double value for m and a non-negative integer value for n,
a double value of A, and positive double values for a and b>a.
Call the function Intgr() to evaluate the integral.

Your main program should be followed by three functions:

double Intgr(double m, int n, double A, double a, double b)

double func(double x, double m, int n, double A)

double nfact(int n)


When writing the function Intgr(), use the program w4-10.c
or the program from part B of your hw5, appropriately modified
to integrate an arbitrary function f(x) from a to b
(let the program ask you for n_trap only once, and scan
sufficiently large value for n_trap). Within Intgr() call another
function func() to specify f(x). The return value of func() should
be equal to A*x^m if n=0, or A*x^m/n! if n>0. To evaluate n! call the
function nfact() that you will also write. To evaluate x^m call
the function pow() already embedded in the math.h library.

.................................................. ..............

Your output should look like this:

Enter the exponents (double)m and (int)n in f(x)=A*x^m/n! : 3.25 5

Enter the coefficient A in f(x)=A*x^m/n! : -1.5

Enter the bounds for the integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000

The value of the integral is -0.792905 .

*/
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. double Intgr(double m, int n, double A, double a, double b);
  4. double func(double x, double m, int n, double A);
  5. double nfact(int n);
  6. main()
  7. {
  8. double m, A, a, b, x, del_x, sum, f;
  9. int n, k, n_trap;
  10. printf("Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : ");
  11. scanf("%f %d", &m, &n);
  12. printf("\nEnter the coefficient A in f(x) = A*x^m/n! : ");
  13. scanf("%f", &A);
  14. printf("\nEnter the bounds for the integration interval, a < b : ");
  15. scanf("%f %f", &a, &b);
  16.  
  17.  
  18. printf("Integrate f(x) on [a,b]\n");
  19. printf("Enter the number of trapezoids : ");
  20. scanf("%d", &n_trap);
  21. printf("\nn! is : %g\n", nfact(n));
  22. printf("\nThe value of the integral is %g\n", Intgr(m,n,A,a,b));
  23. }
  24.  
  25. double Intgr(double m, int n, double A, double a, double b)
  26. {
  27. double m, A, a, b;
  28. int n;
  29. del_x = (b-a)/n_trap;
  30. x = a;
  31. f = A*pow(x,m)/nfact(n);
  32. sum = -0.5 * f;
  33. double m, A, a, b;
  34. int n;
  35. del_x = (b-a)/n_trap;
  36. x = a;
  37. f = A*pow(x,m)/nfact(n);
  38. sum = -0.5 * f;
  39. for(k=0; k<=n_trap; k++)
  40. {
  41. x = k * del_x;
  42. f = A*pow(x,m)/nfact(n);
  43. sum +=f;
  44. }
  45. sum -= 0.5 * f;
  46. sum *= del_x;
  47. }
  48.  
  49. double func(double x, double m, int n double A)
  50. {
  51. double x, m, A;
  52. int n;
  53. if(n==0) return A*pow(x,m);
  54. else return (A*pow(x,m))/nfact(n);
  55. }
  56.  
  57. double nfact(int n)
  58. {
  59. if(n==1)return 1;
  60. else return n*nfact(n-1);
  61. }
  62.  
  63.  
everytime i compile, i get these error messages:

iacs5.ucsd.edu% !g
gcc hw7.c -lm
hw7.c: In function `Intgr':
hw7.c:77: warning: declaration of `m' shadows a parameter
hw7.c:77: warning: declaration of `A' shadows a parameter
hw7.c:77: warning: declaration of `a' shadows a parameter
hw7.c:77: warning: declaration of `b' shadows a parameter
hw7.c:78: warning: declaration of `n' shadows a parameter
hw7.c:79: error: `del_x' undeclared (first use in this function)
hw7.c:79: error: (Each undeclared identifier is reported only once
hw7.c:79: error: for each function it appears in.)
hw7.c:79: error: `n_trap' undeclared (first use in this function)
hw7.c:80: error: `x' undeclared (first use in this function)
hw7.c:81: error: `f' undeclared (first use in this function)
hw7.c:82: error: `sum' undeclared (first use in this function)
hw7.c:83: error: `k' undeclared (first use in this function)
hw7.c: At top level:
hw7.c:93: error: parse error before "double"
hw7.c: In function `func':
hw7.c:94: error: number of arguments doesn't match prototype
hw7.c:54: error: prototype declaration
iacs5.ucsd.edu% vi hw7.c
Version SVR4.0, Solaris 2.5.0
"hw7.c" 105 lines, 2783 characters
Nov 10 '06 #1
11 4360
sicarie
4,677 Expert Mod 4TB
First of all, in double func() you need a comma between the last two variables you are passing it. I also don't think you should be redeclaring the amounts you are passing to it.

It's the same thing with Intgr() and from there, you should be able to debug from the output statements (gets you down to about 15, but they look manageable at first glance - but let us know if you start having trouble with them).
Nov 10 '06 #2
momotaro
357 100+
this is ur code corrected!
pay attention to redeclaration NO NEED!
PS: dont pay attention to the 3 warnings that u will have! ;)
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. double Intgr(m, n, A, a, b);
  4. double func(x, m, n, A);
  5. double nfact(n);
  6. double m, A, a, b, x, del_x, sum, f;
  7. int n, k, n_trap;
  8. main()
  9. {
  10. printf("Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : ");
  11. scanf("%f %d", &m, &n);
  12. printf("\nEnter the coefficient A in f(x) = A*x^m/n! : ");
  13. scanf("%f", &A);
  14. printf("\nEnter the bounds for the integration interval, a < b : ");
  15. scanf("%f %f", &a, &b);
  16.  
  17.  
  18. printf("Integrate f(x) on [a,b]\n");
  19. printf("Enter the number of trapezoids : ");
  20. scanf("%d", &n_trap);
  21. printf("\nn! is : %g\n", nfact(n));
  22. printf("\nThe value of the integral is %g\n", Intgr(m,n,A,a,b));
  23. }
  24.  
  25. double Intgr(double m, int n, double A, double a, double b)
  26. {
  27. del_x = (b-a)/n_trap;
  28. x = a;
  29. f = A*pow(x,m)/nfact(n);
  30. sum = -0.5 * f;
  31. del_x = (b-a)/n_trap;
  32. x = a;
  33. f = A*pow(x,m)/nfact(n);
  34. sum = -0.5 * f;
  35. for(k=0; k<=n_trap; k++)
  36. {
  37. x = k * del_x;
  38. f = A*pow(x,m)/nfact(n);
  39. sum +=f;
  40. }
  41. sum -= 0.5 * f;
  42. sum *= del_x;
  43. }
  44.  
  45. double func(double x, double m, int n, double A)
  46. {
  47. if(n==0) return A*pow(x,m);
  48. else return (A*pow(x,m))/nfact(n);
  49. }
  50.  
  51. double nfact(int n)
  52. {
  53. if(n==1)return 1;
  54. else return n*nfact(n-1);
  55. }
  56.  
Nov 11 '06 #3
momotaro
357 100+
plz mention ur compiler next time !
thx!
Nov 11 '06 #4
okay, thanks for the help everyone! i appreciate it =] i'll let you know how everything goes.

i'm not quite sure which compiler i'm using because it comes from my professor's website.

the program is called
SSH secure shell
and it uses vi editor to make the C files. not sure if that helps.
Nov 11 '06 #5
oh just for clarification
what does the function
double Intgr() output?

i need it to output the sum, but i don't think it's doing that.
Nov 11 '06 #6
program works fine now
only problem is the output i get for the integral

my output:


Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : 3.25 5

Enter the coefficient A in f(x) = A*x^m/n! : -1.5

Enter the bounds for the integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids : 1000

The value of the integral is -5.87171e+151

correct output:


Enter the exponents (double)m and (int)n in f(x)=A*x^m/n! : 3.25 5

Enter the coefficient A in f(x)=A*x^m/n! : -1.5

Enter the bounds for the integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000

The value of the integral is -0.792905 .


I think this has something to do in my Intgr function correct?
Nov 11 '06 #7
momotaro
357 100+
u r right! ;)
good luck!
Nov 12 '06 #8
any idea how you would integrate
A*x^m/n! ?

mathematically i know how to do it. but the only inputs i'm allowed to have are A, m, and n. Therefore, x would have no defined value.
Nov 13 '06 #9
REICC
3
I think it has to do with something in the first part. Any suggestions, guys?
Nov 23 '07 #10
REICC
3
also, why are you declaring the variables before main? Sorry, I am a noob
Nov 24 '07 #11
seforo
60
Any function that is defined below the main function should have its function prototype declared before the main function. Otherwise you should define (or write) all your functions before defining main function.
Nov 24 '07 #12

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

Similar topics

3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
3
by: Aguilar, James | last post by:
Hey all. I was making a newbie mistake that I eventually figured out. That is not my question. My question is about the error message. So let me set the stage for you: class Superclass {...
2
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in...
3
by: Nick Stansbury | last post by:
Hi, Quick (and probably simple) question regarding shadowing and polymorphism. Problem is best explained by an example. Public Class A public CommonProperty as Integer end class Public...
5
by: Dave Taylor | last post by:
I'm trying to derive a class from a typed DataSet to add some methods to one of the DataTables. I would like to keep the names the same in the derived class as in the base class, so I have used...
2
by: David Garamond | last post by:
Is there a feature similar to this currently in Postgres, or will there be? Sometimes (like in a shared hosting environment), we cannot have the luxury of hot-swapped RAID or expensive SAN, and...
1
by: John Salerno | last post by:
I understand that if you reassign a built-in name to your own variable, such as: str = 'hello' then you lose the use of the built-in (in this case str()), but is this also the case in terms...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
3
by: Cartoper | last post by:
I really like the shadowing that this web site is using behind the images. I have tried to dig through their code to figure out how they did it, but it still eludes me. How do they do it? ...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.