Reading: Section 8.2
This is to illustrate arrays. We'll just go through the first parts.
#include <iostream>
#include <string>
using namespace std;
// arraySum: return the sum of the elements in the array
double arraySum(vector<double> &arr, int arr_len) {
double total = 0.0;
for(int i = 0; i < arr_len; i = i + 1) {
total = total + arr[i];
}
return total;
}
// winter: animals age
void winter(double &forage, vector<double> &deer,
vector<double> &panthers) {
for(int i = 11; i > 0; i = i - 1) {
panthers[i] = panthers[i - 1];
}
panthers[0] = 0;
for(int i = 7; i > 0; i = i - 1) {
deer[i] = deer[i - 1];
}
deer[0] = 0;
}
// spring: things renew
void spring(double &forage, vector<double> &deer,
vector<double> &panthers) {
deer[0] = arraySum(deer, 8) / 2;
panthers[0] = 3 * (arraySum(panthers, 12) - panthers[1]) / 2;
forage = forage * 1.5;
if(forage > 5e7) { // forage overgrown;
forage = 5e7; // prune some
}
}
// summer: deer eat
void summer(double &forage, vector<double> &deer,
vector<double> &panthers) {
double num_deer = arraySum(deer, 8);
if(num_deer * 130 > forage / 2) { // some deer starve
double surviving_deer = forage / 2 / 130;
for(int i = 0; i < 8; i = i + 1) { // starve deer
deer[i] = deer[i] * surviving_deer / num_deer;
}
num_deer = surviving_deer;
}
forage = forage - num_deer * 130; // forage eaten
}
// fall: panthers eat
void autumn(double &forage, vector<double> &deer,
vector<double> &panthers) {
double num_deer = arraySum(deer, 8);
double num_panthers = arraySum(panthers, 12);
if(num_panthers * 50 > num_deer / 2) { // some panthers starve
double surviving_panthers = num_deer / 2 / 50;
for(int i = 0; i < 12; i = i + 1) { // starve panthers
panthers[i] = panthers[i] * surviving_panthers
/ num_panthers;
}
num_panthers = surviving_panthers;
}
double surviving_deer = num_deer - num_panthers * 50;
for(int i = 0; i < 8; i = i + 1) { // deer eaten
deer[i] = deer[i] * surviving_deer / num_deer;
}
}
int main() {
// initialize the preserve
double forage;
forage = 1e7;
cout << "How many deer? ";
double n;
cin >> n;
vector<double> deer(8);
for(int i = 0; i < 8; i = i + 1) {
deer[i] = n / 8;
}
vector<double> panthers(12);
for(int i = 0; i < 12; i = i + 1) {
panthers[i] = 2.0;
}
// pass through the years
int year = 0; // how many years have passed
while(forage > 100 && arraySum(deer, 8) > 2
&& arraySum(panthers, 12) > 2) {
year = year + 1; // another year passes
winter(forage, deer, panthers);
if(forage > 100 && arraySum(deer, 8) > 2
&& arraySum(panthers, 12) > 2) {
spring(forage, deer, panthers);
}
if(forage > 100 && arraySum(deer, 8) > 2
&& arraySum(panthers, 12) > 2) {
summer(forage, deer, panthers);
}
if(forage > 100 && arraySum(deer, 8) > 2
&& arraySum(panthers, 12) > 2) {
autumn(forage, deer, panthers);
}
}
// print the result
cout << "The preserve lasted " << year << " years." << endl;
return 0;
}
This is to demonstrate what you can do with strings.
#include <iostream>
#include <string>
// Returns 1 if tofind occurs anywhere in tosearch. For your bound,
// let m be the length of tosearch and let n be the length of tofind
int isSubstring(const string &tosearch, const string &tofind) {
int i; // position within tosearch
for(i = 0; i < tosearch.length(); i = i + 1) {
int j = 0; // will be #chars in tofind matching tosearch starting at i
while(j < tofind.length() && tosearch[i + j] == tofind[j]) {
j = j + 1; // we have a match; go on to next character
}
if(j == tofind.length()) {
return 1; // everything must have matched
}
}
return 0; // tofind doesn't match anywhere within tosearch
}
int main() {
string source;
string pattern;
cout << "Search for what within what? ";
cin >> pattern;
while(pattern != "quit") {
cin >> source;
if(isSubstring(source, pattern)) {
cout << pattern << " appears within " << source << endl;
} else {
cout << pattern << " doesn't occur in " << source << endl;
}
cout << "Search for what within what? ";
cin >> pattern;
}
}
This is another example of recursion. It's a little more complicated than anything else we've seen.
#include <iostream>
#include <string>
void allSubsets(int depth, vector<int> &chosen, int n) {
if(depth == n) { // we've made all choices; print this subset
int i;
for(i = 0; i < depth; i++) {
if(chosen[i]) {
cout << " " << (i + 1);
}
}
cout << endl;
} else {
chosen[depth] = 0; // choose #depth to not be in the subset
allSubsets(depth + 1, chosen, n);
chosen[depth] = 1; // now choose #depth to be in the subset
allSubsets(depth + 1, chosen, n);
}
}
int main() {
int n;
cout << "Choose from how many? ";
cin >> n;
vector<int> subset(n);
allSubsets(0, subset, n);
}