Skip to content

Instantly share code, notes, and snippets.

@natecraddock
Created February 23, 2021 17:41
Show Gist options
  • Save natecraddock/a44401bc5ed640aab64a15018dc92acc to your computer and use it in GitHub Desktop.
Save natecraddock/a44401bc5ed640aab64a15018dc92acc to your computer and use it in GitHub Desktop.
Examples from class
#include <iostream>
#include <string>
#include <cstdlib> // for rand(), srand(), time()
using namespace std;
const int NUMBER = 10;
// Example of a generalized function to return a random int in a range
int RandIntInRange(int min, int max) {
// See zyBooks 5.15 for more info
return rand() % (max - min + 1) + min;
}
int main() {
// without srand() the sequence of random numbers would the the same
// each time you run the program.
// "Seeding" the random number with the time will fix this problem.
// Most code with rand() will use srand(time(0)) in this way
srand(time(0));
for (int i = 0; i < NUMBER; ++i) {
int number = RandIntInRange(2, 5);
cout << number << endl;
}
return 0;
}
// This example isn't critical to understand at the moment,
// It was more to give a conceptual overview of what things
// we can create with code once we have the concept of list
// data (vectors and arrays).
// Once you complete the readings in chapter 6 this will
// make more sense, but feel free to take a look now! :)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
// Create a vector (list) containing strings that we refer to by the name of "names".
// It starts empty.
vector<string> names;
while (true) {
string input;
cout << "Enter your name: ";
cin >> input;
if (input == "DONE") {
break;
}
// Add the input name to the end of the list.
names.push_back(input);
}
// Output each name in the list on a new line
// Notice that .size() and .at() are very similar to
// accessing chararcters in a string!
for (int i = 0; i < names.size(); ++i) {
// Access the string at index i in the list
cout << names.at(i) << endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
const int NOT_FOUND = -1;
int Find(string text, string search) {
// Iterate through characters of the string (starting points)
for (int i = 0; i < text.size(); i++) {
// Assume that it does match until proven otherwise
bool match = true;
// Iterate through characters of the search string
// Also check that we don't search beyond the `text` string's length
// with (i + j) < text.size()
for (int j = 0; j < search.size() && (i + j) < text.size(); j++) {
// Compare each letter in the search string (j) with the letters in the
// string to be searched (i). i + j is to ensure we are also stepping through
// the characters in the string to be searched at the same rate as the search
// string.
if (text.at(i + j) != search.at(j)) {
// If characters don't match we can set this to false
match = false;
break; // We can exit the loop early because we know for a fact it doesn't match
}
}
// A match was found. Return the index of the first matching character.
if (match) {
return i;
}
}
// We return a negative number to indicate that it wasn't found.
// Because a valid position cannot be negative, -1 is a good option
// to represent "not found".
return NOT_FOUND; // similar to string::npos
}
int main() {
string sentence;
string search;
cout << "Enter a sentence: ";
getline(cin, sentence);
cout << "Enter a search word: ";
cin >> search;
// Here it would be best to use sentence.find(search), but to
// show an example of nested loops (and some principles of find
// functions) this is a useful demo.
int findIndex = Find(sentence, search);
if (findIndex != NOT_FOUND) {
cout << search << " was found at index " << findIndex << endl;
}
else {
cout << search << " was not found in " << sentence << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment