Monday, October 31, 2016

October 31 PowerPoints: Vector

Presentations:

Comments

  • The code for srand() did not work because we were using the random() function to get random numbers. The textbook suggested random()  I don't even know where random() comes from ... it's not in any standard reference. 
  • Instead, use the function rand(). This function works with the function srand() to give you different random numbers each time.
  • insert() is not so difficult to use, but it requires iterators, something we haven't talked about yet. See the code below.

Code
//
//  main.cpp
//  Oct31
//
//  Created by Mark Brautigam on 10/31/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;

vector<string> resistorColors = { "black", "brown", "red", "orange",
    "yellow", "green", "blue", "violet", "gray", "white" };

int colorToNumber (string color) {
    int colorFoundWhere = -1;
    for (int i=0; i<resistorColors.size(); i++) {
        if (resistorColors[i] == color) {
            colorFoundWhere = i;
            break;
        }
    }
    return colorFoundWhere;
}

int resistorCalculation (string c1, string c2, string c3) {
    int a = colorToNumber(c1);
    int b = colorToNumber(c2);
    int p = colorToNumber(c3);
    return (a * 10 + b) * pow(10, p);
}


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

    vector<double> temperatures;
//    vector<string> resistorColors;
    
    cout << resistorColors.size() <<endl;
    cout << resistorColors.max_size() <<endl;
    cout << resistorColors.capacity() <<endl;
    // resistorColors.resize(32);
    // cout << resistorColors.size() <<endl;
    // cout << resistorColors.max_size() <<endl;
    // cout << resistorColors.capacity() <<endl;
    
    // cout << resistorColors[17] << endl;
    
    // temperatures.resize(4);
    // cout << temperatures[2] << endl;
    
    cout << resistorColors[1] << endl;
    cout << resistorColors.size() <<endl;
    cout << resistorColors.capacity() <<endl;
    
    // cout << resistorColors << endl; // doesn't work
    
    for (int i=0; i<resistorColors.size(); i++) {
        cout << resistorColors[i] << endl;
        // i++; // DON'T do this
    }
    
    int i=0;
    while (i<resistorColors.size()) {
        cout << resistorColors[i] << endl;
        i++; // don't forget this
    }
    
    // find a color
    string colorToFind = "red";
    int colorFoundAtIndex = colorToNumber("green");
    
    cout << colorToFind << " is number " << colorFoundAtIndex << endl;
    
    cout << "Blue is " << colorToNumber("blue") << endl;
    cout << "Orange is " << colorToNumber("orange") << endl;
    
    cout << resistorCalculation("yellow", "violet", "orange") << endl;
    
    // temperatures.resize(0);
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;
    temperatures.push_back(98.6);
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;
    temperatures.push_back(32.0);
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;
    temperatures.push_back(212);
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;
    temperatures.push_back(212);
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;
    temperatures.push_back(212);
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;
    
    temperatures[6] = -432;
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;
    
    // I wrote this wrong in class, it's a little more complicated
    vector<double>::iterator it;
    it = temperatures.begin();
    temperatures.insert(it+5, 100);
    cout << temperatures.size() << endl;
    cout << temperatures.capacity() << endl;

    // generate random numbers between 1 and 100 instead of 0 to 99
    cout << 1 + rand() % 100 << endl;
    cout << 1 + rand() % 100 << endl;
    cout << 1 + rand() % 100 << endl;
    cout << 1 + rand() % 100 << endl;
    
    vector<int> randomNumbers;
    srand(time(NULL));
    
    // initialize with 10 random numbers
    for (int i=0; i<10; i++) {
        randomNumbers.push_back(rand() % 100);
    }
    
    // print the vector of random numbers
    cout << "---------\n";
    for (int i=0; i<randomNumbers.size(); i++) {
        cout << randomNumbers[i] << endl;
    }
    
    // bubble sort
    for (int i=0; i<randomNumbers.size(); i++) {
        for (int j=0; j<randomNumbers.size()-1; j++) {
            if (randomNumbers[j] > randomNumbers[j+1]) {
                // out of order - swap them
                int temp = randomNumbers[j];
                randomNumbers[j] = randomNumbers[j+1];
                randomNumbers[j+1] = temp;
            }
        }
    }
    
    // print the vector of sorted numbers
    cout << "---------\n";
    for (int i=0; i<randomNumbers.size(); i++) {
        cout << randomNumbers[i] << endl;
    }
    
    // calculate positions of numbers on a clock
    // We will talk about top-down design again next week
    /*
    void printClockNumber(int clock) {
        double degrees = clockToDegrees(clockNumber);
        double radians = degreesToRadians(degrees);
        double x = getX (radians, center, radius);
        double y = getY (radians, center, radius);
    }
     */

    return 0;
}



No comments:

Post a Comment