catching up on examples
This commit is contained in:
parent
09688e3c0e
commit
713c74a919
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include <iostream>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#include <iostream>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <iostream>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include <iostream>
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include <iostream>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#include <iostream>
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue