473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

shadowing a parameter

22 New Member
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 4375
sicarie
4,677 Recognized Expert Moderator Specialist
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 Contributor
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 Contributor
plz mention ur compiler next time !
thx!
Nov 11 '06 #4
wuzertheloser
22 New Member
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
wuzertheloser
22 New Member
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
wuzertheloser
22 New Member
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 Contributor
u r right! ;)
good luck!
Nov 12 '06 #8
wuzertheloser
22 New Member
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 New Member
I think it has to do with something in the first part. Any suggestions, guys?
Nov 23 '07 #10

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

Similar topics

3
16932
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 create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In...
3
9469
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 { public: Superclass(int); }
2
7840
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 the base class that modify the member variable 4. I would like to have the inherited classes override the member variable with a new var that's a...
3
1605
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 Class AB : Inherits A
5
1663
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 "Shadows" to hide the base class members. However, this does not seem to work. Any pointers as to what I'm doing wrong here? (I've attached the...
2
1509
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 it's nice to be able to have a synchronous backup so that in case a disk fails, we can recover to the last moment just before failure. Will PITR...
1
2552
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 of imported names? For example: import MySQLdb
16
3145
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 parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed...
3
1826
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? http://www.ptcphotography.com/p254857847/
0
7507
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...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6030
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...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
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...
0
3492
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...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.