In what concerns the "exercises during classes" component of the evaluation, the exercises that you can submit for this class are:
Deadline for submission: April 7th (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 all the lectures, with special emphasis on 06: Arrays, 08: Basic Types and 09: Strings, .
This week's theme for the exercises is Game of Thrones.
1) Getting started with strings and characters
Save the following code to a file, compile, execute it and make sure you understand everything that it is doing:
#include <stdio.h> // Function to compute the string length // (iterate over all position until terminator is reached) int length(char str[]) { int len = 0; while (str[len] != '\0') len++; return len; } int main(void) { // Create 3 strings char s1[2] = "c"; // we can use exact size (+1 for '\0') char s2[100] = "code"; // we can use larger size than length char s3[] = "programming"; // we can ommit size // Call the length function for each of the 3 strings printf("length of \"%s\" is %d\n", s1, length(s1)); printf("length of \"%s\" is %d\n", s2, length(s2)); printf("length of \"%s\" is %d\n", s3, length(s3)); return 0; }
Here is its output:
length of "c" is 1 length of "code" is 4 length of "programming" is 11
Save this code to a file, compile, execute it and make sure you understand everything that it is doing:
#include <stdio.h> #include <ctype.h> // A library with utilities for characters int main(void) { char c = 'a'; // Test several isX functions // . if true, return number different than 0 // . if false, return 0 printf("islower(%c) = %d\n", c, islower(c)); printf("isupper(%c) = %d\n", c, isupper(c)); printf("isalpha(%c) = %d\n", c, isalpha(c)); printf("isspace(%c) = %d\n", c, isspace(c)); printf("isdigit(%c) = %d\n", c, isdigit(c)); // Test several toX functions printf("tolower(%c) = %c\n", c, tolower(c)); printf("toupper(%c) = %c\n", c, toupper(c)); return 0; }
Here is its output:
islower(a) = 512 isupper(a) = 0 isalpha(a) = 1024 isspace(a) = 0 isdigit(a) = 0 tolower(a) = a toupper(a) = A c = a; code of c is 97 c = a; c+1 = b c = a; c+2 = c
2) My first string functions
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 to traverse the string as in the length
function example
- For each position use the isdigit
to check if the current char is a digit and in that case increment the counter
((instead of using isdigit you could also check if the character is >='0' && <='9'))
- In the end, return the result accumulated in the counter
- Make a cycle to traverse the string and change each char to its transformed/ciphered version if it is a lowercase letter
- Remember each char can be used as an integer
- To shift it k positions you only need to add k to it (and take care to "wrap around" if it results in something bigger than 'z')
- Can you do one without help? 😊 Think on how you can use a cycle...
- Although not really necessary, you can opt to use an extra temporary string to produce the "new string" and the copy it back to the original char array
- Don't forget to add the terminator ('\0') at the end of the string
- Make a cycle to traverse the string
- Keep a variable showing how many chars you already matched on the key
- If the current string cha matches the first non-matched char on the key, increase the number of matched chars
- If all characters are matched, you can return 1 (or 0 otherwise)
3) Reading and storing several strings
For the next problem you will make again a full program (and not just a function). It will be helpful to store several strings.
Here is some example code that might help you get started for the next problem. Test the code. Make sure to compile, run and understand what's happening:
#include <stdio.h> #define N_STRINGS 3 #define MAX_SIZE 100 int main(void) { // Array of strings (array of array of chars) char str[N_STRINGS][MAX_SIZE]; // Read N_STRINGS for (int i=0; i<N_STRINGS; i++) scanf("%s", str[i]); // Print N_STRINGS for (int i=0; i<N_STRINGS; i++) printf("str[%d] = \"%s\"\n", i, str[i]); return 0; }
For instance, if we give the code the following input:
um dois tres
Then the output would be:
str[0] = "um" str[1] = "dois" str[2] = "tres"
You are now ready to solve the next problem:
Read the statement, code and try to submit an Accepted solution to the following problem. Don't forget to test first on your computer!
- Read and store the strings as suggested on the example code
- For each word, check if it appears later on the array (with another cycle)
- If it appears, do not count (it will be counted later); if it does not appear... count it!
Do you want to continue your adventure on the world of Westeros? Here are more exercises 🙂
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!
- Our suggestion is to create a function to check if a key appears exactly at position i on the string (simpler problem than original)
- Solving the problem is now traversing all positions and calling the previously defined function to increment a counter if there is a match on that position
- Our suggestion is to create a function to transform the current string into the next generation
- You can use an auxiliary temporary string to build the next string
- The problem can now be solved by a simple cycle that calls the previous function n times and keeps printing the current string
Congratulations, brave solver of the Game of Strings! 🎉
You have demonstrated the skill and the mind of a true Maester, solving each riddle from the cold North to the burning South of Westeros. But beware, for one final challenge remains. 🐉🔥
Daenerys Targaryen, the Mother of Dragons, is not easily satisfied. There is one last puzzle you must conquer, one final problem to prove your worth. Succeed, and you will earn the respect of the Seven Kingdoms. Fail, and you may find yourself facing the fiery wrath of the dragons...
This challenge is of algorithmic nature, so you need and efficient solution (both on memory and space) and not just a correct (but more "brute force"). Note that this is until now the (algorithmically) most difficult exercise that was proposed on this course 😈.
Give yourself some time to think about it an be proud if you make it! Be sure to chat with your professors for hints or to proudly talk about how you solve it 😁
Happy coding! 😊