A.
Amounts will be all be in whole dollars, so use integers.
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.
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().
gcc ... h1pX.c
I get:
What's the problem?Undefined first referenced symbol in file pow /var/tmp/cca001Gi1.o ld: fatal: Symbol referencing errors. No output written to a.out
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
.
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.
A.
No real error checks are necessary for this 1st assignment.
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.
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.
int
?
A.
That won't work because you'll lose the leading zeros. E.g., with:
You end up with "11" as the fractional part.10101.0011
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.
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?
A.
Although you will use a string to read in the fractional part, you
will convert it into a floating point number.
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 int
s that are too small, you may
use type "long int
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.
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.
A.
It just asks you whether these numbers are positive or negative in our
data files. Your calculations will depend on which it is.
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.
A.
Yes.
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.