Week 04 Weekly Test Sample Answers
Test Conditions
These questions must be completed under self-administered exam-like conditions. You must time the test yourself and ensure you comply with the conditions below.
- You may complete this test in CSE labs or elsewhere using your own machine.
- You may complete this test at any time before Week 6 Monday 17:00.
- Weekly tests are designed to act like a past paper - to give you an idea of how well you are progressing in the course, and what you need to work on. Many of the questions in weekly tests are from past COMP1511 final exams.
- You must treat the first hour as a real exam - no access to external help from other people or the internet.
- Once the first hour has finished, you should take note of how far you got, which parts you didn't understand, and ask questions to clarify your understanding with course staff or on the course forum.
- Failure to follow the above instructions will result in a lower quality learning experience and ultimately a lower mark in the final exam.
- Once the first hour has finished, you may use the internet, get help from other people, and still submit your solutions afterwards.
You may access this language documentation while attempting this test:
You may also access manual entries (the man
command).
Any violation of the test conditions will results in a mark of zero for the entire weekly test component.
weekly test question:
Print The Middle Integer of 3 Integers
Write a C program middle3.c
that reads 3 integers and prints the middle integer.
You are not permitted to use loops or arrays (use if statements).
Your program should behave exactly like this example:
./middle3 Enter integer: 23 Enter integer: 5 Enter integer: 27 Middle: 23 ./middle3 Enter integer: 3 Enter integer: 6 Enter integer: 27 Middle: 6 ./middle3 Enter integer: 9 Enter integer: 7 Enter integer: 8 Middle: 8You can assume the user supplies 3 integers. You do not have to check the return value from scanf.
When you think your program is working you can autotest
to run some simple automated tests:
1511 autotest middle3When you are finished working on this exercise you must submit your work by running give:
give cs1511 test04_middle3 middle3.c
middle3.c
// Modified 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// Print middle of 3 integers
#include <stdio.h>
int main(void) {
int a, b, c;
int tmp;
printf("Enter integer: ");
if (scanf("%d", &a) != 1) {
return 1; // EXIT_FAILURE would be more portable
}
printf("Enter integer: ");
if (scanf("%d", &b) != 1) {
return 1;
}
printf("Enter integer: ");
if (scanf("%d", &c) != 1) {
return 1;
}
// a, b, c can be in any order
// swap a & b if they are not in order
if (a > b) {
tmp = b;
b = a;
a = tmp;
}
// swap a & c if they are not in order
if (a > c) {
tmp = c;
c = a;
a = tmp;
}
// a must be the smallest now
// swap b & c if they are not in order
if (b > c) {
tmp = c;
c = b;
b = tmp;
}
// a, b, c now in order
printf("Middle: %d\n", b);
return 0;
}
weekly test question:
Loop Sum
Write a program called loop_sum.c
that reads an integer
n from standard input, and then scans in n integers from
standard input, adds them together, then prints the sum.
You can assume that n is non-negative (≥ 0).
You can assume that you are given exactly n+1 integers.
You can assume that scanf succeeds (you do not need to check for errors).
Make your program match the examples below exactly.
Note: you are not permitted to use an array in this exercise.
dcc loop_sum.c -o loop_sum ./loop_sum How many numbers: 2 1 2 The sum is: 3
How many numbers: 3 -3 4 13 The sum is: 14
How many numbers: 5 -2 -1 0 1 2 The sum is: 0
When you think your program is working you can autotest
to run some simple automated tests:
1511 autotest loop_sumWhen you are finished working on this exercise you must submit your work by running give:
give cs1511 test04_loop_sum loop_sum.c
loop_sum.c
// Read numbers in a loop, and calculate their sum.
// A sample solution.
#include <stdio.h>
#include <stdlib.h>
int read_num(void);
int main(void) {
printf("How many numbers: ");
int amount = read_num();
int sum = 0;
int i = 0;
while (i < amount) {
sum += read_num();
i++;
}
printf("The sum is: %d\n", sum);
return 0;
}
// Note: you did not need to check that scanf succeeds for this exercise.
int read_num(void) {
int num;
if (scanf("%d", &num) != 1) {
printf("Could not read a number\n");
exit(EXIT_FAILURE);
}
return num;
}
weekly test question:
Print A Hollow Triangle
Write a program called hollow_triangle.c
that reads an integer n from standard input.
and prints a pattern of asterisks forming a hollow triangle.
You can assume n is greater than 3.
Make your program match the examples below exactly.
Note: you are not permitted to use an array in this exercise.
dcc hollow_triangle.c -o hollow_triangle ./hollow_triangle Enter size: 4 * ** * * **** ./hollow_triangle Enter size: 5 * ** * * * * ***** ./hollow_triangle Enter size: 8 * ** * * * * * * * * * * ******** ./hollow_triangle Enter size: 11 * ** * * * * * * * * * * * * * * * * ***********
When you think your program is working you can autotest
to run some simple automated tests:
1511 autotest hollow_triangleWhen you are finished working on this exercise you must submit your work by running give:
give cs1511 test04_hollow_triangle hollow_triangle.c
hollow_triangle.c
// Written 14/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// as a test for COMP1511
// Print a hollow triangle
#include <stdio.h>
int main(void) {
int size;
printf("Enter size: ");
scanf("%d", &size);
int row = 1;
while (row <= size) {
int col = 1;
while (col <= row) {
// row == size gives horizontal line
// row == col gives diagonal line
// col == 1 gives vertical line
if (row == size || col == 1 || row == col) {
printf("*");
} else {
printf(" ");
}
col = col + 1;
}
printf("\n");
row = row + 1;
}
return 0;
}
Submission
You can run give multiple times. Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Week 6 Monday 17:00 to complete this test.
Automarking will be run by the lecturer several days after the submission deadline
for the test, using test cases that you haven't seen:
different to the test cases autotest
runs for
you.
(Hint: do your own testing as well as running
autotest
)
Test Marks
After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give's web interface or by running this command on a CSE machine:
1511 classrun -sturec
The test exercises for each week are worth in total 1 marks.
The best 7 of your 8 test marks for weeks 3-10 will be summed to give you a mark out of 7.