Frequently Asked Questions about HW1


Q.
For Part 2, do we use integer or floating point numbers for dollar amounts?

A.
Amounts will be all be in whole dollars, so use integers.


Q.
I tried to write my programs so that if -1 is entered as an account # the loop stops. However, I didn't get that to work.

A.
Remember that the 2 ways to stop a loop are in what you put in the "condition" part of the loop AND there is a C command you can use to immediately get out of a loop.


Q.
In the example for Part 3, there are exponents noted with a ^. Using ^ for exponents in my program is not working out, however.

A.
^ is just notation. It does something other that an exponent in C. If you want to raise something to a power, use the pow() function and the "-lm" flag with the compiler. You can also generate the powers of 2 you need without pow().


Q.
When compiling my program...
gcc ... h1pX.c

I get:

Undefined                       first referenced
 symbol                             in file
pow                                 /var/tmp/cca001Gi1.o
ld: fatal: Symbol referencing errors. No output written to a.out
What's the problem?

A.
Although library functions normally get linked in automatically, with math functions, like pow(), you must use the -lm (that's "ell" and "em") flag to the compiler, as in:

gcc ... h1pX.c -lm

Don't forget you still need to include the proper header file for function prototypes. For example, when using pow(), you should include math.h.


Q.
Do we have to prompt for input?

A.
Any program that could be run by a user who would have to type in information should prompt for that information. In other words, yes.


Q.
Do we need any error checks in our programs?

A.
No real error checks are necessary for this 1st assignment.


Q.
Should the programs work with input coming from a file OR typed in?

A.
They should work for BOTH.

All you have to do is use normal scanf's, which will allow you to type in data at the keyboard AND will also allow you to redirect in some input file. Redirecting will be discussed in the first lab, but you can read about it in the UNIX Reference.


Q.
For the 0's and plurals of Part 1, is there another way to do them instead of using a lot of if statements for all the combinations?

A.
First of all, there shouldn't be too many combinations. I.e., you can deal with how to print out the hours without considering the minutes and seconds. Likewise, you can deal with how to print the minutes without considering the hours or seconds. You should have hardly any "if" statements that test all 3 values.

You do basically have to perform a "test" to determine if an "s" belongs or not. So, without doing something odd, you'll use either an "if" OR the conditional statement, i.e.:

(cond) ? (value if cond is true) : (value if cond is false)

You should look this last one up in your C reference.


Q.
Can we read in the fractional part as an int?

A.
That won't work because you'll lose the leading zeros. E.g., with:

10101.0011
You end up with "11" as the fractional part.


Q.
Do we have to read in the fractional part as a string? Can we read in the parts as floating point numbers? Or, can we read in the integral part as a string?

A.
Any of those will be acceptable. Make sure precision errors do not present a problem if you use floating point variables. However, these alterations were neither part of the original write-up or the adjusted one and we generally require students to follow instructions. We may not be as flexible in the future.


Q.
How do we deal with the whole number part of the binary number (stored in an int) and pick out each of its bits?

A.
Suppose you need to process 1101. First, you want to extract the rightmost 1. So, how would you do that with division or modulus? Once you get that 1 and use it in accumulating the decimal number, you want to remove it from the number so that you are left with 110. How can you get 110 from 1101 using division or modulus? You need to repeat (suggesting a C construct that does something many times) this process until there are no digits left, i.e., until the number reaches what value?


Q.
Are we not supposed to store the fractional part in a floating point variable? The write-up says to read it in as a string.

A.
Although you will use a string to read in the fractional part, you will convert it into a floating point number.


Q.
In Part 3, what if the whole number part of the binary number is too big to fit in an int?

A.
Integers on our machines are 4 bytes (32 bits), and thus, can store numbers bigger than 2 billion. They are large enough for the numbers you must test for this assignment.

If your home machine has ints that are too small, you may use type "long int" (read in with format "%ld", not "%d"). However, homeworks are supposed to run on our machines.


Q.
I have the whole number part of the binary number in a string. I use an index (like i) to go through the string from character 0 to size-1. I also use i for the power of 2. I'm not getting the correct value when I convert the whole number part?

A.
The powers of 2 (just like in a decimal number) increase from right to left. You are processing the characters from left to right.

Instead, create a loop that goes from size-1 to 0. You can get the size of a string by calling the function strlen() on it. This function is prototyped in string.h.

Of course, if i is the index you use to go through the characters, you'll need something else to go through the proper powers of 2.


Q.
I tried reading both parts as a string (format "%s.%s") but that doesn't work?

A.
The first "%s" will end up reading in the entire number in that case, e.g., since something like:

10101.110

is essentially 1 string.

You need to read in the whole thing as a single string. You can still go through it character by character and when you hit the period (.) you'll know the fractional part follows.


Q.
For Part 2, what does it means "Are debits and credits represented by positive or negative numbers (or both)?"

A.
It just asks you whether these numbers are positive or negative in our data files. Your calculations will depend on which it is.


Q.
For Part 3, my program doesn't work for numbers without fractional parts, like "110"?

A.
You only need the program to work for numbers of the form "XXX.XXX". We couldn't have suggested using format "%d.%s" if we required it to work for numbers without fractional parts.


Q.
For Part 3, a result might be like 205.687500 in decimal. Are the trailing 0's ok?

A.
Yes.


Q. [New]
For Part 3, could you explain what:
digit = fract[i] - '0';

means? How does this line convert the character to a digit? What does '0' mean?

A.
It subtracts the ASCII value of '0' from the ASCII value of the character stored in fract[i]. '0' is the character zero. Since the digit characters ('0' through '9') have sequential ASCII values, the subtraction gives you the integer value of the digit.

As an example, suppose fract[i] holds the character '1'. The ASCII value of '1' is 49. Subtracting the ASCII value of '0' (which is 48) gives you the integer 1.


BU CAS CS 113 - FAQ - HW1