NEWS
COURSE INFO
TUTORS & LAB HOURS
GRADING & HOMEWORK
FAQs
|
 FAQs
- These are some of the questions and answers students might find usefull when there is no one else to ask.
- If you have any questions and you think they are important to every one, please e-mail Ji Lee to post your question on this page. Your question will be answered vie e-mail as well as this FAQ page.
-
|
| Q: Where can I find the solutions to the homework assignmetns? |
A: -
Here!
|
| Q: If your program for hw#7 part 3 keeps stealing one penny from your pocket..... |
A: -
IF your program keeps stealing one penny from you, here is a way to catch the theif.
The prime suspect of this particular case is the combination of Borland C compiler and
Microsoft Windows. If you do 0.01 * 100, the computer will return .999999 instead of 1.0.
Since int data type takes floor value, .9999 will be ßtored as 1, which will
mass up the result.
It seems to me that this does not happen in other systems, so if you are not
doing homework using the computers in the lab, you won't have this problem.
If you want to fix it, you can do
temp = amount * 100 + .1;
That will make .99999 to 1.099999, thus it will get your penny back. ;-)
|
| Q: How can I e-mail my hw7 to my TA? |
A: -
Case 1)
if you have access to e-mail program and if you know how to use them, you can save your
program in a disk and use your e-mail program to write an e-mail to your TA. You can
either "attatch" the file to the e-mail, OR you can also copy N paste to the e-mail
itself. Either way is fine.
Case 2)
If you DON'T have access to e-mail program, you can use your cse5a e-mail account and
telnet to send an e-mail to your TA. Here is how:
Save part one of your hw#7 as H:\hw7p1.c save part 2 of your hw#7 as H:\hw7p2.c
Save part 3 of yoru hw#7 as H:\hw7p3.c Save part 4 of your hw#7 as H:\hw7p4.c
You can use what ever name you want to use, but make sure that you have in H:\ directory
not H:\pc\bc45\bin\ something.. And remember the file names you saved. Please
ask one of the TAs if you need help
Now the saving program part is done.
If you look at the left bottom corner of the computer screen, you should be able to
see a button saying START. Click that
START button. Then it will show a menu looking somewhat like this:
'--------------------------`
| ACS Account Utilities |
|--------------------------|
| Program Files |
| Documents |
| Find File |
| Settings |
| Yadda Yadda |
| Run .. |
|--------------------------|
| Shutdown |
-----------------------------------
| START| stuff
-----------------------------------
Please excuse my ascii pictuer ;) I hope it's somewhat close to reality. hehe. Anyways.
See that blue RUN button? Click that button. Then it will
open a small window looking like this:
'-------------------------------`
| Run _ x|
|-------------------------------|
| Type yadda yadda ...yadda.. |
| ..yadda yadda... ... you |
| -------------------- |
|OPEN : | |V| |
| -------------------- |
| OK CANCEL BROWSE |
----------------------------------
| START| stuff
----------------------------------
See that blue box next to OPEN ? Type telnet ieng9.ucsd.edu. And click OK button. So, it should look like this right before you click OK:
'-------------------------------`
| Run _ x|
|-------------------------------|
| Type yadda yadda ...yadda.. |
| ..yadda yadda... ... you |
| -------------------- |
|OPEN : |telnet ieng9.uc...|V| |
| -------------------- |
| OK CANCEL BROWSE |
----------------------------------
| START| stuff
----------------------------------
Then click the OK button in red....
Okey. THEN it will open a window that says
'----------------------------------------`
| Telnet ieng9.ucsd.edu _ x|
|----------------------------------------|
| Connect Edit Terminal Help |
|----------------------------------------|
| |
|UNIX(r) System V Release 4.0 (ieng9... |
|login: |
| |
Type your cse5a login name ( like cs5aaa, cs5adc, or somehting like that). Then it will ask for
password
|
|UNIX(r) System V Release 4.0 (ieng9... |
|login: cs5a6 |
|Password: |
| |
Don't type cs5a6.(that's me) Then type your password. If you type login name & passwd correctnly, you should get school notices saying useless stuff. Just press < SPACE BAR > until you get
| ieng9.ucsd.edu % |
| |
Yer half way though... okey.? we'll take a break here.
UNDER CONSTRUCTION! MORE TO COME YET!
|
| Q: Where can I find practice final exam?? |
A: -
RIGHT HERE.. Please don't forget to bring your photo ID to your final .
|
| Q: Tips on Homework #7 part 1 |
A: -
Here are some tips on how you can calculate the day of the date. It is fact
that the day of the same day in next year is one day after the past year.
For example, January 1st 1998 was Thursday. Therefore January 1st 1999 is Friday.
And January 1st 2000 is Saturday. and of course January 1st 1997 was Wednesday.
However, there is a catch, if there is a leap day ( 2/29 ) then it skips one day.
For example, year 2000 is leap year. Thus it has an extra day. Thus, while 1/1/2000
is Saturady ( 1/1 is before the leap day), 1/1/2001 is Monday.
Then how do we find out the day other than January 1st? The assignment states that
January 1st of 1984 was Sunday right? Then count the day between 1/1/1984 and the
given date. For example, the given date is 1/11/1984, the days between 1/1/1984 and
1/8/1984 is 7 days. Notice that Sun, Mon, Tue,.. Sat repeats every 7 days.
Thus 7 days after Sunday should be Sunday again. let's say it was 1/11/1984. Then
there are 10 days between 1/1/1984 and 1/11/1984. Then it should be Wednesday.
Notice that 10 is 7 + 3. how about 1/4/94? 4 - 1 = 3 = 7 * 0 + 3. 1/18/84?
18 - 1 = 17 = 7 * 2 + 3. right? All the other days could be represented the same way
if the day is Sunday, days between = 7 * ( some number ) + 0
Monday, days between = 7 * ( some number ) + 1
Tuesday, days between = 7 * ( some number ) + 2
Wednesday, days between = 7 * ( some number ) + 3
Thursday, days between = 7 * ( some number ) + 4
Friday, days between = 7 * ( some number ) + 5
Saturday, days between = 7 * ( some number ) + 6
So, for example, if you want to find the day of 6/12/1998, the mathematical procedure
will look like this:
year 1984, January.
SUN MON TUE WED THU FRI SAT
1 2 3 4 5 6 7
8 ....
1998 - 1984 = 14
but leap years in between
1984, thus 14+1
1988, thus 14+1+1
1992, thus 14+1+1+1
1996, thus 14+1+1+1+1 = 18
since 18 = 7 * 2 +4 this tells you that
1/1/1998 is Thursday.
year 1998, Januray.
SUN MON TUE WED THU FRI SAT
1 2 3
4 5 6 7 8 ....
the day you want to find is June, thus
18 + Jan + Feb + Mar + Apr + May
= 18 + 31 + 28 + 31 + 30 + 31 = 169
since 169 = 24 * 7 + 1,
6/1/1998 is Monday.
year 1998, June.
SUN MON TUE WED THU FRI SAT
1 2 3 4 5 6
7 8 9 ....
Since the date you wanted is 6/12/1998,
169 + 12 - 1 = 180 ( you need to subtarct 1
because the first day a month is 1
not 0 )
Since 180 = 25 * 7 + 5
6/12/1998 should be Friday.
SUN MON TUE WED THU FRI SAT
1 2 3 4 5 6
7 8 9 10 11 12 13
which is correct. !
Since you do not have to worry about the dates before January 1, 1984, I wouldn't worry about the dates before 1/1/1984. okey? =)
|
| Q: Tips on HW 5 |
A: -
A couple things that might help you when you are doing your hw 5.
First, you don't need to worry about wrong inputs. Like, it's okey to
let the users pay only $10.00 when the total is $20.00. You will get
$-10.00 for change in that case, but you don't have to worry about those
stuff.
Another thing. if you use "%f" to print out the sum, it will look like
12.0000000 or something like that. To make it look like "$20.00" (only
two digits after "." ) use "%.2f" instead of "%f". So,
printf ("change = $%f\n", 12.0);
printf ("change = $%.2\f\n", 12.0);
will print
change = $12.000000
change = $12.00
to screen. the number between "." and "f" is the number of digits
that will be printed to screen. The computer will round UP the value
so 12.0570 will become 12.06 when printed on screen. okey? =)
|
| Q: Where can I find the solutions to the midterm? |
A: -
HERE!
|
Q: What does (float) a do?? |
A: -
If you have done hw#3 question #3 ( and I hope you are done with it by now), you
should have seen float keyword. To begin with,
float is one of the primary data type in C language. Just like
int, it stores number. However, while variable declared as
int can only store integer values ( -1, 0 ,1 ,2...),
float can store decimal numbers as well ( 1.34, -3.00,... )
For example:
int i;
float f;
i = 4.5;
f = 4.5;
What will be the values of i , and float? Since
i is declared as int, i can store only
integers.. Thus, the value of i after this code will be 4
instead of 4.5. However, f was declared as float
which can store decimal values, thus the value of f after
this short code is 4.5, instead of 4.
Then how do i print float numbers to screen? just like
int except you need to use %f for float, instead of
%d which is for int. For Example:
int i;
float f;
i = 4.5;
f = 4.5;
printf (" i = %d\n", i);
printf (" f = %f\n", f);
This will print out
i = 4
f = 4.5000000
to screen.
Now that we know what float is, what does (float) a do again?
Let's think of the following code:
int a = 4, b = 5;
float f;
f = a/b +1;
printf ("%d / %d + 1 = %f\n", a, b, f);
Notice a and b are declared as int. What do you think this
program will print to screen? Most people will think that it should print
4 / 5 + 1 = 1.8
to screen. However, computer doesn't think so. The computer will print out
4 / 5 + 1 = 1.0000000
to screen. WHY?? To answer this, you need to know how compilers evaluate
those expressions. When the compiler encounters this expression, it will read the
expression as (int) / (int ) + (int) and the compiler will return int
vaule for calculation between ints. In other words, int * int gives
your int even though the result is decimal number. And int / int
gives you int enen though the result might be decimal. Thus,
3 / 6 = 0
1 / 2 = 0
5 / 2 = 2
So, how do we make the compiler return a float value instead of int?
There are two ways of doing this. One of them is putting .0 (dot zero. not alphabet o)
to each number. For example: 3.0 / 6 or 3 / 6.0 or 3.0 / 6.0.
Then, since there is a float value involved in those expressions, the
compiler will give you float value for the result instead of int value.
For example:
3.0 / 6.0 = 0.500000
3 / 6.0 = 0.50000
3.0 / 6 = 0.50000
BUT 3 / 6 = 0.00000
However, you can't do this for the variables such as a or i. (no, you
can't do like a.0 and expect the computer to understand it). This is where
the typecasts, such as float come into action. What typecasts do is to convert
the data types if it's possible. For instance, float will convert other data
types such as int to float. Thus:
(float) 3 / (float) 6 = 0.5000
3 / (float) 6 = 0.500000
(float) 3 / 6 = 0.500000
3 / 6 = 0.50000
How about the code given in the Hw#3?
int a = 11, b= 5, c= 3;
float f;
f = ((float)a ) / ((float) b ) + c;
printf("f = %f\n", f);
f = (float) 11 / (float) 5 + 3 , and then 2.5 + 3, thus, 5.5. (float)
will not change or affect the value of a , thus a will remain as
integer 3.
what will happen if we get rid of (float)? Since the compiler will read it
as (int) 11 / (int) 5 + 3 , the result will be 2 + 3, thus 5 will be printed
to screen.
I hope this *short* explanation on float helps you guys understanding.. and if
you have any questions, e-mail us! =)
|
| Q: I have NO clue about hw#3. =( |
A: -
First, check out Notes on
while statement if you are having trouble understading what while statement does.
If you are still having trouble coming up the algorithm for hw#3, here is a little hint for ya ;)
what the assignment askes you to do it make a program to get 4
perfect numbers. A perfect number is
the number whose sum of all of its positive integer factors is equal to
the number itself. (except the number itself) for example:
4 is divisible by 1, 2
but 1+2 = 3, so 4 is not a perfect number
5 is divisible by 1
but 1 is not 5 (of course), thus, 5 is not a perfect number
6 is divisible by 1,2,3
and 1+2+3 =6 thus 6 is a perfect number.
Your program is supposed to repeat this procedure until you get 4
perfect numbers.
Still sounds tough? okey. here is even more detailed hint!
what you need to do is have two while
loops.. One of them will be nested inside the other one, so the program
will look somewhat like this:
main() {
....
....
while ( condition1 ) {
....
....
while ( condition2 ) {
....
....
}
....
....
}
}
( NOTE that you can have as many while loops nested inside
another while statement as you want. ) The outer loop ( the
first while statement) will make sure that you get 4 perfect numbers. In
other words, the outer while will repeat the inside statements until you
get 4 perfect numbers.The inner while loop will be used to get the sum of
all the factors except the number itself. The inner while loop will go
through the numbers from 1 to the original_number/2 and get the sum of the
factors. ( you only need to go up to original_number/2 because
original_number/2 is the greatest possible factor of original_number)
And after the inner while loop (the second while loop), you will need to
check if the sum of all the factors is equal to the original_number.
If they are the same, print out the original_number to screen and add
one to the counter ( so the the outer loop can stop running if the counter
becomes 4).
Still confused???
okey. then, here is a little sample program that might give you an
idea about what to do in your program. This program will print out the
factors of numbers 2 through 10. The basic structure of this sample
program is same as above.. two while loops (inner loop and outer loop).
The outer loop will make sure that the inner part will run until the
original_num becomes 10 (make sure that inner stætements will be executed
when original_num is 10). The inner loop will find all the factors of
given original_num. First think about what you need to do in each loop.
void main()
{
/* initize variables..*/
....
....
/* this loop will run until original number
is 10.. */
while ( condition1 ) {
/* now we need to find factors. */
/* this inner loop will go through
2 to orignial_num/2 and check
if the number is a factor or not.*/
while ( condition2 {
/* find if the number is factor
of original number and
if factor, print the value */
/* increment the number to test
the number */
}
/* increment original_number */
}
}
The code above is called pseudo code. Instead of writing in C
language first, write in half English half C first to figure out what to do.
And then fill in the blanks with real C code.. The following is the
completed code that will print the factors of numbers 2 thru 10.
/*
* FileName : factors.c
* Name : Ji Lee
* Login : cs5a6
* Descrpition: This program will print out
* all the factors of numbers
* from 2 to 10.
*/
#include < stdio.h >
void main()
{
/* initializing variables. */
int original_num, factor;
original_num = 2; /* starts from 2 not 1 */
/* outer loop.. iterates the original_num
from 2 to 10 */
while ( original_num <= 10 ) {
printf ("factors of %d are = ", original_num);
/* finding all the factors of
orignianl_num */
factor = 1;
while ( factor <= original_num /2 ) {
/* if the remainder is 0, it's
factor of the number..*/
if ( original_num % factor == 0 ) {
printf (" %d ", factor);
}
factor++;
}
printf ("\n");
original_num++;
}
return;
}
The output of this program looks like:
factors of 2 are = 1
factors of 3 are = 1
factors of 4 are = 1 2
factors of 5 are = 1
factors of 6 are = 1 2 3
factors of 7 are = 1
factors of 8 are = 1 2 4
factors of 9 are = 1 3
factors of 10 are = 1 2 5
|
| Q: I can't seem to print character '%'!!! \045 doesn't work either! |
A: -
In homework #2, there is a part some of you might want to print character '%'. Since '%' is a special character used to format your output, it won't printed to screen (or the rest of the output get all messed up..)
To print "2 * a % - b + c + 1" to screen :
printf ("2 * a %% - b + c + 1\n");
or
printf ("2 * a \045\045 -b + c +1\n");
Notice the usage of \045 in the second example. 45 is the octatal ASCII code of character %. So if you use \045 the compiler will substitute \045 with character %. HOWEVER, you have to use \045\045 twice to make it work right. That's because the compiler will just simply susbstitude \045 with '%'. As a result, print ("2 * a \045 -b + c +1\n"); will become the same as print ("2 * a % -b +c + 1\n"); which doesn't work properly..
The Borland C++ compiler we are using WILL print '%" character even though you use just single % instead of "%%". However, if you try to print an integer using "%d"after that, you won't be able to get the correct answer. So please make sure the you are using double %% instead of %
|
| Q: What shall I do if I can't make it to my tutor's lab hours? |
A: -
First, you can try to make appointments with your tutors by e-mail. Their e-mail addresses are listed in the Lab Hours Page. Make sure that you e-mail your tutor early enough. Remember that you have one week to get your homework assignment graded by your tutor.
Second, you can change your tutor to whoever you like and whoever fits your schedule the best. Just make sure that you e-mail both of the tutors (the one you are currently assignmed to and the new one ) because they have to exchange your records & stuff. However, the change is not guaranteed if the tutor you want has too many students already.
|
| Q: Is a * +-++b error? or is it a valid expression?
|
A: -
It IS a valid expression. When the compiler runs into those expressions, it translates +- as unary operator. From the precedence table, unary operators are on the same level as prefix, and they are done right to left. There fore, a * +-++b is the same expression as a * ( + ( - ( ++b ) ) ).
If a = 3 and b = 4;
a * +-++b , then
a * + - ++b , then
3 * ( + ( - ( 5 ) ) ) , then
3 * ( + ( -5 ) ) , then
3 * ( -5 ) , then
= -15.
Precedence of Operators
| Operators | Associatiavity |
| () ++(post) --(post) [] -> . :: | Left to Right |
| unary + unary - ++(pre) --(pre) ~ ! + - & * | Right to Left |
| * / % | Left to Right |
| + - | Left to Right |
| < <= > >= | Left to Right |
| == != | Left to Right |
| && | Left to Right |
| || | Left to Right |
| = += -= etc. | Right to Left |
NOTE: a+++b is valid as (a++)+b.. but a+++++ gives an error... This kind of programming style is not recommended (although it's good to know these stuff...) because it confuses you & the people who have to read your program.. (and it's a little bit inconsistant among compilers as well.. )
|
| Q: What about a++ * a++ ? or a++ + a++ + a++ + ++a ? |
A: -
The behavior of a++ * a++ is very inconsistant among compilers. (Notice that there are two or more increments of the same variable 'a' in the same expression). Each compiler responds differntly to these kind of expressions. Thus, it is NOT RECOMMENDED to use these kind of expressions in programs. The following is the code I used to test the increment stuff.
#include < stdio.h >
void main()
{
int a,b,c;
a = 3; b = 4; c = 5;
printf("a++ * a++ = %d\n", a++ * a++);
printf("a++ + a++ + a++ + ++a = %d\n",
a++ + a++ + a++ + ++a);
}
And these are the results I got:
gcc compiler from ieng9.ucsd.edu -------
a++ * a++ = 12
a++ + a++ + a++ + ++a = 27
cc compiler from ieng9.ucsd.edu --------
a++ * a++ = 9
a++ + a++ + a++ + ++a = 24
gcc compiler for pc --------------------
a++ * a++ = 9
a++ + a++ + a++ + ++a = 21
|
| Q: What is good commenting style? |
A: -
While grading the first assignments, I (Ji) couldn't help noticing the lack of comments in everyone's code. Here is my little example of how I would comment programs.
/*
* FileName : Sample.c
* Name : Ji Lee
* Login Name : cs5a6
* Assignment : #1
* Description: This programs reads two numbers
* from user, adds five other
* numbers to sum and prints out
* the result to the screen.
*/
#include < stdio.h >
void main()
{
int a, b, c, d, e, f, g, sum;
/* initializing the variables */
scanf ("%d%d", &a, &b);
c = 3;
d = 4;
e = 5;
f = 6;
g = 7;
/* computing the sum */
sum = a + b + c + d + e + f + g;
/* output the result */
printf ("the sum is = %d\n", sum);
}
Well, it looks like I put quite a lot of comments on such a short program. You do not have to comment on every single statement in your program, but you should comment enough to tell the readers what each part of your code does. Just like writing an essay, comments are like topic sentences of each paragraph. Let the "readers" know what you are trying to do. =)
|
Comments? eMail Webmaster
|
|