CISC105 - General Computer Science
Homework #2
Making Decisions, Arrays and Functions
1. (6) Write a simple 'if' statement to keep a count of all
grades that are less than 60.
2. (6) Using the following code write an 'if-else' statement to
display a message stating whether 'number' is even or odd.
remainder = number % 2;
3. (6) Write a compound 'if' statement to keep a count of all
grades that are 80 through 89 inclusive.
4. (6) Rewrite the compound 'if' in problem 3 above using a
nested ‘if'.
5. (6) Write an 'else-if' statement to keep a count of grades in
the following ranges: 90 and above, below 60, 60 through 74,
75 through 89.
6. (6) What is the value of result in the following expression?
a = -10;
(int) result = ( a < 0) ? ( a * -1) : ( a * a );
7. (6) Circle the 'printf' statement that will be executed in
each of the following 'if' statements.
remainder = 1;
if ( remainder )
printf ("The number is odd\n");
else
printf ("The number is even\n");
remainder = 0;
if ( ! remainder )
printf ("The number is even\n");
else
printf ("The number is odd\n");
8. (6) Given the following code write a statement that sets the
50th element in the grades array to 95.
int grades[100];
9. (6) Given the following code write two lines of code that
will initialize all the elements in the grades array to zero.
int grades[100];
10. (6) Write the code to initialize a character array containing
all digits needed ( 0 thru F ) to convert a number to Hexadecimal.
11. (6) Write the code to initialize all the elements in the
following array to zeros.
int matrix[3][3];
12. (6) Rewrite the following code using an assignment operator.
array[index] = array[index] * 2;
(28) The remaining questions require a TRUE(T) or FALSE(F) answer.
13. ____ The variables defined in a function that appear before
the open brace that defines the body of the function
are known as formal parameters.
14. ____ The variables defined inside the body of a function are
known as automatic local variables.
15. ____ Local variables can only be accessed by the function in
which they are defined.
16. ____ The result that is returned by a function does not have
to be assigned to a variable.
17. ____ If the declaration of the type returned by a function
is omitted, then the C compiler assumes that the
function will return an integer.
18. ____ When a variable is passed to a function as an argument,
its value is automatically copied into the formal
parameter so that any changes made to the formal
parameter in the function affect only the formal
parameter and not the variable that was passed to the
function.
19. ____ The result returned by a function can be used in an
arithmetic expression.
20. ____ The type of argument that is passed to a function must
agree with the type of the argument as declared inside
the function.
21. ____ Many different functions may contain formal parameters
and local variables with the same name.
22. ____ It is possible to pass the value of an array element
and even an entire array as an argument to a function.
23. ____ If a function changes the value of an array element,then
that change will be made to the original array
that was passed to the function.
24. ____ For a two-dimentional array, the number of rows may be
omitted, but the declaration must contain the number of
columns of the array.
25. ____ Any function in a program can access the value of a
global variable and can also change its value.
26. ____ Static variables are initialized once only at the start
of the program.