Very new to c programming and need help

This is a discussion on Very new to c programming and need help within the C Programming forums, part of the General Programming Boards category; Hi, I am taking a required c programming class at school, and I feel like the projects are way over ...

  1. #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!!!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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.

  3. #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;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  5. #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!!

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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