Quicksort

Hello! Below is my attempt at a quicksort. Sorters are really interesting code because they can be done in so many different ways, which can make such a difference in the speed an array is sorted. Here is a visual example from my old professor Adam Smith. From this visual guide, you’re able to see when quicksort is most and least effective. Here’s some output:

Screen Shot 2018-02-02 at 12.00.27 PMScreen Shot 2018-02-02 at 12.01.15 PM

//
//  Quicksort2.cpp
//
//  Sorter which will sort array of random values in the "quicksort" style.
//
//  Created by Aidan Takami on 2/1/18.
//  Copyright © 2018 Aidan Takami. All rights reserved.
//


/*
     Must include iostream, cstdlib, and ctime. Site keeps deleting
     these libraries thinking they are HTML tags
/*
#include 
#include 
#include 



using namespace std;

void printArray(int r[]);
void switchValues(int r[], int, int);
void randmoArray(int r[]);
void quickSort(int r[], int, int);

//Declares the array to be sorted, as well as the const to represent the size of the array
int r[100];
const int SIZE = (sizeof(r)/4);


//Method to print out values of the array
void printArray(int r[])
{
    int i=0;
    while(i<SIZE){
        cout<<r[i]<<",";
        i++;
    }
}


//Method to swap the values at the locations of the 2 int args, within the array provided
void switchValues(int r[],int leftSide, int rightSide){
    int temp = r[leftSide];
    r[leftSide] = r[rightSide];
    r[rightSide] = temp;
}

//Method which will randomize an array of the size of the const int SIZE
void randomArray(int r[]){
    
    for(int rep = 0; rep < SIZE; rep++){
        
        r[rep] = int (rand()%100);
    }
}


//Quicksort method
void quicksort(int r[], int leftSide, int rightSide){
    
    int leftTemp = leftSide;
    int rightTemp = rightSide;
    int pivot = r[(leftSide+rightSide)/2];
    
    while(leftSidepivot)
            rightTemp--;
        
        if(leftTemp<=rightTemp){
            switchValues(r, leftTemp, rightTemp);
            leftTemp++;
            rightTemp--;

        }
        else{
            if(leftSide<rightTemp)
                quicksort(r, leftSide, rightTemp);
            if(leftTemp<rightSide)
                quicksort(r,leftTemp,rightSide);
            return;
        }
    }
}


//Main method for testing the quicksort
int main() {
  
    cout << "Generating random array!" << endl;
    randomArray(r);
    
    cout << "The unsorted array: ";
    printArray(r);
    
    cout << endl << endl;
    cout << "Sorting in progress..." << endl;
    const clock_t beginTime = clock();
    quicksort(r, 0, SIZE);
    float totalTime = ( clock () - beginTime ) /  CLOCKS_PER_SEC;
    
    cout << "The sorted array: ";
    printArray(r);
    cout << endl;
    
    cout << "The sort took " << totalTime << " milliseconds!" << endl;
    
    return 0;
}

Game Sample: Converse Method

This is a method from a game I worked on. The method takes 6 strings, 1 representing a possible prompt, and then the others are possible responses, and returns the char selected by the user.

This method is used in my text based game to format dialogue, take a response, and return the letter answered. The method can be sent empty strings for answers which are not relevant, and will dispose of them, not allowing a user to pick them as an answer.

Screen Shot 2018-01-28 at 11.39.59 AMScreen Shot 2018-01-28 at 11.43.15 AM

char converse(string statement, string responseA, string responseB, string responseC, string responseD, string responseE){

    //char to be returned, representing answer. then sleeps to create pause
    char answer;
    sleep(3);

    //bools to represent that the letter chosen is valid
    bool answerB = false;
    bool answerC = false;
    bool answerD = false;
    bool answerE = false;

    //question posed
    cout << endl << endl << statement << endl << endl << endl;

    //possible responses
    cout << "A. " << responseA << endl;           //if there are multiple possible responses, this will trigger and set the corresponding bool to true. //B if(responseB.length() > 0){
        cout << "B. " << responseB << endl; answerB = true; } //C if(responseC.length() > 0){
        cout << "C. " << responseC << endl; answerC = true; } //D if(responseD.length() > 0){
        cout << "D. " << responseD << endl; answerD = true; } //E if(responseE.length() > 0){
        cout << "E. " << responseE << endl; answerE = true;     } //loop to allow user to make invalid entry while(true){ //stores user's answer in "answer" char cin >> answer;
        answer = tolower(answer);
//swtich statement to return user's response
        switch(answer){
            //if user answers:
            case 'a':
                return 'a';
                break;
            //if user answers:
            case 'b':
                if(answerB == true) return 'b';
                else break;
            //if user answers:
            case 'c':
                if(answerC == true) return 'c';
                else break;
            //if user answers:
            case 'd':
                if(answerD == true) return 'd';
                else break;
            //if user answers:
            case 'e':
                if(answerE == true) return 'e';
                else break;
            //otherwise, loop again
            default:
                break;
        }

        cout << "INVALID RESPONSE. Please choose a valid response" <<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>< endl;
    }
}
</pre>

PV = nRT Solver

Hello! Below is a program I wrote after a Chemistry lecture today. PV = nRT is an incredibly fundamental equation within Chemistry. Otherwise known as the Ideal Gas Law, this formula combines basis’ of Boyle’s Law, Charles’ Law, and Avangadro’s Law into a simple equation, which we can use to find any missing variables from the equation, when given the other circumstances.

PV = nRT

P = pressure,

V = volume,

n = substance in moles,

R = 0.0821 (when unit is (atm*L)/(mol*K)) With other units, this constant will vary however, this is the constant used in this program, for these are the units used.            Read more on this constant

T = temperature

The program I have written will take input from the user for the values they DO have, and use those to find the “mystery” value, whichever variable that may be.

Screen Shot 2018-02-22 at 3.04.08 PM

//
//  main.cpp
//  PV=nRT
//
//  Created by Aidan Takami on 2/22/18.
//  Copyright © 2018 Aidan Takami. All rights reserved.
//


#include 
using namespace std;

void numbers();
double calculateP();
double calculateV();
double calculateN();
double calculateT();

//Fields
double p, v, n, t;
string pTemp = "";
string vTemp, nTemp, tTemp;
bool keepGoing = true;
const double R = 0.0821;

//Main
int main() {
    
    //Introduction & directions
    cout << "Hello! Welcome to the PV = nRT calculator! Type \"?\" for the value you are trying to find" << endl;

    
    while(keepGoing){
        //Calls calculation method
        numbers();
    }
    
}


//Method which gathers the numbers to be used in calculation
void numbers(){
    
    //Bool to represent whether mystery value has been found yet
    bool mysteryValue = false;
    
    //Bools to represent which value is mystery value
    bool pBool = false;
    bool vBool = false;
    bool nBool = false;
    bool tBool = false;
    
    //Used for continue/end process
    string temp;
    
   
    //Pressure
    cout << "What is the pressure in atm?" <> pTemp;
    
    //if the first value of the string is a digit
    if(isdigit(pTemp[0])){
        
        //convert the string to a double
        p = stod(pTemp);
    }
    else{
        
        pBool = true;
        mysteryValue = true;
    }

    
    
  
    //Volume
    cout << "What is the volume in L?" <> vTemp;
    
    //if the first value of the string is a digit
    if(isdigit(vTemp[0])){
        
        //convert the string to a double
        v = stod(vTemp);
    }
    else if(mysteryValue == true){
        
        //error message & restart
        cout << "We already have a mystery value! Let's try again!" << endl << endl;
        numbers();
    }
    else{
        
        vBool = true;
        mysteryValue = true;
    }
    
    
    
    
    
    //Moles
    cout << "What is the mole to molar mass ratio of the compound/element?" <> nTemp;
    
    //if the first value of the string is a digit
    if(isdigit(nTemp[0])){
        
        //convert the string to a double
        n = stod(nTemp);
    }
    else if(mysteryValue == true){
        
        //error message and restart
        cout << "We already have a mystery value! Let's try again!" << endl << endl;
        numbers();
    }
    else{
        
        nBool = true;
        mysteryValue = true;
    }
    
    
    
    
    
    //Temperature
    cout << "What is the temperature in Kelvin" <> tTemp;
    
    //if the first value of the string is a digit
    if(isdigit(tTemp[0])){
        
        //convert the string to a double
        t = stod(tTemp);
    }
    else if(mysteryValue == true){
        
        //error message and restart
        cout << "We already have a mystery value! Let's try again!" << endl << endl;
        numbers();
    }
    else{
        
        tBool = true;
        mysteryValue = true;
    }
    
    
    
    //if pressure = mystery value
    if(pBool){
        cout << "The Pressure is: " << calculateP() << " atm!" << endl << endl;
    }
    
    //if volume = mystery value
    else if(vBool){
        cout << "The Volume is: " << calculateV() << " L!" << endl << endl;
    }
    
    //if mole = mystery value
    else if(nBool){
        cout << "The Molar Ratio is: " << calculateN() << " mol!" << endl << endl;
    }
    
    //if temp = mystery value
    else if(tBool){
        cout << "The Temperature is: " << calculateT() << " K!" << endl << endl;
    }
    
    else{
        cout << "You answered your own question! I cannot verify whether it's true or not.!" << endl;
        cout << "Try again!" << endl << endl << endl;
    }

    
    
    //Continue?
    cout << "Would you like to continue? (y/n)" <> temp;
    
    if(temp == "no" || temp == "n"){
        keepGoing = false;
    }
    
    cout << endl;
}

//Calculates Pressure
double calculateP(){
    cout << (n * R * t) << endl;
    
    return (n*R*t)/v;
}


//Calculates Volume
double calculateV(){
    
    return (n*R*t)/p;
}


//Calculates Molar Ratio
double calculateN(){
    
    return (p*v)/(R*t);
}


//Calculates Temp
double calculateT(){
    
    return (p*v)/(R*n);
}

 

Game Sample: Speak Method

This method is a small part of a game I worked on. The purpose of the method is to create a life-like output of words, to mimic dialogue and feel more realistic when characters are communicating.

The method takes a string which is the words to be output, an int which represents, in milliseconds, the pause between each word, and another string, speaker, which is the name of the person speaking.

This method works in a fascinating way and really is able to break down a barrier which is often present in text based games, the lack of realistic interaction.

I am not sure if I will post the entirety of this adventure on my site, but I will continue to update on methods which go into the game.

Screen Shot 2018-01-28 at 11.39.59 AM

<pre>
/*
    Method which creates life like speech by dividing up and outputting single words seperated by intervals
 */

void speak(string statement, int pause, string speaker){
    cout << endl;
    //temp string and stringstream are created
    string temp;
    stringstream ss(statement);
    cout << speaker << ":   ";          //Creates pauses between the output of words     while(ss >> temp){
        std::this_thread::sleep_for(std::chrono::milliseconds(pause));
        cout << temp << " " << flush;
    }
}

</pre>