////////////////////////////////////////////////////////////////////////////////
// QUESTION 1
////////////////////////////////////////////////////////////////////////////////
#include
#include
#define MAX_SIZE 100
// Scans a target character and integer n, finds the first occurrence of the
// target in the array, then prints the next n elements each on a new line.
// The target is always present; n is always in range (see spec).
void print_from_target(int size, char array[MAX_SIZE]) {
char target;
int n;
printf("Please enter a target character: ");
scanf("%c", &target);
printf("Please enter an integer: ");
scanf("%d", &n);
int first = 0;
while (array[first] != target) {
first++;
}
for (int i = 0; i < n; i++) {
printf("%c\n", array[first + 1 + i]);
}
}
// This is a simple main function which could be used
// to test your print_from_target function.
// It will not be marked.
// Only your print_from_target function will be marked.
int main(void) {
char test_array[MAX_SIZE] = {'b', 'a', 'n', 'a', 'n', 'a'};
// If you modify the above array for your own testing,
// you may also need to modify the size variable below accordingly
int size = 6;
print_from_target(size, test_array);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// QUESTION 2
////////////////////////////////////////////////////////////////////////////////
#include
#include
#define NUM_COLS 6
// array_count_odds_evens should return how many rows contain exactly as many
// odd values as even values (zero is treated as even).
// Return 0 if no row is balanced.
int array_count_odds_evens(int num_rows, int array[][NUM_COLS]) {
int count = 0;
for (int row = 0; row < num_rows; row++) {
int odds = 0;
int evens = 0;
for (int col = 0; col < NUM_COLS; col++) {
if (array[row][col] % 2 == 0) {
evens++;
} else {
odds++;
}
}
if (odds == evens) {
count++;
}
}
return count;
}
// This is a simple main function which could be used
// to test your array_count_odds_evens function.
// It will not be marked.
// Only your array_count_odds_evens function will be marked.
int main(void) {
int test_array[][NUM_COLS] = {
{1, 2, 3, 4, 5, 6},
{2, 2, 2, 2, 2, 2},
{0, 1, 2, 3, 4, 5},
{1, 1, 1, 1, 2, 2},
};
int result = array_count_odds_evens(4, test_array);
printf("%d\n", result);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// QUESTION 3
////////////////////////////////////////////////////////////////////////////////
#include
struct student {
char first_initial;
char second_initial;
int wam;
};
int main(void) {
// BUG: Starter had `struct student;` only — no variable. Need an instance: `struct student student;`
struct student student;
printf("Enter first initial: ");
// BUG: Missing `&` on char scans; no leading space — newline after Enter is read as the initial.
scanf(" %c", &student.first_initial);
printf("Enter second initial: ");
scanf(" %c", &student.second_initial);
printf("Enter WAM: ");
// BUG: `&student->wam` — `student` is not a pointer; must be `&student.wam`.
scanf("%d", &student.wam);
// BUG: Starter used separate `if`s so several lines printed; wrong bounds (`<= 50` fail, `<= 65` pass);
// credit used `(wam <= 75 || wam >= 64)` (nearly always true); distinction used `<= 85` so 85 was wrong band.
// Fix: else-if chain — <50 fail; 50–64 pass; 65–74 credit; 75–84 distinction; else high distinction.
if (student.wam < 50) {
printf("%c.%c has a fail wam\n",
student.first_initial, student.second_initial);
} else if (student.wam <= 64) {
printf("%c.%c has a pass wam\n",
student.first_initial, student.second_initial);
} else if (student.wam <= 74) {
printf("%c.%c has a credit wam\n",
student.first_initial, student.second_initial);
} else if (student.wam <= 84) {
printf("%c.%c has a distinction wam\n",
student.first_initial, student.second_initial);
} else {
// BUG: Starter tied HD to a final `else` after a wrong `if (>= 75 && <= 85)` so 85+ and other cases were wrong.
printf("%c.%c has a high distinction wam\n",
student.first_initial,
student.second_initial);
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// QUESTION 4
////////////////////////////////////////////////////////////////////////////////
#include
#include
#define SIZE 50
int main(void) {
char str[SIZE];
printf("Enter a string: ");
fgets(str, SIZE, stdin); // BUG: missing SIZE
int i = 0; // BUG: i = 1
while (str[i] != '\0') { // BUG: == '\0'
// BUG: && instead of ||
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u'
) {
// BUG: i instead of 1
str[i] = str[i] + 1;
}
i++;
}
// BUG: Extra newline + %c
printf("Result: %s", str);
return 0;
}