Help with a program

This is a discussion on Help with a program within the C Programming forums, part of the General Programming Boards category; My prof. wantedme to write a C program that calculates the sine of x with a truncation error less than ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    Help with a program

    My prof. wantedme to write a C program that calculates the sine of x with a truncation error less than .000001. He also says that for each iteration of the series I should print out the iteration number, the calculated value, truncation error, and the C math librar calculated sine or x. I'm not sure whether I need the printf and the scanf that prompts the user to input a number, or what I should set myint equal to. If you can help in any way, I would greatly appreciate it. Thank you,
    Kristina

    #include <stdio.h>
    #include <math.h>
    main()
    {
    double iter, flipflop, num, fact, k, sinx, mynum;
    num=1;
    iter=1;
    fact=1;
    for(k=2; k<=iter; k++);
    {
    fact=fact*k;
    }
    do
    {
    pow(num,iter);
    sinx=sinx + num/fact*flipflop;
    printf("truncation error %f\n",sinx);
    sin(num);
    ~~> printf("please enter a number\n");
    ~~>scanf("%d " , mynum);
    printf("iteration number %f\n", iter);
    printf("math library calculation %f\n",sin(num));
    printf("the calculated value is %f\n",sinx);
    iter=iter+2;
    flipflop=-1;
    }
    while(num/fact <= .000001);
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    You and everyone else... Judging by the time difference between your post and this other one, I'd say your homework's about due now, huh?

    http://www.cprogramming.com/cboard/s...threadid=10837

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Well you've got most of the bits for the airplane, but it's no where near able to fly yet.

    Some things
    1. for(k=2; k<=iter; k++);
    See that ; at the end?
    This loop does exactly nothing, and since iter is 1 when it enters the loop, it does nothing zero times

    2. {
    > fact=fact*k;
    > }
    This isn't being controlled by the previous loop, so it happens just once. It wouldn't have happened at all, if you'd not put the trailing ; on the for loop.

    3. unassigned results
    > pow(num,iter);
    > sin(num);

    4. flipflop=-1;
    So it starts off uninitialised, then it gets stuck at -1
    I think you meant to
    a) initialise it to 1
    b) say flipflop = -flipflop;

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    10
    Thanks Salem, but what do you suppose iter should be equal to??

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    A number you increment until
    "with a truncation error less than .000001"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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