Skip to content

Instantly share code, notes, and snippets.

@SibTiger
Created August 29, 2018 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SibTiger/ead62feeef7b9b9bd7067bbd07f5d93a to your computer and use it in GitHub Desktop.
Save SibTiger/ead62feeef7b9b9bd7067bbd07f5d93a to your computer and use it in GitHub Desktop.
1D Array Rotation Problem
// =====================================================
// Programmer: Nicholas Gautier
// Class: CS1514; Computer Science II [Tutoring Copy]
// Assignment #: N/A
// Due Date: N/A
// Instructor: Dr. Jawad Drissi
// Description: This program is designed to simply rotate
// the contents within the 1D-array given by a
// specific key.
// For example:
// Original Array: {1, 2, 3, 4, 5}
// Key of '4': {3, 4, 5, 1, 2}
// ^_This is our 'Key'
//
// NOTE: I am using Notepad++ and Vi with g++ via Linux Subkernel (NT10)
// ----
// COMPILING NOTES FOR G++
// - NOTHING YET -
// ----
// Return Codes:
// 0 = Successful Operation
// !0 = See Operating System Documentation
// =====================================================
#include <iostream> // We must use this to capture data from the
// user and to display messages on the terminal
// buffer (or essentially, the user's screen.)
// Macro Definitions
// ================================
#define _MAX_ARRAY_SIZE_ 90 // Size of the array
#define _STATIC_KEY_ 18 // Our 'Key' for rotating the array.
// We could make this dynamic, but
// - Lets just hardcode this thing!
// ================================
// Function Prototypes
// ================================
// Rotates the Array by the selected index
void RotateArray(int [], // Holds the incoming array;
// ACHTUNG! This array _will_ be changed!
int, // The size of the array
int); // The selected index in which will
// be the first to be rotated.
// Fill the Array with natural number ordering
void FillArray( int [], // The array to be populated with data
int); // The size of the array.
// Display the Array Data
void DisplayArray(int [], // The array that will be displayed in the STDOUT
int); // The size of the array.
// Duplicate Arrays (Source -> Target)
void MirrorArrays(int [], // Array Source
int [], // Array Target (what is to be changed)
int); // Size of the array
// ACHTUNG: IF MISMATCH SIZE OCCURS WITHIN THE ARRAYS
// THIS WILL BE THE SOURCE OF IT!
// BECAUSE OF THIS PROGRAM BEING SIMPLE, WE ASSUME
// THE SIZES WILL _MATCH_!
// Display the border on the screen.
void DisplayBorder();
// ================================
// Main Entry Point of the Program
int main()
{
// First, declare the array that we will be using
// in this program.
int numArray[_MAX_ARRAY_SIZE_];
// Second, populate the array with valid data.
// Without this, we will read garbage from the RAM.
// Populating this array is ABSOLUTE critical.
FillArray(numArray, _MAX_ARRAY_SIZE_);
// Third, display what the array looks like on the terminal buffer.
// This only displays the data stored in the array, nothing is
// changed during this operation - we are only READING from.
DisplayArray(numArray, _MAX_ARRAY_SIZE_);
// Fourth, perform the rotation algorithm - OUR HYPOTHESIS
RotateArray(numArray, _MAX_ARRAY_SIZE_, _STATIC_KEY_);
// Display a border to make reading the data on the screen easier
DisplayBorder();
// Fifth, confirm if our hypothesis holds true - display the results on the screen.
DisplayArray(numArray, _MAX_ARRAY_SIZE_);
// We are finished; terminate the program with no errors to report to the
// Operating System.
return 0;
} // main()
// Fill Array
// ---------------------
// This function will populate the array with natural
// numbers ordering; nothing fancy here.
// ---------------------
// Example:
// Populate the array with:
// {1, 2, 3, 4, 5, 6, (...), 1-n, n, n+1, n+2}
void FillArray(int arr[], int size)
{
// To populate the array (or initialize the array itself)
// We will use a for() to scan and initialize the indices.
for(int i = 0; i < size; i++)
arr[i] = i+1; // Remember, 'i' is being used as a Whole
// Number, use 'i + 1' to convert to Natural Numbers.
// The array is now populated; we are done in this function.
} // FillArray()
// Display Array
// ---------------------
// This function will display the array contents to the terminal buffer.
// The array will NOT be modified in this function; we are only reading
// 'FROM' the array, NOT WRITING to the array.
// We will also do exactly what the CS2 are expected to output this
// array; five columns per-row.
// ---------------------
// Example:
// 1 2 3 4 5
// 6 7 8 9 10
// 1-n n n+1 n+2 n+3
void DisplayArray(int arr[], int size)
{
// This for() loop is to read through all indices available in the array
for (int i = 0; i < size; i++)
{
// Organize the output in readable tables
if(i % 5 == 0)
std::cout << std::endl;
std::cout << arr[i] << "\t";
}
// Provide extra padding to make the STDOUT clean
std::cout << std::endl
<< std::endl;
} // DisplayArray()
// Rotate Array
// ---------------------
// This function will rotate the array's data within the indices.
// We will MODIFY the array by mirroring the a cached array to the
// original array.
// !!THIS IS THE MEAT OF THE PROGRAM!!
// **************************************
// HYPOTHESIS ON SOLVING THIS
// ---------------------
// Example:
// ORIGINAL:
// 1, 2, 3, 4, 5
// ROTATED AT KEY of 3
// 4, 5, 1, 2, 3
// ---------------------
// DEPENDENCY NOTE:
// MirrorArrays()
// Copy our changes to the original array.
void RotateArray(int arr[], int size, int key)
{
// Create a temporary array that will hold are values as we
// transition through the rotations.
int cacheArr[size];
// Scan through the whole original array
for(int i = 0; i < size; i++)
if(i + _STATIC_KEY_ > size - 1)
cacheArr[i + _STATIC_KEY_ - size ] = arr[i];
else
cacheArr[i + _STATIC_KEY_] = arr[i];
// Mirror the cache array to the original array
MirrorArrays(cacheArr, arr, size);
} // RotateArray()
// Duplicate Arrays
// ---------------------
// This function is designed to essentially duplicate the Source
// array to the Target array, thus all the data that is in the
// source will be 'copied' and 'pasted' to the target array.
// ---------------------
// Example:
// Source -->> Target
// 1, 2, 3 -->> Target
// 1, 2, 3 -->> 1, 2, 3
// SUCCESSFUL MIRROR
void MirrorArrays(int arrSource[], int arrTarget[], int size)
{
// Scan the arrays using a simple for() loop
for (int i = 0; i < size; i++)
arrTarget[i] = arrSource[i]; // Mirror the array
} // MirrorArrays()
// Display Border
// ---------------------
// This function is the most simplest ever function I have
// ever wrote in Cameron University! I am quite proud of this
// one!
void DisplayBorder()
{
std::cout << std::endl
<< "========================"
<< std::endl;
} // DisplayBorder()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment