Monday, September 26, 2016

Week 4 PowerPoints


Code:
//
//  main.cpp
//  Sept26
//
//  Created by Mark Brautigam on 9/26/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

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

double pi = acos(-1.0);

double squareArea (double side) {
    return side * side;
}

double degrees2radians (double degrees) {
    return (degrees * pi / 180);
}

double radians2degrees (double radians) {
    return (radians * 180 / pi);
}

void getSideAndComputeArea () {
    cout << "Please enter the length of a side: ";
    double ui;
    cin >> ui;
    cout << "The area is " << squareArea (ui) << endl;
}

double rectangle (double w, double h) {
    return w * h;
}

double triangle (double a, double b, double c) {
    double s = (a + b + c) / 2;
    double t = s * (s-a) * (s-b) * (s-c);
    if (t < 0)
        ; // ...
    return sqrt (t); // <cmath>
}

long factorial (long n) {
    if (n <= 1)
        return 1;
    else
        return n * factorial (n-1);
}

long fibonacci (long n) {
    if (n <= 2)
        return 1;
    return fibonacci (n-1) + fibonacci (n-2);
}

int main(int argc, const char * argv[]) {
    // insert code here...
    
    cout << fibonacci (5) << endl;
    cout << fibonacci (6) << endl;
    cout << fibonacci (45) << endl;
    // cout << factorial (24.5) << endl;
    // cout << triangle(2, 12, 11) << endl;
    
    // getSideAndComputeArea();
    // getSideAndComputeArea();
    // getSideAndComputeArea();
    
    /*
     cout << squareArea (12) << endl;
    cout << degrees2radians(45) << endl;
    cout << radians2degrees(0.785398) << endl;
    
    double rad = degrees2radians(57);
    cout << rad << endl;
    cout << radians2degrees(rad) << endl;
    
    double x = 17;
    double y = 42;
    cout << rectangle (x, y) << endl;
     */
    return 0;
}



Sunday, September 25, 2016

Homework assignments 3 and 4

These are due on Monday, Oct. 3 before class starts.

Coding assignment 3

Coding assignment 4

There are no videos to go with these assignments. The first assignment asks you to write a series of if...else statements. The second assignment asks you to calculate a mathematical equation that has +, –, *, /, and square roots.


Monday, September 19, 2016

Week 3 PowerPoints

Week 3 PDF

Week 3 PPTX

Resistor colors:

0Black
1Brown
2Red
3Orange
4Yellow
5Green
6Blue
7Violet
8Gray
9White


Week 3 code. This is the code I typed in class.

//
//  main.cpp
//  Sept19
//
//  Created by Mark Brautigam on 9/19/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

#include <iostream>
using namespace std;

string getString (string s) {
    string returnValue;
    cout << s << ": ";
    cin >> returnValue;
    return returnValue;
}

double getDouble (string s) {
    double returnValue;
    cout << s << ": ";
    cin >> returnValue;
    return returnValue;
}

int getInteger (string s) {
    int returnValue;
    cout << s << ": ";
    cin >> returnValue;
    return returnValue;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    // std::cout << "Hello, World!\n";
    
    int userNumber = getInteger ("Give me a number from 0 to 9");
    /*
     double anotherNumber = getDouble ("Give me a second number");
    cout << "The sum is " << userNumber + anotherNumber << endl;
     */
    /*
    if (userNumber % 2 == 1) {
      cout << userNumber << " is odd." << endl;
    }
    else {
        cout << userNumber << " is even." << endl;
    }
     */
    /*
    if (userNumber == 0) {
        cout << "Black" << endl;
    }
    else if (userNumber == 1) {
        cout << "Brown" << endl;
    }
    else if (userNumber == 2) {
        cout << "Red" << endl;
    }
    else if (userNumber == 3) {
        cout << "Orange" << endl;
    }
    else if (userNumber == 4) {
        cout << "Yellow" << endl;
    }
    else if (userNumber == 5) {
        cout << "Green" << endl;
    }
    else if (userNumber == 6) {
        cout << "Blue" << endl;
    }
    else if (userNumber == 7) {
        cout << "Violet" << endl;
    }
    else if (userNumber == 8) {
        cout << "Gray" << endl;
    }
    else {
        cout << "White" << endl;
    }
     */
    switch (userNumber) {
        case 0 : cout << "Black"; break;
        case 1 : cout << "Brown"; break;
        case 2 : cout << "Red"; break;
        case 3 : cout << "Orange"; break;
        case 4 : cout << "Yellow"; break;
        case 5 : cout << "Green"; break;
        case 6 : cout << "Blue"; break;
        case 7 : cout << "Violet"; break;
        case 8 : cout << "Gray"; break;
        case 9 : cout << "White"; break;
        default : cout << "Not recognized"; break;
    }
    cout << endl;

    return 0;
}




