======================================================================== Subject: Homework 4, question 2 Clarification: The grades entered to the program should be numeric grades (obvious from the program listing [we did in class]). --Azer ======================================================================== Subject: HW4 #2 For question #2 in Homework 4, you are expected to turn in a listing of the program and a script showing that it was successfully compiled and executed. --Azer ======================================================================== Subject: Decimal places Someone asked: > Rob, is it o.k if you leave your answers with all the zeroes after the > decimal point like 3.400000000000000000000e+01 in your program for homework. Yes, as long as the answer is correct. Write() and writeln(), by default, display real numbers in scientific notation, i.e., the above is read as "3.4 times ten to the 1st power", in other words, its 34. Rob ======================================================================== Subject: Rounding a decimal number to an integer in Pascal Someone asked: > Is there a way in Pascal to have it drop whatever appears after a > decimal point? In other words, can you force a quotient to be an integer > rather than a real number? In still more (and better) words, can you > round off numbers with pascal? Well, there is more than one way to do things like this. One way is to use the function trunc() to give you the integer part of a real. For example, suppose x is a real and n is an int, the value of x is 34.6, then n := trunc(x); The value of n is 34. If you want the value rounded, you can do: n := trunc(x + 0.5); Now, the value of n is 35. If you are only concerned with the value being printed out with no decimal places, you can tell writeln() to do so. You could do: writeln('x is ', x:0:0); See below for more about what the above writeln() is doing. Rob ======================================================================== Subject: Telling write() or writeln() how many decimal places to print Someone asked: > I'm having a little bit of a problem with my program. It compiles, and > runs just fine, however when it prints out the Max, Min and Average > grades it puts it into scientific form. This causes the average to be > something like 6.940000000e01 instead of 69.4. Is there anyway I can > eliminate this? How about if I multiply all of the final numbers by > negative 10? How would I put the negative in there? You don't want to multiply it by anything since that would change the answer, i.e., 6.940000000e01 is really "6.94 times ten to the 1st power", which is exactly 69.4. You can leave it in scientific notation if you want. If you want to convert the real number to an integer, you can do so, there is a decription of how to do so (see "Rounding a decimal number to an integer in Pascal" above). You can also tell write() and writeln() to print out a real number in so many spaces with a certain number of places after the decimal point. For example, suppose you have a variable "x" of type "real": x := 45.67; writeln('X is ', x:6:1); This code says to print out x's value using 6 positions (for the whole number, i.e., integral part, decimal point and decimal part) and 1 decimal place. This would cause the output to be: X is 45.7 Since we wrote out the number in 6 places, but the number only took up 4 places (i.e., the digit "4", "5", the decimal point and the "7"), then it puts 2 blanks before the number. Also notice that the number is rounded. Similarly, suppose you did: x := 67.342; writeln('X is ', (x / 10):5:2); Notice the parentheses around the expression. The output would be: X is 6.73 ------------- Rob