Hacker Challenge: Spiral Grid Animation
Learn to manipulate a 2-D array to create a rotating spiral and a grid that shifts the values k times.
The previous lesson covered the generation of spiral grid patterns. This lesson builds on those concepts to produce a rotating spiral grid animation. So without any further ado, let's dive right in.
Spiral grid animation
The following illustration depicts the first iteration of a rotating spiral grid.
As you may have noticed, each rotating spiral is contained within its own square/rectangle.
Playground for your implementation
Use the playground below to write your implementation for spiral grid animation.
#include <iostream>
#include <iomanip>
#include <unistd.h>
using namespace std;
#define MaxRows 100
#define MaxCols 100
#define Capacity 10000
void CWRotatingRectangles(int World[MaxRows][MaxCols], int Rows, int Cols) {
    // Write your code here
}
void PrintR2_World(const char Msg[], int World[MaxRows][MaxCols], int Rows, int Cols) {
    system("clear");
    cout << Msg;
    for(int ri=0; ri<Rows; ri++)
    {
        for(int ci=0; ci<Cols; ci++ )
           cout << setw(4)<<World[ri][ci] ;
        cout << endl;
    }
}
int main()  // Animations...
{
    // Write your code here
    
    return 0;
}
Implementation walkthrough
From here onward, we'll provide you with a guide on implementing the solution for the spiral grid animation that you can follow in the playground above. The guide contains both an explanation and the implementation of the functions. You should look at the solution if you are unsure of the answer. Best of luck!
The clockwise rotating rectangles
The CWRotatingRectangles  function will be responsible for rotating all rectangles once. It takes three arguments, as shown in the function prototype below: World that represents the matrix to be rotated, Rows that represents the number of rows of World matrix, and Cols that represents the number of columns of World matrix.
void CWRotatingRectangles(int World[MaxRows][MaxCols], int Rows, int Cols);
The main logic of the function is as follows:
First, we need to determine the number of squares/rectangles in the grid, just like we did in the previous lesson.
Then for all squares/rectangles, we do the following:
Store the starting value of the spiral in a variable (i.e.,
prev).Loop in all four directions (top-left to top-right, top-right to top-bottom, bottom-right to bottom-left, and bottom-left to top-left), where in each iteration, we swap
prevwith the current element.After all, loops are complete; we assign the value of
prevto the starting value of the spiral.
If you correctly implement this function and execute it once, you'll get a matrix with all squares/rectangles shifted by 1 in the clockwise direction.
If you weren't able to implement the function, feel free to look at the implementation below:
The main function
As you may be wondering, the function we implemented earlier only rotated the spiral once, so how do we rotate them repeatedly with delays to generate an animation? We'll leverage the main function for this.
First, we'll declare and initialize our matrix and its dimensions.
Then, we'll run an infinite loop in which we do the following actions:
We invoke the
CWRotatingRectanglesfunction to rotate the spirals once.Then we print the rotated matrix onto the console.
We clear the console by passing
flushtocout(flush immediately prints everything in the buffer to the output console, otherwise, it might cause a delay, and the simulation might not display correctly).Lastly, we add a time delay until the next iteration of the loop by using the
sleepmethod.
Complete implementation of spiral grid animation
#include <iostream>
#include <iomanip>
#include <unistd.h>
using namespace std;
#define MaxRows 100
#define MaxCols 100
#define Capacity 10000
void CWRotatingRectangles(int World[MaxRows][MaxCols], int Rows, int Cols);
void PrintR2_World(const char Msg[], int World[MaxRows][MaxCols], int Rows, int Cols);
int main()  // Animations...
{
    int World[MaxRows][MaxCols]={{ 1, 2, 3, 4, 5, 6, 7},
                                 {24, 1, 2, 3, 4, 5, 8},
                                 {23,16, 1, 2, 3, 6, 9},
                                 {22,15, 8, 1, 4, 7,10},
                                 {21,14, 7, 6, 5, 8,11},
                                 {20,13,12,11,10, 9,12},
                                 {19,18,17,16,15,14,13}}; 
    int Rows=7, Cols=7;
    PrintR2_World("R^2 World:\n\n", World, Rows, Cols);
    
    while(true)
    {
        CWRotatingRectangles(World, Rows, Cols);
        system("clear"); // system is function which clears the screen
        PrintR2_World("R^2 World:\n\n", World, Rows, Cols);
        cout << flush;   // flush outputs the buffer immediately
        sleep(1);        // wait 1 second
    }    
    
    return 0;
}
Exercise: Shifting values k times
Let's write a program that shifts the values k times sequentially row by row instead of shifting the values in a spiral fashion.
Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.
So in one shift operation:
Element at
grid[i][j]moves togrid[i][j + 1]Element at
grid[i][n - 1]moves togrid[i + 1][0]wherenis the number of columnsElement at
grid[m - 1][n - 1]moves togrid[0][0]wheremis the number of rows
Return the 2D grid after applying shift operation k times. Look at the animation below:
Write the implementation for RotateKTimes() in the code editor below.
#include <iostream>
#include <iomanip>
#include <unistd.h>
using namespace std;
#define MaxRows 100
#define MaxCols 100
#define Capacity 10000
void RotateKTimes(int grid[][MaxCols], int Rows, int Cols, int K)
{
  // Write code here.
}
void PrintRotating_World(int World[MaxRows][MaxCols], int Rows, int Cols)
{
    system("clear");
    for(int ri=0; ri<Rows; ri++)
    {
        for(int ci=0; ci<Cols; ci++ )
           cout << setw(4)<<World[ri][ci] ;
        cout << endl;
    }
}
int main()
{
    int World[MaxRows][MaxCols]={{1,2,3,4,5},
                                    {16,1,2,3,6},
                                    {15,8,1,4,7},
                                    {14,7,6,5,8},
                                    {13,12,11,10,9}}; 
        int Rows=5, Cols=5;
        
        
        while(true)
        {
            RotateKTimes(World, Rows, Cols, 2);
            PrintRotating_World(World, Rows, Cols);
            cout << flush;
            sleep(1);
        }
}