Question 01
[1 mark] Explain what the following program does.
#include <stdio.h>
int main() {
int x, y = 0;
while (1) {
scanf("%d", &x);
if (x == -1) {
break;
}
y++;
}
printf("%d\n", y);
return 0;
}
Answer 01
This program counts how many integers the user inputs until the user enters -1 [0.5 mark], at which point the program stops and prints the count [0.5 mark].
Question 02
[3 marks] The following C program contains multiple errors. Rewrite the program in a correct way. Assume the program is intended to determine whether an input string matches a specific keyword.
#include <stdio.h>
int main() {
char keyword[10] = "password"
printf("Enter the keyword: ")
scanf("%d", input)
if (input == keyword) {
printf("Access Granted\n")
} otherwise {
printf("Access Denied\n"
}
}
Answer 02
#include <stdio.h>
#include <string.h>
int main() {
char keyword[10] = "password";
char input[10];
printf("Enter the keyword: ");
scanf("%s", input);
if (strcmp(input, keyword) == 0) {
printf("Access Granted\n");
} else {
printf("Access Denied\n");
}
return 0;
}
Adding #include
<string.h> |
[0.5 mark] |
|---|---|
Adding char
input[10]; |
[0.5 mark] |
Changing scanf("%d",
input); to scanf("%s",
input); |
[0.5 mark] |
or scanf("%d",
input); to fgets(input,
sizeof(input), stdin); |
|
Changing (input
== keyword) to (strcmp(input,
keyword) == 0) |
[0.5 mark] |
Changing otherwise to else |
[0.5 mark] |
Adding return
0; |
[0.5 mark] |
| Fixing the semicolons, etc. | [0.5 mark] |
You only get a maximum of 3 marks.
Question 03
[4 marks] Consider the following program.
-
Show the final output of the program.
-
Explain what the program does in general.
-
Change the value
{0, 7, 8, 10, -4}such that the lineprintf("Output1n");is executed.
#include <stdio.h>
int main() {
int arr[5] = {0, 7, 8, 10, -4};
int x = arr[0];
int y = arr[0];
for (int i = 1; i < 5; i++)
{
if (arr[i] > x){
y = x;
x = arr[i];
}
else if (arr[i] < x && arr[i] > y){
y = arr[i];
}
}
if (x == y){
printf("Output1\n");
}
else{
printf("Output2: %d\n", y);
}
}
Answer 03
-
The final output is
Output2:8. [1 mark] -
The program computes the second-largest element of an integer array, then outputs the result. [2 marks]
-
Any array where at least the two largest elements are equal. [1 mark]
Question 04
[5 marks] Write a C program that finds and prints all pairs of numbers in an array whose sum is equal to a given target value. Your solution should also account for the case when no pair is found.
The following is an example execution of the program.
Enter the size of the array: 5
Enter 5 elements of the array:
1 2 3 4 5
Enter the target sum: 5
Pairs of numbers whose sum is 5:
(1, 4)
(2, 3)
Answer 04
#include <stdio.h>
int main() {
int n, targetSum;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
// Input the array elements
int arr[n];
printf("Enter %d elements of the array:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the targetSum value
printf("Enter the target sum: ");
scanf("%d", &targetSum);
// Find and print pairs whose sum equals the target
printf("Pairs of numbers whose sum is %d:\n", targetSum);
int found = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == targetSum) {
printf("(%d, %d)\n", arr[i], arr[j]);
found = 1;
}
}
}
if (!found) {
printf("No pairs found.\n");
}
return 0;
}
To get the full mark you need the nested loop and the correct condition inside the conditional statement [4 marks]. In addition to this, you need the flag to be updated inside the loop, and checked outside it [1 mark].
Question 05
[5 marks] Write a C program to reverse the digits of a given positive integer n, where n is provided by the user. Display the reversed number.
The following is an example execution of the program.
Enter a positive integer: 120
The reversed number is 21.
Answer 05
#include <stdio.h>
int main() {
int n, reversed = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (n != 0) {
reversed = reversed * 10 + (n % 10);
n /= 10;
}
printf("The reversed number is %d.\n", reversed);
return 0;
}
Your solution must work for any positive number [5 marks].
Question 06
[2 marks] Write a C program to validate a 4x4 Sudoku board. The board is 4x4 grid of numbers 1 to 4, and is valid if:
-
Each row contains the numbers 1-4 without repetition.
-
Each column contains the numbers 1-4 without repetition.
-
Each of the four 2x2 subgrids contains the numbers 1-4 without repetition.
The figure to the right shows an example valid Sudoku board.
The program should take a predefined 4x4 grid as input and output either Valid or Invalid. Assume that cells containing 0 represent empty spaces and should not affect the validation.
The following grid is valid.
1 4 3 2
3 2 4 1
4 1 2 3
2 3 1 4
The following grid is invalid.
1 4 3 2
3 2 4 3
4 1 2 4
2 3 1 4
Solution 06
#include <stdio.h>
#define N 4
// Function to check if a number already exists in the row
int isInRow(int board[N][N], int row, int num) {
for (int col = 0; col < N; col++) {
if (board[row][col] == num) {
return 1;
}
}
return 0;
}
// Function to check if a number already exists in the column
int isInCol(int board[N][N], int col, int num) {
for (int row = 0; row < N; row++) {
if (board[row][col] == num) {
return 1;
}
}
return 0;
}
// Function to check if a number already exists in the 2x2 subgrid
int isInSubgrid(int board[N][N], int startRow, int startCol, int num) {
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) {
if (board[startRow + row][startCol + col] == num) {
return 1;
}
}
}
return 0;
}
// Function to validate the Sudoku board
int isValidSudoku(int board[N][N]) {
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
int num = board[row][col];
if (num != 0) {
// Check row, column, and subgrid constraints
if (isInRow(board, row, num) ||
isInCol(board, col, num) ||
isInSubgrid(board, row - row % 2, col - col % 2, num)) {
return 0; // Invalid board
}
}
}
}
return 1; // Valid board
}
int main() {
int board[N][N] = {
{1, 2, 3, 4},
{3, 4, 1, 2},
{4, 0, 2, 1},
{2, 1, 4, 3}
};
if (isValidSudoku(board)) {
printf("Valid\n");
} else {
printf("Invalid\n");
}
return 0;
}