C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-31-2009, 12:38 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 4
Question Very new to c programming and need help

Hi,

I am taking a required c programming class at school, and I feel like the projects are way over my head. This is only the second project, and I am unsure of how to start it. I'm not asking for anyone to write the code for me. I just need some help starting the project.

The objective is to write a program based on the Taylor series of expansion.

The first part of the project is:

Develop a function in C that calculates the error between an exponential function evaluated at x and the Taylor Series approximation using only the first two terms, i.e. 1+x. It should take in the value of x and return the value of the error.

Any help would be greatly appreciated!!!
MG2610 is offline   Reply With Quote
Old 10-31-2009, 03:50 AM   #2
Robot
 
Join Date: Mar 2009
Posts: 258
Not accounting for precision loss, it sounds like you just need to return the absolute error, i.e.
Code:
double error = abs(1+x - exp(x))
abs and exp are standard math functions found in math.h.
Memloop is offline   Reply With Quote
Old 10-31-2009, 11:27 AM   #3
Registered User
 
Join Date: Oct 2009
Posts: 4
This is what I have so far, but I'm not sure where to go from here:

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

int main(void)
{
	int x;
	double error = abs(1+x - exp(x));
	
	printf("Enter a value for x \n");
	scanf("%d", &x);
	
	getchar();	
	
	return 0;
}
MG2610 is offline   Reply With Quote
Old 10-31-2009, 03:56 PM   #4
Registered User
 
Join Date: Sep 2006
Posts: 3,133
You need to calculate the error, *after* you have been given the value for x by the user's input.

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

int main(void) {
  int x;
  double error;
  printf("\nEnter a value for x: ");
  scanf("%d", &x);

  error = abs(1+x - exp(x));
  printf(" x is: %lf\n\n\n\t\t\t     press enter when ready", x);
  
  getchar();
  return 0;
}
Check that your compiler returns abs as a double, rather than an integer (real). Some compilers will do either a double or as an integer, so you have to tweak the code a bit to get the type you want.

Last edited by Adak; 10-31-2009 at 04:02 PM.
Adak is offline   Reply With Quote
Old 10-31-2009, 09:53 PM   #5
Registered User
 
Join Date: Oct 2009
Posts: 4
Okay I changed it around a little bit...

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

int main(void) {
	int x;
	char input1 = 'E';
	double error;
	
	printf("\n\nEnter E to calculate error\n");
	printf("Enter Q to quit program\n");
	scanf("%c", &input1);
	
	while( input1 != 'Q' )
	{
		if ( input1 == 'E' )
		{

			printf("\nEnter a value for x: ");
			scanf("%d", &x);
	
			error = abs(1+x - exp(x));
			printf("error is: %lf\n\n", error);
		}
		else
		{
			printf( "\nERROR! Please try again!\n\n" );
		}
	}

	return 0;
}
Let me know if this makes sense.

Thanks for the help!!
MG2610 is offline   Reply With Quote
Reply

Tags
help new to c programming

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 08:07 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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