Programming Assignment 2

Code:

//
//  main.cpp
//  homework2
//
//  Created by Mark Brautigam on 9/18/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

/*
 Mark Brautigam - ID 123456
 CS 102 - Fall 2016 - Ohlone College
 Sept. 18, 2016
 */

#include <iostream>
#include <math.h>       /* sqrt */
using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    cout << "Hello, World!" << endl;
    
    cout << "What is your zip code? ";
    string zipcode;
    cin >> zipcode;
    cout << "Your zip code: " << zipcode << endl;
    
    // Arithmetic operations
    cout << "What is the length of side a : ";
    string a;
    cin >> a;
    double aa = atof(a.c_str());
    
    cout << "What is the length of side b : ";
    string b;
    cin >> b;
    double bb = atof(b.c_str());
    
    //     _  /  2   2
    // c =  \/  a + b
   
    double cc = sqrt ((aa * aa) + (bb * bb));
    cout << "The hypotenuse is " << cc << endl;
    
    char c = 320;
    cout << c << endl;
    
    short s = 12345;
    int i = 4321;
    int pi = 3.14;
    cout << pi << endl;
    
    return 0;
}

/*
 Hello, World!
 What is your zip code? asdf
 Your zip code: asdf
 What is the length of side a : 4
 What is the length of side b : 3
 The hypotenuse is 5
 @
 3
 Program ended with exit code: 0
 */




Programming Assignment 1


Code, in case you have trouble reading the video:

//
//  main.cpp
//  homework1b
//
//  Created by Mark Brautigam on 9/18/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

/*
 Mark Brautigam - ID 123456
 CS 102 - Fall 2016 - Ohlone College
 Sept. 18, 2016
 */

#include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    cout << "Hello, World!" << endl;
    
    string name = "Mark Brautigam";
    cout << name << endl;
    
    string nick = "Mixed Up";
    string nonsense = nick + " " + name;
    cout << nonsense << endl;
    
    cout << "name = " << name << endl;
    cout << "nick = " << nick << endl;
    cout << "nonsense = " << nonsense << endl;
    
    int x = 17;
    cout << "Enter a value for x: ";
    string xstring;
    cin >> xstring;
    x = atoi(xstring.c_str());
    int xcubed = x * x * x; // x^3
    cout << xcubed << endl;
    cout << "If x is " << x << ", then xcubed = " << xcubed << endl;
    
    return 0;
}

Monday, September 12, 2016

Online C++ Compilers

The course materials will go way beyond the capabilities of these online resources very quickly. But you may use them in a pinch for very small projects like the ones we will do in class the first few weeks.

Code Chef
https://www.codechef.com/ide

IDE One
http://ideone.com/
(tends to go down a lot, especially during class)

C++ Shell
http://cpp.sh

Tutorials Point
http://www.tutorialspoint.com/compile_cpp11_online.php
(Choose C++11 from the menu if it's not chosen already)


New Project in Xcode (Mac)

1. Start Xcode
2. File --> New --> Project ...
3. Choose OS X --> Application --> Command Line Tool

(click to enlarge)


4. Next
5. Product name should be whatever you like
6. Organization name should be your name
7. Organization identifier should be com.yourname (insert your own name)
8. Language should be C++

(click to enlarge)


9. Next
10. Choose "Desktop" for the location
11. Create

You'll get a project that has a main.cpp file. That file should look like this:

//
//  main.cpp
//  Week3homework
//
//  Created by Mark Brautigam on 9/12/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

#include <iostream>

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
}

Note that this code looks a lot like the template code I gave you in another post.


Sunday, September 11, 2016

Hello World Program

This hello world program is from page 7 in the textbook. It should work on any computer you try to use.

If you ask for an empty project in Visual Studio (Windows) or Xcode (Mac), you might get a program that has a little bit more than this, but the extra stuff is "mostly harmless." If you get a lot more stuff than this, you probably asked for the wrong kind of project.

#include <iostream>
using namespace std;

// main: generate some simple output

int main ()
{
  cout << "Hello, world." << endl;
  return 0;
}