#include <iostream>
#include <vector>
using namespace std;
int main() {
const int VALS_SIZE = 6;
vector<int> valsVctr(VALS_SIZE);
int i = 0;
int sumVal = 0;
double avgVal = 0;
valsVctr.at(0) = 30;
valsVctr.at(1) = 20;
valsVctr.at(2) = 20;
valsVctr.at(3) = 15;
valsVctr.at(4) = 5;
valsVctr.at(5) = 10;
sumVal = 0; // accumulator variable
/* FIXME: Write for loop to iterate through vector */
for(i = 0; i < VALS_SIZE; i++)
{
cout << "Vector at " << i << " contains " << valsVctr.at(i)
<< endl;
sumVal += valsVctr.at(i);
}
avgVal = (double)sumVal / VALS_SIZE;
cout << "Avg: " << avgVal << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
// Compute a FACTORIAL of 5.
// 5! = 5*4*3*2*1
// 8! = 8 * (7 * (6*5*4*3*2*1))
// n! = n * (n-1)!
int num = 40;
int i = 1;
long long fac = 1;
while(i <= num)
{
fac = fac * i;
cout << "Multiply by " << i << " to get " << fac << endl;
i++;
}
cout << num << "! is " << fac << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
// How many multiples of 7 are there
// between 15 and 100?
const int START = 15;
const int STOP = 100;
const int MULTIPLE = 7;
int i = START;
int numMultiples = 0;
// Count from START to STOP
for( ; i <= STOP; i++)
{
if(i % MULTIPLE == 0) {
numMultiples++;
cout << "Got " << i << " (#" << numMultiples << ")" << endl;
}
else {
cout << "Excluding " << i << endl;
}
}
cout << "There were " << numMultiples
<< " multiples of " << MULTIPLE << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int VALS_SIZE = 6;
vector<int> valsVctr(VALS_SIZE);
int i = 0;
int sumVal = 0;
double avgVal = 0;
valsVctr.at(0) = 30;
valsVctr.at(1) = 25;
valsVctr.at(2) = 20;
valsVctr.at(3) = 15;
valsVctr.at(4) = 5;
valsVctr.at(5) = 10;
// Count # even numbers in the vector
int count = 0;
for(i = 0; i < VALS_SIZE; i++)
{
cout << "Vector at " << i << " contains " << valsVctr.at(i)
<< endl;
if(valsVctr.at(i) % 2 == 0) {
count++;
cout << "EVEN!" << endl;
}
}
cout << "There are " << count << " even numbers." << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
string s;
cout << "Enter a word: ";
getline(cin, s);
int n = s.length();
cout << n << " characters." << endl;
for(int i = 0; i < n; i++) {
// switch( ... )
// {
// case 'a': case 'A':
// case 'e': case 'E':
// case 'i': case 'I':
// // handle vowels
// break;
// default:
// // handle non-vowels
// }
cout << "Char at " << i << " is "
<< s.at(i) << endl;
}
return 0;
}
On back
vs. pop_back
: the push and pop operations modify the vector, but back
just retrieves the last element without modifying anything.
v.back()
is equivalent v.at(v.size()-1)
What’s the real difference between an array and a vector?
Vectors can easily resize. Vectors are bounds-checked. So when you do v.at(i)
, it will throw an error message if i
is out of range.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> courseGrades;
int i = 0;
courseGrades.push_back(4);
courseGrades.push_back(8);
courseGrades.push_back(2);
courseGrades.push_back(16);
// Put the number 9 in position 4, expanding the
// size of the vector as needed.
courseGrades.push_back(9);
courseGrades.push_back(2);
courseGrades.push_back(15);
courseGrades.push_back(71);
// Print values forwards
for(int i = 0; i < courseGrades.size(); i++) {
// cout << "(" << i << ")";
cout << courseGrades.at(i) << " ";
}
cout << endl;
// Now backwards
for(int i = courseGrades.size()-1; i >= 0; i--) {
// cout << "(" << i << ")";
cout << courseGrades.at(i) << " ";
}
cout << endl;
// Print the last element
cout << "The last element is " << courseGrades.back() << endl;
courseGrades.pop_back(); // Remove last element
courseGrades.pop_back(); // Remove last element
courseGrades.pop_back(); // Remove last element
cout << "After popping three times, last is "
<< courseGrades.back() << endl;
return 0;
}
#include <iostream>
using namespace std;
// DECLARATION of the function (aka PROTOTYPE)
void PrintFace(char faceChar);
// void is the RETURN TYPE (void means "no value")
// faceChar is a PARAMETER (aka ARGUMENT aka INPUT)
int main() {
PrintFace('x'); // CALL of the function
cout << "Alice" << endl;
PrintFace('o'); // another CALL
cout << "Bob" << endl;
cout << "What character for YOUR face? ";
char myFaceChar;
cin >> myFaceChar;
PrintFace(myFaceChar+5); // another CALL
cout << "¡YOU!" << endl;
// A variety of faces
for(char c = 'a'; c <= 'k'; c++) {
PrintFace(c);
}
return 0;
}
// DEFINITION of the function
void PrintFace(char faceChar) {
cout << " " << faceChar << " " << faceChar << endl; // Eyes
cout << " " << faceChar << endl; // Nose
cout << " " << faceChar << faceChar << faceChar << endl; // Mouth
cout << endl;
return;
}
#include <iostream>
#include <cmath>
using namespace std;
// Function DECLARATIONS (aka PROTOTYPES)
double areaOfCircle(double radius);
double square(double num);
// The 'double' on the left are RETURN TYPES
// The 'radius' and 'num' are PARAMETERS
int main()
{
const double RADIUS = 8.72;
// Below is a CALL, in the context of a cout.
cout << areaOfCircle(RADIUS) << endl;
return 0;
}
double square(double num)
{
return num * num;
}
double areaOfCircle(double radius)
{
return M_PI * square(radius);
}