Monday, October 3, 2016

October 3 PowerPoints

Instructor's in-class presentations for October 3:
Code
//
//  main.cpp
//  phi2
//
//  Created by Mark Brautigam on 10/3/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

#include <iostream>
using namespace std;

// A recursive function that computes PHI to the
// specified accuracy based on an initial estimate
//
double phi (double depth) {
    if (depth == 1)
        return 1.5;
    else
        return 1 + 1 / phi(depth-1);
}

// Function overloading ... three functions with the
// same name, but different signatures.
//
string combine (string a, string b) {
    return a+b;
}

double combine (double x, double y) {
    return x*y;
}

int combine (int p, int q) {
    return p+q;
}

// The MAIN function
// The proram starts here.

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

    // some string functions: Chapter 7
    //
    string s = "amanaplanpanama";
    cout << s[10] << endl;
    cout << s.length() << endl;
    cout << s.find("ama") << endl;
    cout << s.rfind("ama") << endl;
    cout << s + " zowie!" << endl;
    cout << s.substr(9, 6) << endl;
    
    // A 2-dimensional ASCII table with
    // nested while loops
    cout << "\t";
    int i=0;
    while (i<=15) {
        cout << std::hex << i << "\t";
        i++;
    }
    cout << endl;
    
    int row = 2;
    // This "outer" loop prints a sequence of rows.
    //
    while (row <= 7) {
        cout << row << "\t";
        i = 0;
        
        // This "inner" loops prints the contents of one row.
        //
        while (i<=15) {
            cout << char(row*16+i) << "\t";
            i++;
        }
        cout << endl;
        row++;
    }
    
    // Print HEX and ASCII values in a linear table
    //
    i = 32;
    while (i <= 127) {
        cout << std::dec << i << "\t" << std::hex << i << "\t" << char(i) << endl;
        i++;
    }
    
    // Print squares and cubes in a linear table
    //
    i = 1;
    while (i <= 20) {
        cout << i << "\t" << i*i << "\t" << i*i*i << endl;
        i++;
    }
    
    // Print the numbers from 50 to 100 using a while loop
    //
    i = 50;
    while (i <= 100) {
        cout << i << " ";
        i++;
    }
    
    // Use the overloaded "combine" functions declared above.
    //
    cout << combine (17, 42) << endl;
    cout << combine (17.0, 42.0) << endl;
    cout << combine ("Hello", "World") << endl;
    
    // Arithmetic operators are overloaded to handle different types.
    cout << 17+42 << endl;
    cout << 17.0+42.0 << endl;
    // cout << "seventeen"+"fortytwo" << endl;
    
    // Compute PHI to various levels of accuracy.
    //
    cout << phi(20) << endl;
    cout << phi(40) << endl;
    return 0;
}



I got this output:
a
15
0
12
amanaplanpanama zowie!
panama
  0 1 2 3 4 5 6 7 8 9 a b c d e f 
2   ! " # $ % & ' ( ) * + , - . / 
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 
4 @ A B C D E F G H I J K L M N O 
5 P Q R S T U V W X Y Z [ \ ] ^ _ 
6 ` a b c d e f g h i j k l m n o 
7 p q r s t u v w x y z { | } ~  
32 20  
33 21 !
34 22 "
35 23 #
36 24 $
37 25 %
38 26 &
39 27 '
40 28 (
41 29 )
42 2a *
43 2b +
44 2c ,
45 2d -
46 2e .
47 2f /
48 30 0
49 31 1
50 32 2
51 33 3
52 34 4
53 35 5
54 36 6
55 37 7
56 38 8
57 39 9
58 3a :
59 3b ;
60 3c <
61 3d =
62 3e >
63 3f ?
64 40 @
65 41 A
66 42 B
67 43 C
68 44 D
69 45 E
70 46 F
71 47 G
72 48 H
73 49 I
74 4a J
75 4b K
76 4c L
77 4d M
78 4e N
79 4f O
80 50 P
81 51 Q
82 52 R
83 53 S
84 54 T
85 55 U
86 56 V
87 57 W
88 58 X
89 59 Y
90 5a Z
91 5b [
92 5c \
93 5d ]
94 5e ^
95 5f _
96 60 `
97 61 a
98 62 b
99 63 c
100 64 d
101 65 e
102 66 f
103 67 g
104 68 h
105 69 i
106 6a j
107 6b k
108 6c l
109 6d m
110 6e n
111 6f o
112 70 p
113 71 q
114 72 r
115 73 s
116 74 t
117 75 u
118 76 v
119 77 w
120 78 x
121 79 y
122 7a z
123 7b {
124 7c |
125 7d }
126 7e ~
127 7f 
1 1 1
2 4 8
3 9 1b
4 10 40
5 19 7d
6 24 d8
7 31 157
8 40 200
9 51 2d9
a 64 3e8
b 79 533
c 90 6c0
d a9 895
e c4 ab8
f e1 d2f
10 100 1000
11 121 1331
12 144 16c8
13 169 1acb
14 190 1f40
32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 3b
714
HelloWorld
3b
59
1.61803
1.61803
Program ended with exit code: 0


No comments:

Post a Comment