Page 1 of 1

Homework 4, due October 2, 2008.

Posted: Sun Sep 28, 2008 1:04 pm
by goodwine
Reading: chapter 4 from the course text.

Collaborative Problem:
Write a program that computes an approximation for the cosine function by computing a Taylor series about 0 that does the following:
  1. prompts the user to enter an angle between -90 and+90 degrees;
  2. checks that what the user entered is in the right range, and if not, tells the user that what they entered was invalid and then prompts them again to enter an angle again;
  3. computes the series up to the first nonzero term that has a magnitude that is less than 10E-6 times the magnitude of the sum of all the preceding terms;
  4. compares the computed value to the value returned by the COS() function (which presumably is pretty accurate).
Notes:
  1. hopefully it's clear to everyone that the angle must be converted to radians;
  2. use the proper logical operator in the IF statement when checking that the user entered a proper value; and,
  3. the ABS() function will probably be useful.
Individual Problem:
Write a computer program that prompts the user to enter:
  1. the percentage grade the user has in the class;
  2. the average percentage grade for the class; and,
  3. the standard deviation for the class;
and using the "guaranteed grade" system on the course syllabus, print the minimum letter grade the student will receive.

The program should be "user friendly" in that it properly prompts the user to enter the data (e.g., do you enter ".85" for 85%, or do you enter "85") and checks that what the user is entering makes sense. If it does not make sense, then the user should be instructed what is the right form for the data and prompted to enter the data again.

Re: Homework 4, due October 2, 2008.

Posted: Mon Sep 29, 2008 2:29 am
by mnguye10
For the cos(x) problem, I found that if the absolute value of the angle entered is close enough to 90 degrees, (for me, it was values of 88.140071 something), then the program flips out and goes into an infinite loop but works fine if the absolute value is 88.140071 or less. I tried printing out the values through the loop and found that it printed NaN. It does the same with both real and double precision data types. Should the user input range be restricted accordingly or should the program display an error message or take other measures for the values that would cause an infinite loop?

Also, on the minimum grade thing, there's no case for the overall grade being exactly equal to the mean - 2.0 standard deviations. Should that be grouped with the D or F category?

Re: Homework 4, due October 2, 2008.

Posted: Mon Sep 29, 2008 6:54 am
by goodwine
mnguye10 wrote:For the cos(x) problem, I found that if the absolute value of the angle entered is close enough to 90 degrees, (for me, it was values of 88.140071 something), then the program flips out and goes into an infinite loop but works fine if the absolute value is 88.140071 or less. I tried printing out the values through the loop and found that it printed NaN. It does the same with both real and double precision data types. Should the user input range be restricted accordingly or should the program display an error message or take other measures for the values that would cause an infinite loop?Also, on the minimum grade thing, there's no case for the overall grade being exactly equal to the mean - 2.0 standard deviations. Should that be grouped with the D or F category?
I'll have to look into the first question.

You can put the -2.0 stdev either way.

Re: Homework 4, due October 2, 2008.

Posted: Mon Sep 29, 2008 7:09 am
by goodwine
mnguye10 wrote:For the cos(x) problem, I found that if the absolute value of the angle entered is close enough to 90 degrees, (for me, it was values of 88.140071 something), then the program flips out and goes into an infinite loop but works fine if the absolute value is 88.140071 or less. I tried printing out the values through the loop and found that it printed NaN. It does the same with both real and double precision data types. Should the user input range be restricted accordingly or should the program display an error message or take other measures for the values that would cause an infinite loop?
I still haven't worked it out myself yet, but are you using sufficient precision? For angles where cos() is near zero, it will be difficult to have the next term be 10^-6 less than the sum. Also, it will never quit for cos(90) since that is zero.

Re: Homework 4, due October 2, 2008.

Posted: Tue Sep 30, 2008 2:08 am
by mnguye10
I found the issue; I was using an integer for the factorial accumulator and it apparently broke the int limit and went to zero for the higher angle values, giving a division by zero and setting my term value to infinity.

Re: Homework 4, due October 2, 2008.

Posted: Wed Oct 01, 2008 5:19 pm
by goodwine
Someone asked me:
Professor Goodwin,
whenever I us a goto, it will not redo the read statements nor the conditionals.
Is there something I need to do to cause those statements to work after a go to?
Thank you
There isn't anything you have to do to get a statement to work again when a goto points to it. I can only speculate, but some of the programs I saw in my office hours had gotos that told the program to go to a line inside a do loop or inside an if-then block. Those definitely won't work because if the program goes there, it doesn't know what the value of the index should be when it jumps into it from outside the loop. You can only goto the beginning of a do loop or the beginning of an if statement.

Re: Homework 4, due October 2, 2008.

Posted: Wed Oct 01, 2008 5:22 pm
by goodwine
Someone asked me:
I'm trying to run my program for the Taylor series of the cosine function right now and I keep getting the error message "Floating Exception"...what does that mean?
That usually happens with a division by zero.