From 713c74a9193ff731f1091df1b855a1cc7667008b Mon Sep 17 00:00:00 2001 From: left_adjoint Date: Wed, 28 Feb 2024 18:08:39 -0800 Subject: [PATCH] catching up on examples --- array2.cpp | 22 ++++++++++++++++++++++ array3.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ find1.cpp | 16 ++++++++++++++++ fun2.cpp | 15 +++++++++++++++ prototype1.cpp | 16 ++++++++++++++++ sarcasmCase.cpp | 24 ++++++++++++++++++++++++ substring.cpp | 18 ++++++++++++++++++ switch1.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ tostr.cpp | 13 +++++++++++++ whileRef.cpp | 35 +++++++++++++++++++++++++++++++++++ 10 files changed, 242 insertions(+) create mode 100644 array2.cpp create mode 100644 array3.cpp create mode 100644 find1.cpp create mode 100644 fun2.cpp create mode 100644 prototype1.cpp create mode 100644 sarcasmCase.cpp create mode 100644 substring.cpp create mode 100644 switch1.cpp create mode 100644 tostr.cpp create mode 100644 whileRef.cpp diff --git a/array2.cpp b/array2.cpp new file mode 100644 index 0000000..218787a --- /dev/null +++ b/array2.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +void printArray(int arr[], int size){ + for(int i=0; i < size; i++){ + cout << arr[i] << endl; + } +} + +int main(){ + //set aside memory for 20 integers + int myArray[20]; + for(int i = 0; i < 20; i++){ + myArray[i] = i*i; // myArray_i = i^2 + } + + //for(int i = 0; i < 20; i++){ + // cout << myArray[i] << endl; + //} + printArray(myArray,20); + return 0; +} \ No newline at end of file diff --git a/array3.cpp b/array3.cpp new file mode 100644 index 0000000..bbbb8bc --- /dev/null +++ b/array3.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +void printRow(string name, string species, int age){ + cout << "Pet: " << name << " is a " << species << " and is " << age << " year(s) old" << endl; +} + +int main(){ + const int arraySize = 5; + string petName[arraySize]; + string petSpecies[arraySize]; + int petAge[arraySize]; + // populate arrays + for(int i=0; i < arraySize; i++){ + cout << "Pet name: "; + cin >> petName[i]; + cout << "Pet species: "; + cin >> petSpecies[i]; + cout << "Pet age: "; + cin >> petAge[i]; + } + + // modify this program to: + // ask for a pet species + // and a maximum age + // and print only entries that match that + // hint: put an if-statement in the for loop below + int maxAge = 0; + string species; + + cout << "Max age: "; + cin >> maxAge; + cout << "Species: "; + cin >> species; + //print arrays: + for(int i=0; i < arraySize; i++){ + if(petAge[i] <= maxAge && petSpecies[i] == species){ + printRow(petName[i],petSpecies[i],petAge[i]); + } + } + + return 0; +} \ No newline at end of file diff --git a/find1.cpp b/find1.cpp new file mode 100644 index 0000000..6b2d967 --- /dev/null +++ b/find1.cpp @@ -0,0 +1,16 @@ +#include +#include +using namespace std; + +int main(){ + string str1 = "My head is full of rocks, rocks I say!"; + string str2 = "paper"; + + cout << int(str1.find(str2)) << endl; + + // let's check what happens at runtime if I access + // outside the length of a string + + cout << str2.length() << endl; + return 0; +} \ No newline at end of file diff --git a/fun2.cpp b/fun2.cpp new file mode 100644 index 0000000..f8b52b2 --- /dev/null +++ b/fun2.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int sillyFun(int num){ + num = num + 1; + return num; +} +// ourNumber = sillyFun(ourNumber) "equivalent" to pass-by-reference?? + +int main(){ + int ourNumber = 10; + cout << "sillyFun returns: " << sillyFun(ourNumber) + << endl; + cout << "ourNumber is: " << ourNumber << endl; +} \ No newline at end of file diff --git a/prototype1.cpp b/prototype1.cpp new file mode 100644 index 0000000..33256c1 --- /dev/null +++ b/prototype1.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int adder(int num1, int num2); +int rec(int num); + +int main(){ + + adder(1,2); + return 0; +} + +int adder(int num1, int num2){ + cout << "Look I'm adding" << endl; + return num1 + num2; +} diff --git a/sarcasmCase.cpp b/sarcasmCase.cpp new file mode 100644 index 0000000..d9a8ae1 --- /dev/null +++ b/sarcasmCase.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +using namespace std; + +string sarcasmCase(string s){ + for(int i =0; i < s.length(); i++){ + if(rand() % 2 == 0){ + s[i] = toupper(s[i]); + } + else{ + s[i] = tolower(s[i]); + } + } + return s; +} + +int main(){ + srand(time(0)); + + string str = "you can't do that!"; + + cout << sarcasmCase(str) << endl; +} \ No newline at end of file diff --git a/substring.cpp b/substring.cpp new file mode 100644 index 0000000..6d44a0e --- /dev/null +++ b/substring.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main(){ + string str1 = "My head is full of rocks, rocks I say!"; + string str2 = "rock"; + + int pos = str1.find(str2); + cout << str1.substr(pos,str2.length()) << endl; + cout << str1.substr(pos,str2.length()+1) << endl; + cout << (str1.substr(pos,0) == "") << endl; + + cout << str1.substr(pos,string::npos) << endl; + cout << int(string::npos) << endl; + + return 0; +} \ No newline at end of file diff --git a/switch1.cpp b/switch1.cpp new file mode 100644 index 0000000..c98880c --- /dev/null +++ b/switch1.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +int main(){ + int option = 0; + cout << "Enter a number between 1 and 5" << endl; + + cin >> option; +/* + if(option == 1){ + //... + } + else if(option == 2){ + //... + } //... + else { + + } +*/ + + switch(option){ + case 1: + cout << "You have one dog" << endl; + break; + case 2: + cout << "You have two cats" << endl; + break; + case 3: + case 4: + cout << "You have some number of birds" << endl; + break; + case 5: + cout << "That's a lot of ducks" << endl; + break; + default: + cout << "These are your ideal pets" << endl; + } + + return 0; +} \ No newline at end of file diff --git a/tostr.cpp b/tostr.cpp new file mode 100644 index 0000000..d2ae138 --- /dev/null +++ b/tostr.cpp @@ -0,0 +1,13 @@ +#include +#include +using namespace std; + +int main(){ + int thing = 300; + char c = 'a'; + double d = 3.14; + + cout << to_string(thing) + to_string(c) + to_string(d) << endl; + + return 0; +} \ No newline at end of file diff --git a/whileRef.cpp b/whileRef.cpp new file mode 100644 index 0000000..c25dc7e --- /dev/null +++ b/whileRef.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +// should run a while loop collecting values +// and return the sum of those values +// *and* put the number of values read in +// into the reference variable numberOfValues +// loop should keep going until user enters +// something < 0 +double averageRef(int &numberOfValues); + +int main(){ + int numberThings = 0; + double sum = averageRef(numberThings); + double sum2 = averageRef(numberThings); + cout << "The average is: " << sum2 / numberThings << endl; + return 0; +} + +double averageRef(int &numberOfValues){ + numberOfValues = 0; + double temp = 0; + double input = 0; + + cout << "Enter a number (<0 to quit): "; + cin >> input; + while(input >= 0){ + numberOfValues++; + temp = temp + input; + cout << "Enter a number (<0 to quit): "; + cin >> input; + } + + return temp; +} \ No newline at end of file