////////////////////////////////////////////////////////////////////////////////
// QUESTION 1
////////////////////////////////////////////////////////////////////////////////
#include
#include
#define SIZE 1000
int unique_sum(int size, int array[SIZE]);
// This is a simple main function which could be used
// to test your unique_sum function.
// It will not be marked.
// Only your unique_sum function will be marked.
int main(void) {
int test_array[SIZE] = {1, 1, 5, 10, 7, 5, 3, 1, 1};
int result = unique_sum(9, test_array);
printf("%d\n", result);
return 0;
}
// unique_sum should return the sum of
// the unique values of the array. Numbers should be entered into
// the array until Ctrl+D is pressed
int unique_sum(int size, int array[SIZE]) {
int is_unique = 1;
int sum = 0;
for (int i = 0; i < size; i++) {
is_unique = 1;
for (int j = 0; j < i; j++) {
if (array[i] == array[j]) {
is_unique = 0;
}
}
if (is_unique) {
for (int k = i + 1; k < size; k++) {
if (array[i] == array[k]) {
is_unique = 0;
}
}
}
if (is_unique) {
sum = sum + array[i];
}
}
return sum;
}
////////////////////////////////////////////////////////////////////////////////
// QUESTION 2
////////////////////////////////////////////////////////////////////////////////
#include
#include
#define MAX_SIZE 100
int magic_square(int size, int array[MAX_SIZE][MAX_SIZE]);
// This is a simple main function which could be used
// to test your magic_square function.
// It will not be marked.
// Only your magic_square function will be marked.
int main(void) {
int test_array[MAX_SIZE][MAX_SIZE] = {
{11, 24, 7, 20, 3},
{ 4, 12, 25, 8, 16},
{17, 5, 13, 21, 9},
{10, 18, 1, 14, 22},
{23, 6, 19, 2, 15}
};
int size = 5;
if (magic_square(size, test_array) != 0) {
printf("You have a magic square.\n");
} else {
printf("You do not have a magic square.\n");
}
return 0;
}
// Is a magic square if sum of rows, columns and diagonals all equal
int magic_square(int size, int array[MAX_SIZE][MAX_SIZE]) {
int expected_sum = 0;
for (int j = 0; j < size; j++) {
expected_sum += array[0][j];
}
// Check rows
for (int i = 0; i < size; i++) {
int row_sum = 0;
for (int j = 0; j < size; j++) {
row_sum += array[i][j];
}
if (row_sum != expected_sum) {
return 0;
}
}
// Check columns
for (int j = 0; j < size; j++) {
int col_sum = 0;
for (int i = 0; i < size; i++) {
col_sum += array[i][j];
}
if (col_sum != expected_sum) {
return 0;
}
}
// Check main diagonal
int main_diag_sum = 0;
for (int i = 0; i < size; i++) {
main_diag_sum += array[i][i];
}
if (main_diag_sum != expected_sum) {
return 0;
}
// Check secondary diagonal
int sec_diag_sum = 0;
for (int i = 0; i < size; i++) {
sec_diag_sum += array[i][size - 1 - i];
}
if (sec_diag_sum != expected_sum) {
return 0;
}
return expected_sum; // magic!
}
////////////////////////////////////////////////////////////////////////////////
// QUESTION 3
////////////////////////////////////////////////////////////////////////////////
#include
#include
#define SIZE 3
#define MAX_GROCERY_NAME 20
struct grocery_item {
char item_name[MAX_GROCERY_NAME];
double cost;
};
int main(void) {
printf("Enter a budget: ");
double budget;
scanf("%lf", &budget);
getchar();
printf("Enter 3 grocery items and their costs:\n");
struct grocery_item array[SIZE];
for (int i = 0; i < SIZE; i++) {
fgets(array[i].item_name, MAX_GROCERY_NAME, stdin);
scanf("%lf", &array[i].cost);
getchar();
}
printf("Checking grocery items...\n");
////////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE ANY OF THE CODE ABOVE HERE //
// THE CODE ABOVE IS SIMPLY TO ASSIST IN SCANNING IN THE GROCERY ITEMS AND//
// THEIR COSTS INTO THE ARRAY. THERE ARE NO BUGS ABOVE //
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////// ONLY EDIT CODE BELOW HERE ///////////////////////////
////////////////////////////////////////////////////////////////////////////
int j = 0;
int over_budget = 0;
while (j < SIZE) {
// BUG: < instead of >
if (array[j].cost > budget) {
over_budget++;
}
// Omitted increment
j++;
}
// BUG: == budget instead of == SIZE
if (over_budget == SIZE) {
printf("Oh no, all your items are over budget!\n");
} else if (over_budget == 0) {
printf("Great work! All your items are within budget!\n");
} else {
// BUG: Start at 1 not 0
int k = 0;
printf("Over budget items:\n");
while (k < SIZE) {
if (array[k].cost > budget) {
// BUG: .cost not .item_name
printf("%s", array[k].item_name);
}
k++;
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// QUESTION 4
////////////////////////////////////////////////////////////////////////////////
#include
#include
#define SIZE 50
void delete_char_from_string(char input[SIZE], int position);
void strip_newline(char *string);
int main(void) {
printf("Enter a string: "); // BUG: missing semicolon
char input[SIZE];
fgets(input, SIZE, stdin); // BUG: missing 'stdin' from arguments
strip_newline(input);
printf("Enter a position to remove: ");
int position; // BUG: missing declaration
scanf("%d", &position);
delete_char_from_string(input, position); // BUG: called with 'input[]', extra '0' arg
printf("Result: %s\n", input);
return 0;
}
void delete_char_from_string(char string[SIZE], int position) {
int i = position - 1; // LOGIC BUG: missing "- 1" from initialisation
while (i < strlen(string)) {
string[i] = string[i + 1];
i++; // LOGIC BUG: missing increment
}
}
////////////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE ANY OF THE CODE BELOW HERE //
// The code below is correct as given. You do NOT need to modify it //
////////////////////////////////////////////////////////////////////////////////
// Removes a newline character from end of the given string, if it exists.
void strip_newline(char *string) {
int length = strlen(string);
if (length != 0 && string[length - 1] == '\n') {
string[length - 1] = '\0';
}
}