For the continuous evaluation component involving exercise-solving during the semester, the exercises you can submit in this class are:
Submission Deadline: October 11th (submit to AED's Mooshak)
You are encouraged to talk to the professor and to your colleagues if you have difficulties. However, any more direct help you have received from other colleagues or LLMs should be acknowledged in the comments of the code you submit.
After the deadline the problems will still be available on Mooshak, but the submissions will not count towards your grade.
Each class is worth 9 per cent of the grade for this component. As there will be 12 classes with submissions, you can get full marks even if you haven't done everything.
For a problem to count, you have to get all the tests right (i.e. accepted). Even if you solve all the problems, the maximum in a single class is 100 per cent.
It is guaranted that the main exercises on each class will sum to at least 100% of the class grade.
This class includes exercises about invariants. It is therefore convenient that you know what was discussed on the lectures:
1. My first Mooshak submissions
On this course we will use Mooshak to automatically evaluate your C++ code (see Mooshak's help).
To access the system you can use your login (with the word "up")
and password that was sent to your institutional email (message sent on the 21st of September with the title "[AED] Mooshak password - login")
(if you have trouble accessing it, send a message on discord to @Pedro Ribeiro | your practical class professors also know your passwords)
In this problem the goal is to make your first submissions in Mooshak, all for the [AED001] Forty-Two. The problem statements will also be available inside Mooshak. Always start by carefully reading the problem statement given!
2. My first invariant analysis
We will now work towards showing why the previously given program works (aed001.cpp - html version).
"When we are testing the condition \(i \lt n\) in the for loop for the \(k\)-th time, for \(k \ge 1\), the value of \(i\) is \(\ldots\), the value of \(count\) is \(\ldots\) and \(n\) has its initial value.
- \(i\) takes integer values, being 0 initially and, from the problem statement, \(n\) contains a positive integer;
- Assuming the invariant is true, we can claim that \(n\) does not change along the loop, that the value of \(i\) increases by one at each iteration, and that, when we test the loop condition \(i < n\) for \(k=n+1\), we have \(i=n\);
- Therefore, \(i < n\) is false when \(k=n+1\). Does that mean the loop terminates?
- Assuming the invariant is true, which is the value of \(count\)
when \(k=n+1\)?
Remark that, for the answer, you must write down just what the invariant says if we replace \(k\) by \(n+1\).
- So far, we have not proved that the invariant is true.
- Intialization ("base case") We must justify that the property is true when \(k=1\). For that, check what was executed in the program before the first test.
- Maintenance ("induction step" or "inheritance") Assume the property is true at \(k\)-th test (as "induction hypothesis") and that \(i < n\
\) is true. Then, argue that under that assumption, if we execute the body of the loop, the property will still be true for \(k+1\). Remark that, we must check what happens if a == 42 is true at that iteration, and if it is not.
3. Removing numbers
Consider the following C++ function which removes all values equal to \(x\) from the array \(a\) with \(n\) integers. The final result will be in \(a[0],\ldots,a[used-1]\) and the function returns \(used\). In the end, the values in the positions \(used\) to \(n-1\) of \(a\) are not relevant (can be anything).
int remove(int a[], int n, int x) { int used = 0; int pos = 0; while (pos < n) { if (a[pos] != x) { a[used] = a[pos]; used++; } pos++; } return used; }
"When we are testing the condition \(pos \lt n\) in the while loop for the \(k\)-th time, for \(k \ge 1\), the values of \(n\) and \(x\) are the same they had before the loop, the value of \(pos\) is \(\ldots\), the value of \(used\) is \(\ldots\) and the contents of the array \(a[]\) are \(\ldots\)"
- Follow the program step by step, writing down the content of each variable when it is checking the loop condition \(pos < n\) for \(k=1\), for \(k=2\), for \(k=3\) and so forth.
- Identify a pattern and write it formally, i.e., write the mathematical expressions that define the state (e.g., is it true that \(pos = k-1\)?)
- \(pos\) takes integer values and contains 0 initially and, from the problem statement, \(n\) contains a positive integer;
- Assuming the invariant is true, we can claim that \(n\) does not change along the loop, the value of \(pos\) increases by one at each iteration, and that, when we test the loop condition \(pos < n\) for \(k=n+1\), we have \(pos = n\), since \(pos=k-1\);
- Therefore, \(pos < n\) is false when \(k=n+1\). Does that mean the loop terminates?
- Assuming the invariant is true, which is the value of \(used\) and the content of the vector when \(k=n+1\)?
Remark that, for the answer, you must write down just what the invariant says if we replace \(k\) by \(n+1\). Hopefully, that will be exactly what it is required by the problem specification.
- So far, we have not proved that the invariant is true.
- Intialization ("base case") We must justify that the property is true when \(k=1\). For that, check what was executed in the program before the first test.
- Maintenance ("induction step" or "inheritance") Assume the property is true at \(k\)-th test (as "induction hypothesis") and that \(pos < n\) is true. Then, argue that under that assumption, if we execute the body of the loop, the property will still be true for \(k+1\). Remark that, we must check what happens if a[pos] != x is true at that iteration, and if it is not.
- In the worst case, the instructions a[used] = a[pos]; and used++; are executed in every iteration. In the best case, they are never executed. You must say what characterizes a best case instance and a worst case instance, i.e., the property that the elements in the sequence satisfy in each case
Problem: [AED002] Unlucky Numbers.
4. Odd numbers
For this exercise you will be solving the problem [AED003] Space Oddity.
Note: did you know that Space Oddity is a David Bowie song and that astronaut Chris Hadfield filmed a (revised) cover of this song while he was on the International Space Station? 🚀
5. Number Factorization
AED Mooshak classes will usually include at least one extra exercise that you can use of you want to keep testing your knowledge. For this class we propose a problem in which efficiency matters.
For this exercise you will be solving the problem [AED004] Bart's Math Problem.
Here are some helpful notes:
In order to be efficient, your function must use the properties of prime numbers stated above. An inefficient solution might get Time Limit Exceeded veredict at Mooshak (the time limit for this problem is 1 second).
AED Mooshak classes will usually include one challenge exercise, which can be much harder the previous ones and might even need knowledge that was not given explicitly on the course.
The main idea is to give you the opportunity of improving your problem solving abilities.
For this exercise you will be solving the problem [AED005] Number Spiral.
Because this is the first challenge, the selected problem is not too hard and can be solved by any student without any knowledge of advanced algorithms or data structures. You just need to think a little bit and of course be efficient and go beyond the naive "brute force" approach 😉
Since this is a challenge, I will not give you any hints, but if you are stuck just contact @Pedro Ribeiro on Discord. Feel free to also contact @Pedro Ribeiro to discuss your accepted solution.
Happy coding! 😊