Assignment 13
Due Wednesday, February 28 at 9:20am


Assignment 13.1: Multiplication Tables (10 pts)

  • One of the principle applications of nested loops is to display tabular data (data organized into rows and columns).
  • In this assignment, we will create a program to display the times tables.
  • Create a new file called multiplication.cpp
  • Copy and paste the starter code into your program and fill in your name in the comments section:

/**
* Name
* CIS 22A
*/

int main() {

    int size = 0;
    cout << "***Time to Learn The Times Table!***\n\n";
    //write the rest of the code here

    return 0;
}
  • Next, add code to prompt the user to enter in a number for the times tables they want to be displayed.
  • For example: If the user enters 5, the program will display the times tables from 1 up to and including 5.
  • The user should see a message like this one appear on the console:
Enter the size of the times table: _
  • Next, read in the user input and store it in the size variable.
  • Finally, add a nested for loop to your program to print out the times tables from 1 to size
  • Fill in the missing parts of the code here:

for (int row = ????; row <= 12; row++) { //printing times table 1 through 12
    for (int col = ????; col <= ????; col++) { //for the numbers up to size
        cout << col << " * " << row << "\t";
    }
    cout << endl;

}

  • Now, as an intermediate step, run your code and verify that you get the following output:

***Time to Learn The Times Table!***

Enter the size of the times table: 5
1 * 1     2 * 1     3 * 1     4 * 1     5 * 1   
1 * 2     2 * 2     3 * 2     4 * 2     5 * 2   
1 * 3     2 * 3     3 * 3     4 * 3     5 * 3   
1 * 4     2 * 4     3 * 4     4 * 4     5 * 4   
1 * 5     2 * 5     3 * 5     4 * 5     5 * 5   
1 * 6     2 * 6     3 * 6     4 * 6     5 * 6   
1 * 7     2 * 7     3 * 7     4 * 7     5 * 7   
1 * 8     2 * 8     3 * 8     4 * 8     5 * 8   
1 * 9     2 * 9     3 * 9     4 * 9     5 * 9   
1 * 10    2 * 10    3 * 10    4 * 10    5 * 10   
1 * 11    2 * 11    3 * 11    4 * 11    5 * 11   
1 * 12    2 * 12    3 * 12    4 * 12    5 * 12       

  • Finally, let's alter the cout statement inside the for loop so that we print out the products, getting an output like the following:
***Time to Learn The Times Table!***

Enter the size of the times table: 5
1 * 1 = 1      2 * 1 = 2      3 * 1 = 3      4 * 1 = 4      5 * 1 = 5   
1 * 2 = 2      2 * 2 = 4      3 * 2 = 6      4 * 2 = 8      5 * 2 = 10   
1 * 3 = 3      2 * 3 = 6      3 * 3 = 9      4 * 3 = 12     5 * 3 = 15   
1 * 4 = 4      2 * 4 = 8      3 * 4 = 12     4 * 4 = 16     5 * 4 = 20   
1 * 5 = 5      2 * 5 = 10     3 * 5 = 15     4 * 5 = 20     5 * 5 = 25   
1 * 6 = 6      2 * 6 = 12     3 * 6 = 18     4 * 6 = 24     5 * 6 = 30   
1 * 7 = 7      2 * 7 = 14     3 * 7 = 21     4 * 7 = 28     5 * 7 = 35   
1 * 8 = 8      2 * 8 = 16     3 * 8 = 24     4 * 8 = 32     5 * 8 = 40   
1 * 9 = 9      2 * 9 = 18     3 * 9 = 27     4 * 9 = 36     5 * 9 = 45   
1 * 10 = 10    2 * 10 = 20    3 * 10 = 30    4 * 10 = 40    5 * 10 = 50   
1 * 11 = 11    2 * 11 = 22    3 * 11 = 33    4 * 11 = 44    5 * 11 = 55   
1 * 12 = 12    2 * 12 = 24    3 * 12 = 36    4 * 12 = 48    5 * 12 = 60 
  • What do you need to add to the cout statement inside the nested for loop to get the above display? Hint: You are multiplying row and col together.
  • Note: Part of the output may get cut off if the user enters too large of a number or the numbers might not line up perfectly. Both are okay.
  • When you are finished, submit your assignment to Canvas

Assignment 13.2: Loopy Characters (10 pts)

  • Counting loops are very commonly used in programming to ... count things. Character data types are used to store and display individual letters and other symbols. In this project we explore both counting loops and characters in C++.
  • Copy and paste the starter code into a file called loopChars.cpp:

#include <iostream>
using namespace std;

int main() {
    int n; // the integer number
    char ch; // the single character
    cout << "** Loopy Characters!**\n\n";
    cout << "Enter an integer between 1 and 20: ";
    cin >> n;

    cout << "Enter a single character: ";
    cin>> ch;

    //Repeating the char n times with a for-loop.
    cout << "#1. Printing " << ch << " " << n << " times:\n";
    // Put your code here
    cout << endl << endl;

    // Printing starting with char and the following n ASCII chars.
    cout << "#2. Printing starting with " << ch << " and the following " << n - 1
         << " ASCII characters:\n";
    // Put your code here
    cout << endl << endl;

    // Repeating the char n times with stars on odd indexes.
    cout << "#3. Printing " << ch << " character " << n
         << " times substituting '*' on odd indexes:\n";
    // Put your code here
    cout << endl << endl;

    // Repeating the character n times with tick marks (+) every 5 chars
    cout << "#4. Printing " << ch << " character " << n
         << " times substituting (+) every fifth character:\n";
    // Put your code here
    cout << endl << endl;

    cout << "#5. Printing " << n << " lines of the previous loop:\n";
    // Hint: put your for-loop from the previous challenge inside another
    // for-loop that has a different counting variable.
    // Put your code here

    return 0;
}


  • User input is already coded into the worksheet, consisting of an integer number, n, and a single character.
  • Do not add any other input commands or change the input order.
  • (#1) Use a for-loop to print the character entered by the user the number of times specified by the integer number typed in. See the Example Run for an example.
  • (#2) Use a second for-loop to print the character entered by the user followed by the subsequent n - 1 characters in the ASCII table. See the Example Run for an example.
    • Hint: In this loop, we are adding one to the ASCII value of a character each time the for loop executes (add one more to the char's value each time the loop executes)
  • (#3) Use a third for-loop to print the character n / 2 times with a '*' instead of the character on even counts of the loop as shown in the Example Run.
    • Hint: Use and if-statement to test for even or odd counts of the loop.
  • (#4) Use a fourth for-loop to print the character n times with a tick mark '+' substituted every fifth character as shown in the Example Run.
    • Hint: Use and if-statement to test for the every fifth count of the loop. Start the for-loop count from 1, but check the end condition! Only print the number if nothing else is printed.
  • (#5) Use yet another loop to print the previous loop n times. Thus, this problem should use two loops.
  • Hint: put your for-loop from the previous challenge inside another for-loop that has a different counting variable. Print a newline character inside the outer loop after the inner loop completes.
  • Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.


**Loopy Characters!**

Enter an integer between 1 and 20: 10

Enter a single character: A

#1. Printing A 10 times:
AAAAAAAAAA

#2. Printing starting with A and the following 9 ASCII characters:
ABCDEFGHIJ

#3. Printing A character 10 times substituting '*' on even indexes:
A*A*A*A*A*

#4. Printing A character 10 times substituting (+) every fifth character:
AAAA+AAAA+

#5. Printing 10 lines of the previous loop:
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+

    

  • In the above example run, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  • After displaying the output, exit the program. Submit your file to Canvas when you are finished.