2020/7/19 COMP1521 20T2 ¡ª Week 06 Weekly Test Questions
Week 06 Weekly Test Questions 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 Tuesday 21 July 21: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 final exams.
Once the first hour has finished, you must submit all questions you’ve worked on.
You should then take note of how far you got, which parts you didn’t understand.
You may choose then to keep working and submit test question anytime up to Tuesday 21 July 21:00
However the maximum mark for any question you submit after the first hour will be 50%
You may access this language documentation while attempting this test: C quick reference
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.
Set up for the test by creating a new directory called test06, changing to this directory, and fetching the provided code by running these commands:
Or, if you’re not working on CSE, you can download the provided code as a zip file or a tar file.
$ mkdir test06
$ cd test06
$ 1521 fetch test06
:
MIPS Are Not Negative
You have been given not_negative.s, a MIPS assembler program that reads a numbers and then prints it.
Add code to not_negative.s to make it equivalent to this C program:
$ 1521 spim -f not_negative.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a number: 42
You entered: 42
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/06/questions 1/5
2020/7/19 COMP1521 20T2 ¡ª Week 06 Weekly Test Questions
// read numbers until a non-negative number entered #include
int main(void) {
int x;
while (1) {
printf(“Enter a number: “);
scanf(“%d”, &x);
if (x < 0) {
printf("Enter a positive number\n");
} else {
printf("You entered: %d\n", x); break;
} }
return 0; }
In other words it should read numbers until a non-negative number is entered, For example:
$ 1521 spim -f not_negative.s Enter a number: -5
Enter a positive number Enter a number: -1
Enter a positive number
Enter a number: 24
You entered: 24
$ 1521 spim -f not_negative.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a number: -1
Enter a positive number Enter a number: -2 Enter a positive number Enter a number: -3 Enter a positive number Enter a number: -4 Enter a positive number Enter a number: -5 Enter a positive number Enter a number: 0
You entered: 0
$ 1521 spim -f not_negative.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a number: 100
You entered: 100
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
You have been given reverse_negative.s, a MIPS assembler program that reads numbers into an array. Add code to reverse_negative.s to make it equivalent to this C program:
$ 1521 autotest not_negative
$ give cs1521 test06_not_negative not_negative.s
:
Reversed MIPS
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/06/questions 2/5
2020/7/19 COMP1521 20T2 ¡ª Week 06 Weekly Test Questions
// Read numbers into an array until a negative number is entered
// then print the numbers in reverse order
#include
int numbers[1000];
int main(void) {
int i = 0;
while (i < 1000) { int x;
scanf("%d", &x); if (x < 0) {
break; } else {
numbers[i] = x;
}
i++; }
while (i > 0) { i–;
printf(“%d\n”, numbers[i]); }
}
In other words make it stop when a negative number is read and then print the numbers readin reverse order. For example:
$ 1521 spim -f reverse_negative.s
Loaded: /home/cs1521/share/spim/exceptions.s 1
2
3
4
-1
4
3
NOTE:
You can assume that a negative number will always be entered and at most 999 numbers are entered before a negative number is entered.
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
$ 1521 autotest reverse_negative
$ give cs1521 test06_reverse_negative reverse_negative.s
2
1
$ 1521 spim -f reverse_negative.s
Your time for this test has finished. You must submit your work now. You should reflect
15
4 on how you went in this hour, and discuss with your tutor if you have concerns. You
1 42 -3 42 1
Loaded: /home/cs1521/share/spim/exceptions.s
Test Complete!
may choose to keep working, but the maximum mark for any questions you submit later will be 50%.
4 15
:
Different MIPS
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/06/questions 3/5
2020/7/19
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/06/questions 4/5
COMP1521 20T2 ¡ª Week 06 Weekly Test Questions
You have been given different10.s, a MIPS assembler program that reads 10 numbers into an array. Add code to different10.s to make it equivalent to this C program:
#include
int numbers[10];
int main(void) {
int x, i, n_seen;
n_seen = 0;
while (n_seen < 10) {
printf("Enter number: ");
scanf("%d", &x);
i = 0;
while (i < n_seen) {
if (x == numbers[i]) { break;
}
i++; }
if (i == n_seen) { numbers[n_seen] = x; n_seen++;
} }
printf("10th different number was %d\n", x); return 0;
}
In other words make it stop when 10 different numbers have been read and print a message including For example:
$ 1521 spim -f different10.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a number: 11
Enter a number: 12
Enter a number: 13
Enter a number: 14
Enter a number: 15
Enter a number: 16
Enter a number: 17
Enter a number: 18
Enter a number: 19
Enter a number: 20
10th different number was: 20
$ 1521 spim -f different10.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a number: 11
Enter a number: 11
Enter a number: 12
Enter a number: 11
Enter a number: 13
Enter a number: 14
Enter a number: 13
Enter a number: 14
Enter a number: 15
Enter a number: 16
Enter a number: 29
Enter a number: 19
Enter a number: 18
Enter a number: 55
10th different number was: 55
NOTE:
You can assume that a 10 different numbers will always be entered.
2020/7/19 COMP1521 20T2 ¡ª Week 06 Weekly Test Questions
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
When you are finished each exercise make sure you submit your work by running give.
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 Tuesday 21 July 21: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:
The test exercises for each week are worth in total 1 marks.
The best 6 of your 8 test marks for weeks 3-10 will be summed to give you a mark out of 9.
COMP1521 20T2: Computer Systems Fundamentals is brought to you by the School of Computer Science and Engineering
at the University of New South Wales, Sydney.
For all enquiries, please email the class account at cs1521@cse.unsw.edu.au CRICOS Provider 00098G
$ 1521 autotest different10
$ give cs1521 test06_different10 different10.s
Submission
$ 1521 classrun -sturec
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/06/questions
5/5