Home‎ > ‎CIS22ATTh‎ > ‎cis-22a-tth-schedule‎ > ‎

assignment13

Assignment 13
Due Thursday, November 10 at 3:20pm on Catalyst

Assignment 13.1: Fibonacci (10 pts)
  • Let's write a program to print out the first 14 Fibonacci numbers.
  • The Fibonacci Sequence is the series of numbers:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

    The next number is found by adding up the two numbers before it.

  • The 2 is found by adding the two numbers before it (1+1)
  • Similarly, the 3 is found by adding the two numbers before it (1+2),
  • And the 5 is (2+3),
  • and so on!
  • In other words, the rule for any number xn in the Fibonacci Sequence is this:  xn = xn-1 + xn-2
  • Above is from the Math is Fun website. For more information, check out their page on the Fibonacci Sequence.
  • The goal of our program is to write out the first 14 numbers of the Fibonacci Sequence.
  • We are going to use a for loop to accomplish our task.
  • Open up CodeBlocks and create a new C++ file called fibonacci.cpp.
  • Because each number in the Fibonacci Sequence is based on the previous two numbers, we need to keep track of both of the previous two numbers with a variable.
  • Declare two integer variables at the top of main like this:
int fib1 = 0;
int fib2 = 1;
  • We assign them the two initial values of the Fibonacci Sequence (0 and 1).
  • Now, print out a message to the user about the purpose of the program. The message should say this:
The first 14 Fibonacci numbers are:
  • Next, create a for loop to help us calculate and print the 14 Fibonacci numbers. When should your loop start and stop?
  • We want the loop to print each value of the Fibonacci sequence on the same line and followed by a space.
  • Inside your for loop, write a cout statement to print the first two values of the Fibonacci Sequence by printing the current values of your variables (0 and 1) with a blank space between the two numbers.
  • Now run your program. If you have been following the directions, your program should currently print the following:

  • If your program is printing more than 14 numbers, now is the time to adjust the starting and stopping conditions of your for loop.
  • Next, we need to calculate the Fibonacci Sequence by adding some statements to our for loop.
  • Below the cout statement in your for loop, add two statements to update the values of fib1 and fib2. I will give you the first one:
fib1 = fib1 + fib2;
fib2 = //You fill in the rest of the statement here;


  • When you are finished, your output should look identical to the sample output below.
  • Upload your fibonacci.cpp file to Catalyst


The output of your program should look identical to the sample output below:


Assignment 13.2: 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.
  • Open up CodeBlocks and create a new file called multiplication.cpp
  • Copy and paste the starter code into your program and fill in your names in the comments section:

/**
* Name 1
* Name 2
* 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 Catalyst.



Assignment 13.3: The Right Angle, Part 1 (10 pts)
  • We are going to get really loopy in this assignment.
  • When you have completed part 2 of this program (due on Monday November 14, you will have 6 loops total!
    • 2 NESTED for loops (4 loops total)
    • 1 do-while loop
    • 1 while loop
  • However, for now, you will simply write part of the program (2 nested loops + 1 while loop).
  • Open up CodeBlocks and create a new C++ file called triangle.cpp.
  • I recommend using the square printing example from class as your starter code.
  • Copy and paste it into your program and run it to remind yourself of how it works.
#include <iostream>
using namespace std;

int main() {
    cout << "I will print squares for you!\n";
    cout << "Enter the width of the square: ";
    int size;
    cin >> size;

    for (int row = 1; row <= size; row++)
    {
        for (int col = 1; col <= size; col++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}
  • Declare two variables at the top of your program. One should be an integer and one should be a string.
int base;
string repeat = "y";
  • Your goal is to create a program like the sample output located at the bottom of this page.
  • Your program will print out triangles for your user like in the sample output.
  • Your first step will be to alter the square example code so that you are printing out a triangle instead of a square.
  • The user will tell you how many stars will be in the base of your triangle. Store this input as the base variable.
  • Now, alter the nested for loops of the starter code to print a triangle.
  • Hint: One approach is to put an if statement in your inner-most for loop with a test condition for the col and row variables. However, there are several other solutions to this problem. Select the one that makes the most sense to you.
  • Once you have made this alteration, run your program again, and your output should look similar to the output below:

  • Make sure that the base of the triangle has the number of stars requested by your user by counting the number of stars that are printed to the screen.
  • If you do not have enough stars at the base, try adjusting the starting or stopping value of your loop. Remember that off-by-one errors are very common with loops.
  • Now you need a second, nested,  for loop to print out the triangle with its base on top and its point on bottom. 
  • Copy and paste your first nested for loop right below the first nested for loop.
  • Now, alter the copy of the nested for loop so that it prints out the inverted triangle as shown below.
  • Hint: You will need to alter the if statement of your second nested for loop so that it will print out a triangle that is upside down version of the first one.
  • When you are finished, your program should display pairs of triangles that look like the ones in the sample output below.
  • Next, add in a while loop that will allow the user to run the program again to display more triangles.
  • The user should be able to enter y to repeat the program.
  • Where should the {} of the while loop go? Hint: What code do you want repeated?
  • Finally, outside the while loop, add a cout statement to display the message:
Thank you!
And, remember, programming is easy if you approach it from the right angle.
  • Once your output looks identical to the output below, submit your program to Catalyst:


Comments