Completed: / exercises

Pointers in C

Important note for advanced users: malloc , calloc and free operations are not permmited in the code.

Shuffling the deck (2p)

.. well not exactly, but an array, and using the Fisher-Yates algorithm's pen and paper version.
Write a function that shuffles the given random size array containing random values (well, the variable sizes limit the range of random values). The function should return results in the same array. Use the prototype below.
void shuffle(uint8_t *list, uint16_t list_size);
In C, you can create random numbers with stdlib.h library function rand(). Note that you need also to initialize the (pseudo)random generator by giving a seed value in function srand, which you can get from the function time(NULL); Please only initialize the generator in your main function when testing, not in the shuffle function!
For example this could happen in your function.
..
// a random array (yeah right)
uint8_t list[8] = { 1,2,3,4,5,6,7,8};
..
// shuffling
shuffle(list,8);
..
// the array afterwards
list[8] = { 2,5,3,1,8,6,7,4};
Hint. You can use temporary array to save picked numbers or just move values by re-indexing the array.
Hint 2. Don't forget to include time.h-library
Hint 3. When testing your code, use a fixed array size. Try testing with different fixed sizes. Keep in mind that variable-length arrays (arrays whose size is set using a variable rather than a constant) may not be supported on all architectures or compilers.
Warning: You have not logged in. You cannot answer.

Moving Average (2p)

Without going too deep on digital signal processing (i.e. sensor data processing), moving average is a very handy method to reduce random noise on a measured signal (i.e. sensor data).
In this exercise, write a function that calculates and prints out the Simple Moving Average for the given float-type array and where the array size and window size are given as additional parameters. Use the following function prototype.
void movavg(float *array, uint8_t array_size, uint8_t window_size);
Note: In this case we want to just print the function in the screen. No need to add it again in the array.
We reccommend to use the following way to call the movavg-function for testing:
...
float data[5] = { 1.0, 2.0, 4.0, 6.0, 9.0 };

movavg(data, 5, 3);
Thus, the average would be calculated during the first iteration from the values 1.0, 2.0, 4.0, during the next iteration from the values 2.0, 4.0, 6.0, and during the last iteration from 4.0, 6.0, 9.0. The calculation of the average "slides" through the array.
The function would then print the floating-point numbers representing the moving average with two decimal places:
2.33,4.00,6.33
Note! The comma is used as a separator in the good old CSV style.
Warning: You have not logged in. You cannot answer.

Tic-tac-toe (3p)

Wite a function that checks the final output of a tic-tac-toe game.
Use function prototype int8_t tictactoe_check(int8_t * gameboard, int win_len); with pointer to the array consisting of the game board and the length of the row of x's or o's required to the win the game.
The game board size is 10 x 10, but is given as an array of bytes length of 100. All rows are written in sequence, so for example, index 42 in the array represents the third square of the fifth row of the grid. Each array element is a number with this 3 possible values: 1 stands for x, 2 for o and 0 for empty element.
The function needs to return the result of the game that can be: 1 if x wins, 2 if o wins and 0 if there is no winner or there are two winners (e.g. the board is presents impossible values).
Hint: Maybe you only need to process the squares that have game pieces.
Warning: You have not logged in. You cannot answer.
?