C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-02-2006, 11:26 AM   #1
Registered User
 
Join Date: Mar 2006
Posts: 6
Entering in an nth degree polynomial

For my Engineering Class I have to write a program that will take in an nth degree polynomial. The degree will always be positive and an integer. Then I have to use Simpson's Rule and the Rectangular rule to compare the numerical integration results, well... on to the point. I am having trouble with the entering of the polynomial. I don't know if I should have the subfunction be type void or not, because I am going to have most of my calculations done in subfunctions. Also I am unsure whether or not to have the user input the polynomial coefficients as a string or a character array. Any help or advice would be appreciated.

Code:
#include <stdio.h>
#include <stdlib.h>

void polynomial(char *);

main()
{

}

void polynomial(char *ptr)
{
	
	int degree;
	printf("\nEnter the degree of the polynomial you wish to integrate.");
	scanf("%d", &degree);
	ptr=(char *)malloc((degree+1) * sizeof(char));
	if(ptr= NULL)
	{
		puts("Memory Allocation Failed.");
	}

}
When I have
Code:
ptr=(char *)malloc((degree+1) * sizeof(char));
the degree+1 is for the entire length of the polynomial, for instance if you had a 2nd degree polynomial you would have 3 terms, x^2 + x + #.
Noah is offline   Reply With Quote
Old 03-02-2006, 09:02 PM   #2
The Richness...
 
Richie T's Avatar
 
Join Date: Jan 2006
Location: Ireland
Posts: 469
well what i would do is make your functions return the result of
the numerical integration - that way main can compare the two:
also you could use the maximum error calculation for simpsons
rule and see if the difference between the two methods is less
than it - just an aside. also, for reading in the coefficients, they're
gonna be integers, not chars - its not that easy to get a computer
to do calculations with variable names as opposed to numerical
values - dynamically allocate an array of ints, use a loop to
read in the correct coefficient into each element and then
implement your function - in my own opinion i would think it
better to enter the coefficients one by one. your output could
look like:

Code:
Enter degree: 2

Enter the coefficient of x^2: 1

Enter the coefficient of x: 0

Enter constant: 5

The polynomial is: x^2 + 5
getting output like that would be detailed (notice how it didn't
print x^1 and x^0? - just something i'd do if i had the time myself)

Good luck with implementing the numerical methods, hope i
answered your questions.

PS - you could avoid memory allocation and just define an array
large enough to store a reasonable number of coefficients - say
100. also, it wouldn't increase the complexity if you used double
for the coefficients, its up to you, but its always good to give a
user the freedom to enter in .87 if they so feel like it (and if the
same makes logical sense for the program of course!)
__________________
No No's:
fflush (stdin); gets (); void main ();


Goodies:
Example of fgets (); The FAQ, C/C++ Reference


My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition


ASCII stupid question, get a stupid ANSI
Richie T is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C program Degree to Radians Cyberman86 C Programming 5 05-04-2009 09:34 PM
Computing Degree Question cjwenigma General Discussions 2 09-17-2007 01:06 PM
large polynomial squeaker C++ Programming 2 03-22-2006 01:15 AM
Polynomial Unregistered C++ Programming 1 04-17-2002 05:00 PM
Polynomial Problem softcoder76 C++ Programming 5 03-01-2002 02:07 PM


All times are GMT -6. The time now is 01:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22