From 3cc00673a423fbf2a8549e48499eab0a3136d1c6 Mon Sep 17 00:00:00 2001 From: Josh Bundt Date: Tue, 26 Jul 2011 14:11:15 -0700 Subject: [PATCH] adding classroom code --- Class711.cpp | 61 ++++++++++++++++ Class712.cpp | 67 ++++++++++++++++++ Class713.cpp | 133 ++++++++++++++++++++++++++++++++++ Class715.cpp | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++ Class718.cpp | 143 +++++++++++++++++++++++++++++++++++++ Class719.cpp | 126 +++++++++++++++++++++++++++++++++ Class720.cpp | 163 ++++++++++++++++++++++++++++++++++++++++++ Class725.cpp | 101 ++++++++++++++++++++++++++ 8 files changed, 990 insertions(+) create mode 100755 Class711.cpp create mode 100755 Class712.cpp create mode 100755 Class713.cpp create mode 100755 Class715.cpp create mode 100755 Class718.cpp create mode 100755 Class719.cpp create mode 100755 Class720.cpp create mode 100755 Class725.cpp diff --git a/Class711.cpp b/Class711.cpp new file mode 100755 index 0000000..80d9bc1 --- /dev/null +++ b/Class711.cpp @@ -0,0 +1,61 @@ +//============================================================================ +// Name : Classroom1.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +using namespace std; + +int mypower(int n, int i) { + + if (i < 0) { + return -1; + } + + int result = 1; + + while (i > 0) { + + result = n * result; //result *= n + + i--; + } + + return result; +} + +int square(int n) { + + return n * n; + +} + +int main() { + + int val = mypower(2, -4); + + if (val < 0) { + cout << "Error"<< endl; + + } else { + cout << "Result " << val << endl; + } + + cout << "2 ** -4 " << mypower(2, -4) << endl; +// cout << "5 ** 0 " << mypower(5, 0) << endl; + +/* int cnt = 1; + + while (cnt <= 10) { + + cout << cnt << " " << square(cnt) << endl; + cnt = cnt + 1; //cnt += 1 or cnt++ + } +*/ + return 0; + + +} diff --git a/Class712.cpp b/Class712.cpp new file mode 100755 index 0000000..ec32c60 --- /dev/null +++ b/Class712.cpp @@ -0,0 +1,67 @@ +//============================================================================ +// Name : Class712.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +using namespace std; + +int sum(int n) { + + int total = 0; + + //Version 1 -- loop +// for (int i = 1; i <= n; i++) { +// total += i; +// } + while (n > 0) { + total += n; + n--; + } + + //Version 2 -- closed form + //total = n * (n + 1) / 2; //watch out for integer division + + //Version 3 -- recursion +// if (n == 0) { +// return 0; +// } else { +// total = sum(n-1) + n; +// return total; +// } + + return total; +} + + +int main() { + + int val; + + do { + cout << "Enter (neg to stop): "; + cin >> val; + + if (val < 0) break; + + cout << "val: " << val << endl; + cout << "Result: " << sum(val) << endl << endl; + cout << "val: " << val << endl; + /* + * The call-by-value parameter passing mechanism is used + * when calling the sum function. Consequently, the value of + * the variable 'val' does not change, even the corresponding + * parameter 'n' in the function is modified. + */ + +// if (val >= 0) { +// cout << "Result: " << sum(val) << endl << endl; +// } + + } while (true);// (val >= 0); + + return 0; +} diff --git a/Class713.cpp b/Class713.cpp new file mode 100755 index 0000000..2deede5 --- /dev/null +++ b/Class713.cpp @@ -0,0 +1,133 @@ +//============================================================================ +// Name : Class713.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +#include +#include +using namespace std; + +#define PI 3.14159265 + +void sample1( ) { + double degree, result; + degree = 60.0; + + result = cos (degree * PI/180); + + cout << result << endl; + + return; +} + +void string1( ) { + + string str; + int n; + + n = 10; + str = "Hello"; + string space = ""; + + for (int i = 0; i < 10; i++) { + + space += " "; + + cout << space << str << endl; + // cout << str << " " << i << endl; + } + + return; +} + +void string2 ( ) { + + string first, last; + + first = "John"; + last = "Smith"; + + string fullname1 = first + " " + last; //John Smith + string fullname2 = last + ", " + first; //Smith, John + cout << fullname1 << endl << fullname2 << endl; + + string fullname3 = ""; + +// fullname3 = first[0]; +// fullname3 = fullname3 + ". " + last; + + + fullname3.append(1, first[0]); //change 1 to 3 and see what happens + fullname3.append(". " + last); + //or + //fullname3 = fullname3 + ". " + last; + + cout << fullname3 << endl; + + /* + Note: + + We cannot the string concatenation operation as + + string s = first[0] + ". " + last; + + because of data type mismatch. Expression first[0] is char, not string + */ + + return; +} + +void string3( ) { + + cout << endl << endl; + + string str = "This is a test"; + + cout << "'" << str << "' has " << str.size() << " characters." << endl; + cout << str.substr(0,4) << endl; + cout << "First blank space in the string at position " << str.find(" ") << endl; + cout << str << endl; + + return; +} + +void string4( ) { + //find the number of blank spaces in a given string + string str = "Hello world how are you ."; + + string searchStr = " "; + + int cnt = 0; + int loc = str.find(searchStr); + + while (loc >= 0) { /*not found*/ + + cnt++; + + str = str.substr(loc+1,str.size( )); + loc = str.find(searchStr); + } + + cout << cnt << endl; + + return; +} + +int main() { + + //sample1( ); + + //string1( ); + + //string2( ); + + //string3( ); + + string4(); + + return 0; +} diff --git a/Class715.cpp b/Class715.cpp new file mode 100755 index 0000000..78996fd --- /dev/null +++ b/Class715.cpp @@ -0,0 +1,196 @@ +//============================================================================ +// Name : Class715.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +#include +#include +using namespace std; + +void practice1_Step1() { + + const float DEG_TO_RADIAN = 3.14159 / 180.0; + float dist, height, alpha, beta; + + cout << "Enter distance between points A and B: "; + cin >> dist; + + cout << "Enter degree alpha: "; + cin >> alpha; + + cout << "Enter degree beta: "; + cin >> beta; + + height = 150; + + cout << "distance: " << dist << endl; + cout << "alpha: " << alpha << endl; + cout << "beta: " << beta << endl << endl; + cout << "height: " << height << endl; + +} + +/* + * d - distance between A and B + * a - angle A in radian + * b - angle B in radian + * + * return h - height = d * sin(a) * sin(b) / sqrt(sin(a + b) * sin(a-b)) + */ +float computeHeight(float d, float a, float b) { + //Note: Pass values such that a > b? + float h; + + //h = 150.0; //test value to check the function before + //writing a full implementation + + h = d * sin(a) * sin(b) / sqrt(sin(a+b) * sin(a-b)); + + //test outputs +// cout << sin(a) << endl; +// cout << sin(b) << endl; +// cout << sin(a+b) << endl; +// cout << sin(a-b) << endl; + + return h; +} + +void practice1_Step2(){ + + const float DEG_TO_RADIAN = 3.14159 / 180.0; + float dist, height, alpha, beta; + + cout << "Enter distance between points A and B: "; + cin >> dist; + + cout << "Enter degree alpha: "; + cin >> alpha; + + cout << "Enter degree beta: "; + cin >> beta; + + height = computeHeight(dist, alpha * DEG_TO_RADIAN, beta * DEG_TO_RADIAN); + + cout << "distance: " << dist << endl; + cout << "alpha: " << alpha << endl; + cout << "beta: " << beta << endl << endl; + cout << "height: " << height << endl; +} + +/* + * Count how many times string c occurs in string s + */ +int search(string s, string c) { + int result = 0; + + int loc = 0; + + while (true) { + + loc = s.find(c, loc); //start looking for c in s from loc + + if (loc < 0) break; + + result++; + loc++; + } + + return result; +} +/* + * Scan the string multiple times. Each scan will detect + * the number of occurrence of a specified vowel. + */ +void practice2Ver1() { + + string str; + + /* + * NOTE: The following input routine does not work as normally expected. + * Variable 'str' is terminated by a blank space so if you enter + * + * This is a test + * + * the text 'This' is assigned to str. + * + * You need to use the getline function to read the complete string, + * up to the RETURN key press. + */ +// cout << "Enter String: " ; +// cin >> str; + + cout << "Enter string: "; + getline(cin, str); //input full line + + int cnt = 0; + + cnt += search(str, "a"); + cnt += search(str, "e"); + cnt += search(str, "i"); + cnt += search(str, "o"); + cnt += search(str, "u"); + + cout << "Vowel Cnt: " << cnt; +} + +/* + * In this version, we scan the string only once. For each character we + * process, we test if it is a lowercase vowel and increment the vowel + * counter accordingly. + */ +void practice2Ver2(){ + + string str; + + cout << "Enter string: "; + getline(cin, str); //input full line + + int cnt = 0; + + for (int loc = 0; loc < str.size(); loc++) { + char ch = str[loc]; + //NOTE: char constant is single quote + if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { + cnt++; + } + } + + cout << "Vowel Cnt: " << cnt; +} + + +int main() { + + /* + * Implement this exercise in 2 steps. In Step 1 we implement + * input and output routines using a fixed (dummy) output. Once + * we confirm the routines work okay, then we implement the + * computation routine. Two steps are written in two separate + * functions to illustrate the development steps. In practice, + * you write the first step and then add or modify code to the + * existing code. + */ + //practice1_Step1(); + //practice1_Step2(); + + /* + * Count the number of lowercase vowels in a given string str. + * There are two (perhaps more) approaches. Approach 1 is to scan + * the string for a specific character. We are looking for + * five specific characters (a, e, i, o, & u) so we scan + * the string fives, one for each vowel. Approach 2 is to scan + * the string exactly once. For each character in the string, + * check if this character is a vowel. + * + * Again you should develop the functions in incremental steps. Here + * we provide only the final version. + */ + //practice2Ver1(); + practice2Ver2(); + + return 0; +} diff --git a/Class718.cpp b/Class718.cpp new file mode 100755 index 0000000..e3b82c3 --- /dev/null +++ b/Class718.cpp @@ -0,0 +1,143 @@ +//============================================================================ +// Name : Class718.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +using namespace std; + +void sample1(){ + //Compute the average of given input values -- 0 to terminate + double val, sum, avg; + int cnt; + + sum = 0.0; + cnt = 0; + + do { + cout << "Enter value (0 to terminate): "; + cin >> val; + + if (val == 0) break; + + cnt++; + sum += val; + + } while (true); + + avg = sum / cnt; + + cout << endl << "Average: " << avg << endl; +} + +void sample2(){ + + //compute the average of given input values + //determine the number of values less than average and those + //greater than average + + //KEY POINT: I need to REMEMBER the input values + + double number[100]; //static declaration and allocation + + double val, sum, avg; + int cnt, belowAvgCnt, aboveAvgCnt; + + sum = 0.0; + cnt = belowAvgCnt = aboveAvgCnt = 0; + + do { + cout << "Enter value (0 to terminate): "; + cin >> val; + + if (val == 0) break; + + number[cnt] = val; + + cnt++; + sum += val; + + } while (true); //NOTE: This loop will crash if someone tries to enter more than 100 numbers + + avg = sum / cnt; + + for (int i = 0; i < cnt; i++) { + if (number[i] < avg) { + belowAvgCnt++; + } else if (number[i] > avg) { + aboveAvgCnt++; + } + } + + cout << endl << "Average: " << avg << endl; + cout << "Below Average Cnt: " << belowAvgCnt << endl; + cout << "Above Average Cnt: " << aboveAvgCnt << endl; + +} + +int sum(int collection[ ], int size) { + + int total = 0; + + for (int i = 0; i < size; i++ ) { + total += collection[i]; + } + + return total; +} + +void sample3(){ + + //Passing an array to a function + + //static initialization of arrays + int arr1[ ] = {1, 4, -5, 8, 6, 9, 19}; +// int arr1[7]; +// arr1[0] = 1; +// arr1[1] = 4; + //... + + int arr2[ ] = {10, 8, 6, 4, 2, 0, -2, -4, -6, -8, -10}; + + cout << "Sum 1: " << sum(arr1, 7) << endl; + cout << "Sum 2: " << sum(arr2, 11) << endl; +} + +void sample4() { + + int mylist[ ] = {10, 8, 6, 4, 2, 0, 1, 3, 5, 7, 9}; + + int sum1 = 0; + for (int i = 0; i < 11; i++) { //sum of even integers + if (mylist[i] % 2 == 0) { + sum1 += mylist[i]; + } + } + + int sum2 = 0; + for (int j = 0; j < 11; j++) { //sum of integers in even index position + if (j % 2 == 0) { + sum2 += mylist[j]; + } + } + cout << "Sum of even ints = " << sum1 << endl; + cout << "Sum of ints in even pos = " << sum2 << endl; + + +} + +int main() { + +// sample1(); + +// sample2(); + +// sample3(); + + sample4(); + + return 0; +} diff --git a/Class719.cpp b/Class719.cpp new file mode 100755 index 0000000..df279d8 --- /dev/null +++ b/Class719.cpp @@ -0,0 +1,126 @@ +//============================================================================ +// Name : Class719.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +using namespace std; + +//An array is a homogeneous collection of data items. In contrast +//a struct is a heterogeneous collection of data items. + +struct Cat { + string name; + string breed; + int age; + float wgt; +}; + + +void clearArray(int num[ ], int size) { + + //Is an array passed by value?? Let's test + for (int i = 0; i < size; i++) { + num[i] = 0; + } + +} + +void sample1( ) { + + int list[ ] = { 10, 20, 30, 40, 50, 60}; + + for (int i = 0; i < 6; i++) { + cout << " " << list[i]; + } + + cout << endl; + + clearArray(list, 6); + + for (int i = 0; i < 6; i++) { + cout << " " << list[i]; + } + + cout << endl; +} + +void sample2( ) { + + Cat cat1, cat2; + + cat1.name = "Puff Puff"; + cat1.breed = "Mixed"; + cat1.age = 3; + cat1.wgt = 13.5; + + cout << cat1.name << endl; + cout << cat1.breed << endl; + cout << cat1.age << endl; + cout << cat1.wgt << endl; + +// cout << cat1 << endl; //?? + + cat2 = cat1; + + cout << cat2.name << endl; + cout << cat2.breed << endl; + cout << cat2.age << endl; + cout << cat2.wgt << endl; + + cat2.age += 100; + + cout << cat1.age << endl; + cout << cat2.age << endl; +} + +void happyBday(Cat cat) { + cat.age += 1; + + cout << cat.age << endl; +} + +void sample3( ) { + + Cat cat1; + + cat1.name = "Puff Puff"; + cat1.breed = "Mixed"; + cat1.age = 3; + cat1.wgt = 13.5; + + happyBday(cat1); + + cout << cat1.age << endl; +} + +void sample4( ) { + + //Combine two composite types array and struct into one + //Here's an array of struct + Cat myPet[10]; + + myPet[0].name = "Bom Bom"; + myPet[0].breed = "Maine Coon"; + myPet[0].age = 10; + myPet[0].wgt = 9.8; + + //... +} + + +int main() { + +// sample1( ); + +//~ sample2( ); + +//~ sample3( ); + + sample4( ); + + return 0; +} diff --git a/Class720.cpp b/Class720.cpp new file mode 100755 index 0000000..f3dc3fe --- /dev/null +++ b/Class720.cpp @@ -0,0 +1,163 @@ +//============================================================================ +// Name : Class720.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +#include +using namespace std; + + +void sample1( ) { + + int* p1; + int * p2; + int *p3; //p1, p2, and p3 are all pointers to an int + + int* a, b; //WATCH OUT: a is a pointer to int but b is a regular int variable + + //The values of pointers are addresses so you cannot assign a data value to them directly. + //We need the dereferencing operator. But before we apply this operator, we need to + //create a memory location to store a data value. + + p1 = new int; //DYNAMIC ALLOCATION, Yeah!! Now we have a box to store an int + //The 'new' operation returns int* an address of a newly created int box + + *p1 = 45; + + p2 = p1; //Now we have two pointers referring to the same data + + p3 = new int; //another box + + *p3 = *p1; //data in a box pointed by p1 is copied to another box pointed by p3 + + cout << *p1 << " " << *p2 << " " << *p3 << endl; + + *p3 = 20; + + cout << *p1 << " " << *p2 << " " << *p3 << endl; + + *p1 = 50; + + cout << *p1 << " " << *p2 << " " << *p3 << endl; + +} + + +void sample2( ) { + + int n; + + int* p; + + n = 100; + + p = &n; //& is the address-of operator + + //at this point, p points to the int box named 'n' + + cout << n << " " << *p << endl; + +} + +void exchange1(int a, int b) { + + int t = a; + a = b; + b = t; + +} + +void exchange2(int* a, int* b) { + + int t; + t = *a; + *a = *b; + *b = t; + +} + +void sample3( ) { + + int i = 10; + int j = 20; + + exchange1(i, j); + + cout << i << " " << j << endl; + + int* p1; + int* p2; + + p1 = &i; p2 = &j; + + exchange2(p1, p2); + + cout << *p1 << " " << *p2 << endl; +} + +void clear(int list[ ], int size) { //We are actually passing a pointer to the first position of an array + + for (int i = 0; i < size; i++) { + list[i] = 0; + } + +} + +void clear2(int *list, int size) { + + for (int i = 0; i < size; i++) { + list[i] = 0; + } +} + +void clear3(int list[], int size) { + + int* p; + + p = list; + + int cnt = 0; + + while (cnt < size) { + + *p = 0; + + p = p + 1; + + cnt++; + } +} + + +void sample4( ) { + + int num[ ] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; + + clear(num, 10); + + clear2(num, 10); + + //clear3(num, 10); + + for (int i = 0; i < 10; i++) { + cout << " " << num[i]; + } +} + +int main() { + + sample1(); + + sample2(); + + sample3(); + + sample4( ); + + + return 0; +} diff --git a/Class725.cpp b/Class725.cpp new file mode 100755 index 0000000..fc31e2a --- /dev/null +++ b/Class725.cpp @@ -0,0 +1,101 @@ +//============================================================================ +// Name : Class725.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +//============================================================================ +// Name : Class725.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +using namespace std; + +void swap(int* p, int* q) { //exchanging the two values + + int t = *p; + *p = *q; + *q = t; +} + +void swap2(int* p, int* q) { //this confirms that pointers are passed by value + + //set p point to q's value box and + //q point to p's value box + + int* t; + + t = p; + p = q; + q = t; +} + +void swap3(int& r, int& s) { + + int t; + + t = r; //value box is referenced directly by the reference variable; + r = s; //you don't need a deference operator like the one for the pointer variable + s = t; //Reference variable is fixed; it can't change to refer to a different value box +} + +void sample1() { + + int* p1; + int* p2; + + p1 = new int; + p2 = new int; + + *p1 = 10; + *p2 = 20; + + cout << *p1 << " " << *p2 << endl; + + swap(p1, p2); + + cout << *p1 << " " << *p2 << endl; + + *p1 = 100; + *p2 = 200; + + cout << endl; + cout << *p1 << " " << *p2 << endl; + + swap2(p1, p2); + + cout << *p1 << " " << *p2 << endl; +} + +void sample2( ) { + + //This is an example of call-by-reference + int n, m; + + n = 10; + m = 20; + + cout << endl; + cout << n << " " << m << endl; + + swap3(n, m); + + cout << n << " " << m << endl; +} + + +int main() { + + sample1(); + + sample2(); + + return 0; +} +