Notes week of Nov 3

Muddiest points & want to learn

Activity 4.2.6: Range of data types

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;
}

Activity 4.5.2

Providing the different parts of the for loop: initializer, test, and update.

Activity 4.6.3

I don’t understand why it prints 20 and 21 if the cout is after the for 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).

Challenge homework 4.6.1

#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;
}

Challenge homework 4.6.2

// 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;
}

Challenge homework 4.8.1

#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;
}

Activity 9.3.1

Went over the meaning of fixed, scientific, and setprecision. Precision is interpreted a bit differently in fixed vs in scientific.

How to use loops

Here are comments from three different students, that indicate we’re hitting the steepest part of the learning curve!

  • While loops as a whole. I don’t understand the concepts behind it.
  • How to use for loops when coding
  • How to use while loop iterations

Keep plugging and practicing!

Project 6 solution

#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;
}