Looked at this. Change int
to long long
for numAnc
. Then it won’t wrap around to a negative number and collapse to zero, unless you go back really far.
#include <iostream>
using namespace std;
int main() {
const int YEARS_PER_GEN = 20; // Approx. years per generation
int userYear = 0; // User input
int consYear = 0; // Year being considered
long long numAnc = 0; // Approx. ancestors in considered year
cout << "Enter a past year (neg. for B.C.): ";
cin >> userYear;
consYear = 2020;
numAnc = 2;
while (consYear >= userYear) {
cout << "Ancestors in " << consYear << ": " << numAnc << endl;
numAnc = 2 * numAnc; // Each ancestor had two parents
consYear = consYear - YEARS_PER_GEN; // Go back 1 generation
}
return 0;
}
Providing the different parts of the for
loop: initializer, test, and update.
I don’t understand why it prints 20 and 21 if the
cout
is after thefor
statement.
Went through this. The update section of the for
loop is not executed until after the loop body (even though it comes before in the program).
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
int i = 0;
int j = 0;
cout << "Input a limit: ";
cin >> userNum;
for(i = 0; i < userNum; i++) {
// Print i spaces
for(j = 0; j < i%7; j++) {
cout << " ";
}
cout << i << endl;
}
return 0;
}
// Given numRows and numCols, print a list of all seats in a theater.
// Rows are numbered, columns lettered, as in 1A or 3E. Print a space
// after each seat, including after the last.
// Ex: numRows = 2 and numCols = 3 prints:
// 1A 1B 1C 2A 2B 2C
#include <iostream>
using namespace std;
int main() {
int numRows = 3;
int numCols = 7;
for(int i = 1; i <= numRows; i++)
{
for(char j = 'A'; j < 'A'+numCols ; j++)
{
cout << i << j << " ";
}
}
cout << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string simonPattern;
string userPattern;
int userScore = 0;
int i = 0;
userScore = 0;
simonPattern = "RRGBRYYBGY";
userPattern = "RRGBBRYBGY";
for(i = 0; i < simonPattern.length(); i++)
{
cout << "Comparing position " << i << ": "
<< simonPattern.at(i) << " vs " << userPattern.at(i) << endl;
if(simonPattern.at(i) != userPattern.at(i)) {
break;
}
userScore++;
}
cout << "userScore: " << userScore << endl;
return 0;
}
Went over the meaning of fixed
, scientific
, and setprecision
. Precision is interpreted a bit differently in fixed vs in scientific.
Here are comments from three different students, that indicate we’re hitting the steepest part of the learning curve!
Keep plugging and practicing!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double start, stop, step;
cout << "Enter start: ";
cin >> start;
while(true) {
cout << "Enter stop: ";
cin >> stop;
if(start < stop) {
break;
}
cout << "ERROR: start must be less than stop." << endl;
}
while(true) {
cout << "Enter step: ";
cin >> step;
if(step > 0) break;
cout << "ERROR: step must be more than zero." << endl;
}
// for(;;) { // Leaving all blank means infinite loop
// cout << "WHEEEE!" << endl;
// }
cout << "MILES KILOMETERS" << endl;
cout << fixed << setprecision(3) << right;
for( ; start <= stop; start += step)
{
double km = start * 1.60934;
cout << setfill(' ');
cout << setw(15) << start
<< " "
<< setw(15) << km << endl;
}
return 0;
}