Exercise Solutions
Concept Questions
Question 1
The conditional operator is an expression that produces a value; it should be used when you need to evaluate a condition and use the resulting value. It is not a control flow statement and should not be used to perform actions.
Incorrect usage (treats ?: like if/else):
(x > 0) ? printf("positive\n") : printf("negative\n");
This discards the result of the operator and uses it only for side effects.
Correct usage (uses ?: as an expression):
printf("Number is %s\n", (x > 0) ? "positive" : "negative");
Here, the operator produces a string value that is used by printf().
Question 2
Output:
Maximum: 10
Explanation: The conditional operator is appropriate here. The condition x > y evaluates to false (5 is not greater than 10), so the operator returns y (10). This value is assigned to max and printed. This is a proper use of the conditional operator as an expression.
Question 3
Fall-through behavior: When a case label matches in a switch statement, execution begins at that point and continues through subsequent statements until either a break statement is encountered or the end of the switch block is reached.
Importance of break: The break statement terminates the switch block, preventing execution from continuing into the next case. Without break, the program will execute all statements from the matching case onward, which is usually unintended and causes logical errors.
Example:
switch(x){
case 1:
printf("One\n");
// NO break - falls through to case 2
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
}
// If x == 1, output is: One\nTwo\n
// If x == 2, output is: Two\n
Question 4
In C, the name of an array is a pointer to the first element of that array. When you declare an array like int myArray[10], the name myArray resolves to the address of the first element (&myArray[0]).
Because of this relationship, an array can be passed to a function expecting a pointer parameter. The array is treated as a pointer when passed as an argument. This is why function parameters can be declared as either int array[] or int* array — they are equivalent.
Question 5
Output:
In main: sizeof(myArray) = 200
In printSize: sizeof(arr) = 8
(Note: The pointer size may be 4 or 8 bytes depending on the system; 8 bytes is typical for 64-bit systems)
Explanation: In main(), myArray is a locally-declared array of 50 integers. On most systems, an int is 4 bytes, so sizeof(myArray) returns bytes.
In printSize(), the parameter arr is a pointer (the array decayed to a pointer when passed). sizeof(arr) returns the size of a pointer, not the original array. This is why sizeof() cannot be used to determine array capacity in function parameters.
Question 6
When an array is passed to a function, it is treated as a pointer. The sizeof() operator on a pointer returns the size of the pointer itself (typically 4 or 8 bytes), not the size of the original array. Therefore, sizeof() cannot reliably determine the array size or capacity within a function.
The only way to know how many elements are in an array passed to a function is to pass the size as a separate parameter:
void processArray(int array[], int size)
{
// size tells us how many elements are in the array
}
Walkthrough
Exact Output:
Conditional
Programming
Part a
void getGrade(int score)
{
switch(score / 10){
case 10:
case 9:
printf("A+");
break;
case 8:
printf("A");
break;
case 7:
printf("B");
break;
case 6:
printf("C");
break;
case 5:
printf("D");
break;
default:
printf("F");
}
}
Part b
int getDaysInMonth(int month, int year)
{
int days;
int isLeapYear = (year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0);
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = isLeapYear ? 29 : 28;
break;
default:
days = -1; // Invalid month
}
return days;
}
Part c
#include <stdio.h>
void printOddEven(int numbers[], int used)
{
for (i = 0; i < used; i++){
printf("array[%d] = %d which is %s\n", i, numbers[i], (numbers[i] % 2 == 0) ? "even" : "odd");
}
}