clowncar vector solution

Amazingly, this solution came fast an easily:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//

 

/*

 1. empty car (0)

 2. pick up Pennywise and John Wayne Gacy (2)

 3. Pick up Bozo and Krusty (4) 

 4. Pick up Ronald McDonald (5)

 5. Ronald McDonald gets kicked out (4)

 6. All the rest stop for a drink at the Alehouse together (0)

 */

 

#include <iostream>

#include <vector>

usingnamespacestd;

using std::vector;

 

//functions

void printVectorContents(vector<string>);

 

int main(int argc, const char * argv[])

{

    vector<string> clowncar;     //sets up a vector for fish

    int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    clowncar.push_back(“Pennywise”);

    clowncar.push_back(“John Wayne Gacy”);

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.push_back(“Bozo”);

    clowncar.push_back(“Krusty”);

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.push_back(“Ronald McDonald”);

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.pop_back();

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    while(clowncar.size() > 0){

        clowncar.pop_back();

    }

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

 

    return 0;

}

 

 

//functions

void printVectorContents(vector<string> car){

    for(int i=0; i < car.size(); i++){

        cout<<“clown #”<<i<<“: “<<car[i]<<endl;

    }

}

 

 

/*  output – surprisingly, this worked right first try.

    the timing of the cout statements for vector size is a bit kooky, but it’s working the way I wanted

 

 vector is size: 0

 vector is size: 2

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 vector is size: 4

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 clown #2: Bozo

 clown #3: Krusty

 vector is size: 5

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 clown #2: Bozo

 clown #3: Krusty

 clown #4: Ronald McDonald

 vector is size: 4

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 clown #2: Bozo

 clown #3: Krusty

 vector is size: 0

 Program ended with exit code: 0

 

*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: