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 24th (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 and mostly 04: Cycles
1) My first cycle program
#include <stdio.h> int main(void) { int a, b; printf("Insert two numbers: "); scanf("%d %d", &a, &b); // Example of a for instruction that shows all numbers between a and b // (because the body is only one line, curly braces are optional) // (also note how the variable can be declared inside the for initialization) printf("for:"); for (int i=a; i<=b; i++) printf(" %d", i); printf("\n"); // Example of a while instruction that shows all numbers between a and b printf("while:"); int i = a; while (i<=b) { printf(" %d", i); i++; } printf("\n"); // Example of a do while instruction that shows all numbers between a and b printf("do...while:"); i = a; do { printf(" %d", i); i++; } while (i<=b); printf("\n"); return 0; }
Here is an example execution when we input the numbers 2 and 6:
Insert two numbers: 2 6 for: 2 3 4 5 6 while: 2 3 4 5 6 do...while: 2 3 4 5 6
2) My first Mooshak cycle 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
3) Cycles inside cycles
😁 cycles inside cycles...
#include <stdio.h> int main(void) { for (int i=0; i<3; i++) for (int j=0; j<5; j++) printf("%d %d\n", i, j); return 0; }
Read the statement, code and try to submit Accepted solutions to the following problems. Don't forget to test first on your computer!
- Make one cycle to iterate through all test cases and inside it put the code for each test case
- Each test case can be solved by a scanf to read the n value followed by another cycle to iterate until the number becomes 1
4) Iterating through the digits of a number
#include <stdio.h> int main(void) { int n = 5672; // print all digits of n while (n>0) { printf("%d\n", n%10); n /= 10; } return 0; }
Read the statement, code and try to submit Accepted solutions to the following problems. Don't forget to test first on your computer!
- Make one cycle to iterate through all test cases and inside it put the code for each test case
- Each test case can be solved by a scanf to read the n value followed by another cycle to iterate through the digits of n
- To sum, use an auxiliary variable that can serve as an accumulator: first it should be initialized to zero and then you increment the value with the current digit
5) More cycle problems!
Can you now solve two problems withouth having any base code?
Read the statement, code and try to submit Accepted solutions to the following problems. Don't forget to test first on your computer!
- Make one cycle to iterate through all test cases and inside it put the code for each test case
- After reading n, each test case can be solved by two other cycles: one for iterating all lines and inside each line one for iterating trough the columns
- Knowing you are on line i and column j, can you create an if clause to decide if it should be a '#'
or a '.'
?
- Make one cycle to iterate through all test cases and inside it put the code for each test case
- After reading n, each test case can be solved by two other cycle that tries all possible divisors
- Do you need more help or can you do the rest by yourself? 😁
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!
- While traversing the digits, if you keep the "last" digit, you can compare it to the current one, right? 😉
- Be careful with the order you are iterating
- Think first of an "easier" problem: can you calculate the reverse of a number? While iterating through the digits, how can you incrementally build the reverse number?
- After having the original and the reversed number, the problem becomes trivial!
You almost conquered the cycles set of problems, but there is still one last hurdle. Can you do the ultimate challenge of this class?
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! 😊