Skip to content

Commit

Permalink
adding classroom code
Browse files Browse the repository at this point in the history
  • Loading branch information
wideglide committed Jul 26, 2011
1 parent 655cd24 commit 3cc0067
Show file tree
Hide file tree
Showing 8 changed files with 990 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Class711.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//============================================================================
// Name : Classroom1.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
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;


}
67 changes: 67 additions & 0 deletions Class712.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//============================================================================
// Name : Class712.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
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;
}
133 changes: 133 additions & 0 deletions Class713.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//============================================================================
// Name : Class713.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <math.h>
#include <string>
#include <iostream>
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;
}
Loading

0 comments on commit 3cc0067

Please sign in to comment.