Programming

You are currently browsing the archive for the Programming category.

This one is a bit more crafty than the simple ROT-13 we worked on earlier and was used for military secrets/communication encryption back in the good ole’ days.

/*-----Greymask.com--------*/

#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>

// pad user input
void pad_message(std::string & message)
{
    int msglen = message.size();
    int side = static_cast < int >(std::sqrt(msglen));
    if (side * side != msglen) {
	++side;
    }

    int area = side * side;
    message.resize(area);

    // random number generator
    std::srand(std::time(0));

    for (int i = msglen; i < area; ++i) {
	message[i] = static_cast < char >(std::rand() % 26 + 'a');
    }
}

// print columns
void print_columns(std::string const &message)
{
    int side = static_cast < int >(std::sqrt(message.size()));

    for (int i = 0; i < side; ++i) {
	for (int j = 0; j < side; ++j) {
	    std::cout << message[side * j + i];
	}
    }
}

int main()
{
    char c;
    std::string message;

// gather user input as characters
    while (std::cin >> c) {
	message += c;
    }

// remove whitespaces/punctuation
    message.erase(remove_if(message.begin(), message.end(), isspace), message.end());
    message.erase(remove_if(message.begin(), message.end(), ispunct), message.end());
    message.erase(remove_if(message.begin(), message.end(), isdigit), message.end());

// encrypt
    pad_message(message);

// print out encyphered message
    print_columns(message);
    std::cout << "n";
}

For example, if we take this segment of lyrics by John Lennon’s “Imagine”:

Imagine there's no heaven
It's easy if you try
No hell below us
Above us only sky
Imagine all the people
Living for today

…and run it through this program, it will spit out the encrypted version:

IrnylbsalrmeIolokletastubvylLognsteeItidioerlumhvanhayosaeiyeesNwogpnltayouniegahvihslnofzeefeAyepof

More info on how Transposition ciphers work.

This is not pretty but it get’s the job done. It’s a fairly straightforward and (unnecessarily complicated and inefficient) example of a bubble sort used to sequentially sort an array. This array will be populated with pseudo random numbers using rand().

First, here is a header file (arraysort.h) with the necessary function prototype(s)/interface.

/*----- Greymask.com -----*/
#ifndef ARRAYSORT_H               // protect against multiple inclusion
#define ARRAYSORT_H

class Arraysort
{
private:
	int * array;            // pointer for array
	int size;
	bool sorted;            // set it from sort_array()

public:
	bool is_sorted(void);   // getter for sorted
	int  get_size(void);    // getter for size
	void load_array(int n); // load with random numbers mod n
	void print_array(void);
	void sort_array(void);  // bubble sort
	Arraysort(int n);       // Constructor
	~Arraysort(void);       // Destructor
};
#endif

Second, the meat of the program: Arraysort.cpp – This will contain all of our sorting and printing functions.

/*----- Greymask.com -----*/
#include <iostream>
#include <cstdlib>
#include "arraysort.h"

int *array;
int size;
bool sorted;

// constructor
Arraysort::Arraysort(int n)
{

    srand(time(0));		// seed random
    array = new int[n]; // allocate array dynamically
    size = n;           // set array size
    sorted = false;     // not sorted yet, so false
    load_array(n);      // load the array
}

int Arraysort::get_size()
{
    return size;        // return array size
}

void Arraysort::load_array(int n)
{
    sorted = false;
    size = n;
    int i = 0;

    do {
	array[i] = rand() % 100;	// fill array with random numbers
	i++;
    } while (i < size);
}

void Arraysort::print_array()
{
    // print array upto it's size
    int i = 0;
    do {
	std::cout << array[i] << std::endl;
	i++;
    } while (i < size);
}

void Arraysort::sort_array()
{
    // use bubble sort to sort the array from low to high
    int temp = 0;
    for (int j = 0; j < size - 1; j++) {
	for (int i = 0; i < size - 1; i++) {
	    if (array[i] > array[i + 1]) {
		temp = array[i];
		array[i] = array[i + 1];
		array[i + 1] = temp;
	    }
	}
    }
    // set sorted flag = true after sorting
    sorted = true;
}

bool Arraysort::is_sorted()
{
    return sorted;
}

// destructor
Arraysort::~Arraysort()
{
    delete[]array;
}

Finally, main.cpp to run the bad boy.

/*----- Greymask.com -----*/
#include <cstdio>
#include <cctype>
#include <iomanip>

#include <iostream>
using std::endl;
using std::endl;

#include "arraysort.h"

int main (int argc, char *argv[], char **env)
{
    Arraysort A(10);
	A.print_array();
	if (!A.is_sorted())
		A.sort_array();
	printf("==========================\n");
A.print_array();

return 0;

}

Here is how the output looks:

45
8
86
9
63
10
11
21
85
41
==========================
8
9
10
11
21
41
45
63
85
86

Again, this is not the best way to do this, but due to my slight dementia, I need to record this for future reference to get my nog going 🙂

« Older entries § Newer entries »