EECS168 09:Lab3
| Navigation | 
|---|
| Home | 
| Information | 
| Classwork | 
Objectives
- Build complex boolean statements.
 - Use nested loops.
 - To monitor disk usage.
 
Compound Boolean Expressions
The Modulo Operator
In C++, the modulo or mod operation is represented with a %. A short description of the mod operator is that it gives you the remainder of division; page 70 of your textbook explains the operator fully. Here, you will use it to determine if a number is a multiple of another number:
- 9 % 3 == 0 will be true, because 9 is a multiple of 3
 - 8 % 5 == 0 will be false, because 8 is not a multiple of 5
 
Determining Leap Years and Centennial Years in between the years 1500 and 2400
| Leap Year | Not Leap Year | 
|---|---|
| 1960 | 1643 | 
| 1600 | 1700 | 
| 2252 | 2200 | 
| 2400 | 1786 | 
A year that is evenly divisible by 4 is a leap year, unless it is a centennial year (a year that ends in 00, like 100, 200, etc.), in which case it is only a leap year if it is evenly divisible by 400. Copy the following code into a file named whatYear.cpp
#include <iostream>
using namespace std;
int main()
{
    int year;
    cout << "Enter a year: ";
    cin >> year;
    bool div_by_4 = (year % 4 == 0);
    bool div_by_100 = (year % 100 == 0);
    bool div_by_400 = (year % 400 == 0);
    if (/*BOOLEAN EXPRESSION*/)
    {
     if ( /* BOOLEAN EXPRESSION */ )
     {
        cout << "It is a leap year but not a centennial year." << endl;
     }
     else if ( /* BOOLEAN EXPRESSION */ )
     {
        cout << "It is a centennial year but not a leap year." << endl;
     }
     else if (/*BOOLEAN EXPRESSION*/)
     {
        cout << "It is a centennial year and a leap year." << endl;
     }
     else 
     {
        cout << "It is neither a centennial year nor a leap year." << endl;
     }
    }
    else
    {
      cout<<"The year is beyond the range of 1500-2400." << endl;
    }
    return 0;
}
Fill in the boolean expression needed for the program to work properly. Test your code with the examples in the table on the right.
Simple Loops
Below example code illustrates the working for a simple loop.
#include <iostream>
using namespace std;
int main()
{
    int count = 1;
    while (count <= 5)
    {
        cout << "The value of the count is:" << count << endl;
        count++;
    }
return 0;
}
This code will generate the following output:
- The value of the count is: 1
 - The value of the count is: 2
 - The value of the count is: 3
 - The value of the count is: 4
 - The value of the count is: 5
 
Nested Loops
We frequently use nested loops to handle tables. There is an outer loop that runs once for each row, and an inner loop than runs once for each value in the row. Here is an example of code with nested loops:
int row = 0;
while (row < 5)
{
    int column = 0;
    while (column < 10)
    {
        cout << column << " ";
        column++;
    }
    cout << endl;
    row++;
}
When run as part of a program, that code will generate this output:
- 0 1 2 3 4 5 6 7 8 9
 - 0 1 2 3 4 5 6 7 8 9
 - 0 1 2 3 4 5 6 7 8 9
 - 0 1 2 3 4 5 6 7 8 9
 - 0 1 2 3 4 5 6 7 8 9
 
First Table Program
Use loops to write a program printTable1.cpp that produces the following output:
- 0
 - 0 1
 - 0 1 2
 - 0 1 2 3
 - 0 1 2 3 4
 - 0 1 2 3 4 5
 - 0 1 2 3 4 5 6
 - 0 1 2 3 4 5 6 7
 - 0 1 2 3 4 5 6 7 8
 - 0 1 2 3 4 5 6 7 8 9
 - 0 1 2 3 4 5 6 7 8 9 10
 
Your program must use nested loops to receive credit. Notice that by using loops, you can change the size of the resulting pyramid by changing a single value in your code.
Second Table Program
First, create a file named printTable2.cpp. Copy the following code into printTable2.cpp to get started:
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
    // REPLACE THIS WITH YOUR CODE
    return 0;
}
This program should print out a table with k rows and m columns. Declare the variables “k” and “m” as constants. Take k = 6 and m = 5. Each row of the table contains partial sums of jth power of first n integers (1^j+2^j+3^j……..n^j); 1<=n<=m. Proceed this way to the other rows k>=j>=1. The table should be computing in the following way:
| 1k | 1k+2k | 1k+2k+3k | ... | 1k+2k+...+mk | 
| ... | ||||
| 13 | 13+23 | 13+23+33 | ... | 13+23+...+m3 | 
| 12 | 12+22 | 12+22+32 | ... | 12+22+...+m2 | 
| 11 | 11+21 | 11+21+31 | ... | 11+21+...+m1 | 
The Output should look like:
- 1 65 794 4890 20515
 - 1 33 276 1300 4425
 - 1 17 98 354 979
 - 1 9 36 100 225
 - 1 5 14 30 55
 - 1 3 6 10 15
 
There is a power function that you can use called pow:
int x = pow(2,3);
cout << x;
The code above would print "8"(i.e, 23)
Monitor Disk Usage
Follow the instructions on disk usage given here Monitor the disk usage of your EECS Account and include your total disk usage in your lab submission e-mail.
Lab Submission
Submit the following files (in a tarball):
- whatYear.cpp
 - printTable1.cpp
 - printTable2.cpp
 - Your tarball and the directory inside it should be named like Smith-1234567-Lab-03
 - The email subject should be "[EECS 168] Lab 03". Include your full name and student number in the email.
 - Include your Disk Usage in the email