In what concerns the "exercises during classes" component of the evaluation, the exercises that you can submit for this class are:
Deadline for submission: March 31st (submit to PII's Mooshak)
You are encouraged to talk to the professors and your colleagues if you encounter difficulties.
However, any more direct help that you receive 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 practical class is worth 18% of your final component for exercices during class. Since there will be 6 classes with submissions, you can achieve maximum grade even without doing fully doing all classes.
For a problem to count you must pass all tests (that is, to have an accepted). Even if you solve all problems, the maximum on one class is 100%.
To obtain 100% it will always be enough to solve the main exercises.
With the exercises in this class you will develop the following skills:
If you feel stuck, go back and revise the lecture 01: C Fundamentals, 02: Expressions, 03: Conditional Execution, 04: Cycles and mostly 05: Functions, 06: Arrays and 07: About Algorithms
This week's theme for the exercises is Stranger Things.
1) My first function
On this class you will be using functions. Here is a very simple example to remind you of them:
#include <stdio.h> // Simple function to return the sum of two integers int sum(int a, int b) { return a+b; } // Simple function to return the maximum of two integers int max(int a, int b) { if (a>b) return a; else return b; } int main(void) { // You can call functions passing variable arguments int a = 2; int b = 3; int c = sum(a,b); printf("sum(%d, %d) = %d\n", a, b, c); // You can also call them directly printf("sum(4, 5) = %d\n", sum(4,5)); printf("max(42, 1) = %d\n", sum(42,1)); printf("max(6, 10) = %d\n", sum(6,10)); return 0; }
Here is its output:
sum(2, 3) = 5 sum(4, 5) = 9 max(42, 1) = 43 max(6, 10) = 16
2) My first Mooshak function exercise
Armed with what you learned from the previous exercise, you are now ready to tackle the first problem from this class.
Read the statement, code and try to submit Accepted solutions to the following problems. Don't forget to test first on your computer!
- Make a cycle between a and b like in the example code
- Use an if statement inside the cycle to check if the number is divisible by n
Note that for the exercises of the class you need to submit a .c
file with only the function definition (and no main function).
For instance, the following code could be the full content of a pii025.c
file to submit on Mooshak (it would give Wrong Answer as result because obviously it does not produce the correct answer).
int triangle(int a, int b, int c) { return 0; }
If you want to test on your computer you can compile with the test code that is given on the problem statement:
pii025_test.c
and the function definition is on pii025.c
- you could compile by a command line such as:3) My first array code
Test the code examples from the lectures on arrays. Make sure to compile, run and understand what's happening:
arrays.c
- simple usage of an array
array_functions.c
- using an array as argument of a function
array_modify.c
- changing the array inside the function
4) Array problems
Can you now solve four problems with arrays?
Read the statement, code and try to submit Accepted solutions to the following problems. Don't forget to test first on your computer!
- A simple cycle on the array elements while keeping the current sum on a variable and then return that sum
- A cycle through the elements and keeping the maximum difference
- Be careful to always consideer absolute differente (e.g. use ifs or make a separate function to return absolute difference)
- Swap first element with last, second with second to last, etc or
- Create auxiliary array, copy everything to it and the do a cycle from the last element to the first and put the elements in place on the original array- Can you think on any algorithm to sort? (it does not need to be efficient)
For example, how would you sort cards on your hand?
Here are three simple examples that use simple cycles: Bubble Sort | Insertion Sort | Selection Sort
Can you implement all three algorithms and test them?
Loved the main exercises and craving more challenges? These extra problems are here to help you sharpen your skills and reward those that are coding for the love of learning 😍
Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!
- Can you do one without any help?
- Binary search is your friend :)
This challenge is of algorithmic nature, so you need and efficient solution and not just a correct (but more "brute force") approach if you don't want to face a Time Limit Exceeded. Can you see some patterns on the spiral and how you can take advantage of them?
This exercise is more challenging and not so trivial or immediate, so give yourself some time to think about it before looking or asking for help.
Happy coding! 😊