PLEASE NOTE: THIS POST IS FROM KIRA89. ALL REFERENCES TO GCC HAVE BEEN CHANGED TO GCHello,
I am new to C and I'm getting an error I have no idea how to fix.
For my assignment I am making a program that finds the area under a curve
using trapezoidal approximation.
Here's my code:
CODE
# Makefile for Lab7.c
# CS201
# Makefile for Multiple Files
# Lab7.o: Lab7.c Proto.h
Lab7: Lab7.o Trap.o XSquaredSinx.o HalfCircle.o TestFunction.o
gc -o Lab7 Lab7.o Trap.o XSquaredSinx.o HalfCircle.o TestFunction.o
Lab7.o: Lab7.c Proto.h
gc -c Lab7.c
XSquaredSinx.o: XSquaredSinx.c
gc -c XSquaredSinx.c
HalfCircle.o: HalfCircle.c
gc -c HalfCircle.c
TestFunction.o: TestFunction.c
gc -c TestFunction.c
CODE
/* Proto.h - Function Prototypes */
#ifndef _PROTO_H
#define _PROTO_H
void Trap(double f(double f_arg), double a, double b, double n, double *h,
double *area);
double XSquaredSinx(double x);
double HalfCircle(double x);
double TestFunction(double x);
#endif
CODE
/* Lindsey Hanna
* lab6 section3
* February 36, 2008
* Pg. 321: Create a program to assist with the calculation of intravenous
rates.
*/
#include <stdio.h> // printf, scanf, fprint, fscanf, fopen, fclose
definitions
#include <stdlib.h> // exit definition
#include <math.h> // math library
#include "Proto.h" // prototypes of methods for calculation
int i, j; // variables for 'for' loops
int main(void)
{
double h = 0;
double area = 0;
printf("CS201 - Lab 7 - Area Under The Curve\n");
printf("g(x) = XSquaredSinX\n");
double a = 0.00000;
double b = 3.14159;
int n = 2;
printf("a = %.5d, b = %.5d", a, b);
for (i = 1; i < 8; i++)
{
n = pow(n, i);
h = (b-a)/n;
for (j = 0; j < n;j++)
{
b = a + h;
Trap(XSquaredSinx, a, b, n, &h, &area);
a = b;
}
printf("n = %3d, h = %7.5d, area = %12.5d", n, h, area);
}
printf("h(x) = HalfCircle\n");
a = -2.00000;
b = 2.00000;
n = 0;
printf("a = %.5d, b = %.5d", a, b);
for (i = 1; i < 8; i++)
{
n = pow(n, i);
h = (b-a)/n;
for (j = 0; j < n;j++)
{
b = a + h;
Trap(HalfCircle, a, b, n, &h, &area);
a = b;
}
printf("n = %3d, h = %7.5d, area = %12.5d", n, h, area);
}
printf("f(x) = TestFunctopm\n");
a = -0.00000;
b = 12.00000;
n = 0;
printf("a = %.5d, b = %.5d", a, b);
for (i = 1; i < 8; i++)
{
n = pow(n, i);
h = (b-a)/n;
for (j = 0; j < n;j++)
{
b = a + h;
Trap(TestFunction, a, b, n, &h, &area);
a = b;
}
printf("n = %3d, h = %7.5d, area = %12.5d", n, h, area);
}
return(0);
}
CODE
/* Function designed to calculate area under a curve using Trapezoidal Rule
Trapezoidal rule: T = (h/2)(f(a) + f(b) + 2(summation from i = 1, to
n-1 of f(xi)
Pre: f(x) function, a, b, n defined.*/
#include <stdio.h>
void Trap(double f(double f_arg), // input: function under which
to find area
double a, double b, // input: interval for area calculation
double n, // input: number of trapezoids
double *h, // output:
double *area) // output: area under function on [a, b]
{
double heightA;
double heightB;
double heightAverage;
heightA = f(a);
heightB = f(b);
heightAverage = (heightA + heightB) / 2;
*area = (*area + (heightAverage * n));
}
CODE
/* Function designed to calculate value of (x^2)(six(x))
Pre: x defined*/
#include <math.h>
#include <stdio.h>
double XSquaredSinx(double x)
{
double X_2; // local variable for calculation of x squared
double sinX; // local variable for calculation of Sin(x)
double value; // local variable for ending calculation of
(x^2)(six(x))
X_2 = x * x; // calculation of x squared
sinX = sin(x); // calculation of Sin(x)
value = X_2 * sinX; // calculates (x^2)(six(x)) and assigns to
'value' variable
return value; // returns value
}
/* Post: (x^2)(six(x) calculated, returned*/
CODE
/* Function designed to calculate half-circle with radius 2
pre: x defined */
#include <math.h>
#include <stdio.h>
double HalfCircle(double x)
{
double h;
h = 4 - (x*x);
h = sqrt(h);
return h;
}
CODE
/* Function stub for TA use */
double TestFunction(double x)
{
return x;
}
// This function is not supposed to do anything, it's a stub method for
the use of my TA
When I compile normally I get the following error messages:
[lhanna@cslab10 lab7]$ make
gc -c Lab7.c
gc -o Lab7 Lab7.o Trap.o XSquaredSinx.o HalfCircle.o TestFunction.o
Lab7.o: In function `main':
Lab7.c:(.text+0x8b): undefined reference to `pow'
Lab7.c:(.text+0x1bc): undefined reference to `pow'
Lab7.c:(.text+0x2ed): undefined reference to `pow'
XSquaredSinx.o: In function `XSquaredSinx':
XSquaredSinx.c:(.text+0x22): undefined reference to `sin'
HalfCircle.o: In function `HalfCircle':
HalfCircle.c:(.text+0x3e): undefined reference to `sqrt'
collect2: ld returned 1 exit status
make: *** [Lab7] Error 1
When I compile using a method my C teacher suggested for problems with
connecting the math.h library, I get the follow error message:
[lhanna@cslab10 lab7]$ gc -lm -o Lab7 Lab7.c
/tmp/ccK7l5Nf.o: In function `main':
Lab7.c:(.text+0xf0): undefined reference to `XSquaredSinx'
Lab7.c:(.text+0xf5): undefined reference to `Trap'
Lab7.c:(.text+0x221): undefined reference to `HalfCircle'
Lab7.c:(.text+0x226): undefined reference to `Trap'
Lab7.c:(.text+0x352): undefined reference to `TestFunction'
Lab7.c:(.text+0x357): undefined reference to `Trap'
collect2: ld returned 1 exit status
I have no idea how to fix this.
Any help would be awesome.
Thanks,
Kira89