CISC105 - General Computer Science
Homework #1
Variables and Program Looping
1. (5) Identify syntactic errors in the following program.
main ()
(
INT sum;
/* COMPUTE RESULT
sum = 25 + 37 - 19
/* DISPLAY RESULTS */
printf ("The answer is %d\n" sum);
}
2. (2) What output might you expect from the following program?
main ()
{
int answer, result;
answer = 100;
result = answer - 10;
printf ("The result is %d\n", result + 5);
}
3. (24) Which of the following are invalid variable names?
sum char 6_05
_calloc Xx q
floating _2131 one_two_3
ReInitialize 2Gether A$
4. (24) Which of the following are invalid constants?
624.819 0x11.2 0X0G1
06 0xABCD 475L
0Xec02 0L -623.81
0983 -11E-11 06677
5. (2) What output would you expect from the following program?
main ()
{
char c, d;
c = 'd';
d = c;
printf ("d = %c\n", d);
}
6. (24) Give the format or conversion characters for displaying
the following:
int hex long octal
float octal short int
double long int short hex
char long hex short octal
7. (2) What is the value of result in the following expression?
int a = 10, b = 20, c = 30, d = 40, e = 50;
float result;
result = c + d - b + e % a * c / d;
8. (2) What is the value of result in the following expression?
int result;
float x = 30, y = 7;
result = x / y;
9. (2) What is the value of result in the following expression?
int m = 75;
float n = 30, result;
result = m / n;
10. (2) How many times will the following loop be executed?
i = 0;
for ( ; i < 100; i = i + 1)
i = i + 4;
11. (5) Express the following statement using the increment operator.
hours_worked = hours_worked + 1;
12. (6) Rewrite the following 'for' loop using a 'do' loop.
for ( x = 1; x <= 100; x = x + 1)
salary = hours_worked * pay_rate;