Assignment 20
Due Friday, December 9 at midnight on Catalyst


Assignment 20.1: Calculating My Grade (10 pts)

  • Make a copy of this Google worksheet
    • Either copy and paste into a new Google sheet
    • Or download the document as an Excel spreadsheet
  • Add up the number of points you have received in each category of this class
  • What is your current percentage in the course?
  • Now fill in the second half of the grade worksheet with your current point totals and enter an estimated grade for midterm 2.
  • What score do you need on the final exam to have (or keep) an A, B, and C in the class
    • Enter various scores in the final exam cell to see how these scores would affect your grade.
  • When you are finished, save the completed worksheet as either a pdf or an xls document
  • Upload it to Catalyst

Assignment 20.2: More Barnyard Animals (10 pts)

  • In this assignment we write a program that stores 5 strings in a string array, and 5 ints in an integer array and then writes the contents of the two arrays to the console.
  • Copy the following program into a C++ file, save it as animals2.cpp, and then compile and run the starter program to make sure you copied it correctly.
    #include <iostream>
    using namespace std;
    
    int main() {
        // Enter your code here
    
        return 0;
    }
    
  • At the top of main declare a string array named animals of size 5. 
  • Note that you should store the size of the array in a separate constant variable:
  • In other words, you should now have two variables like these at the top of main:

const int SIZE = 5;
string animals[SIZE];
  • Then, let's declare an integer array of size 5 named number_animals.
  • Store the following values in the string array, one at each index:
"sheep"
"donkeys"
"goats"
"geese"
"cats"
  • Remember: you can do this one of two ways:
Option 1: static initialization:

string animals[SIZE] = {"sheep", "donkeys", "goats", "geese", "cats"};

Option 2: Non-static initialization:

string animals[SIZE];
animals[0] = "sheep";
animals[1] = "donkeys";
//What goes here?
animals[4] = "cats"
  • Pick ONE of the above options to store the string values in your string array.
  • Next, pick one option to store the following values in the integer array (number_animals), one at each index:

5
3
8
2
4
  • We are now going to write the contents of our arrays to the console.
  • First, inside of main, add the following cout statement:

    cout << "Other animals at our farm" << endl;

  • Next, write a function that prints out the the two arrays.
  • Copy and paste the below comment and function prototype above main:
void printArrays(int array1[], string array2[], const int SIZE);

//Prints two arrays side-by-side to the console
  • Inside your function, you will need a for loop to print the arrays to the console, side-by-side, like this:
5 sheep
3 donkeys
8 goats
2 geese
4 cats
  • See the class notes if you get stuck for an example of using for loops with arrays.
  • Also see the class notes for examples of how to write a function to print the contents of an array.
  • Next, below your cout statement inside of main, call the printArrays function, passing it the two arrays and the size of the arrays.
  • Finally, compile and run your code and verify that the following output appears in the console window:

    Other animals at our farm
    5 sheep
    3 donkeys
    8 goats
    2 geese
    4 cats
  • Submit your program to Catalyst when you are finished


Assignment 20.3: Arrays and Functions Worksheet

  • Copy and paste the starter code into a new file called arrayFun.cpp
  • Write the required functions as described by the prototypes and comments.
  • The functions should be written below main.
  • Then, run the code when you are finished to see if you wrote the functions correctly.
  • Check the test results and make any alterations to your functions as necessary.
  • When all of the tests pass, upload your code to Catalyst.

/**
 *
 * CIS 22A
 */

#include <iostream>
using namespace std;

bool first10Last(int data[], int size);
//Given an array of ints, return true if 10 appears as
//either the first or last element in the array. The array will be size 1 or more.
//first10Last([1, 2, 10], 3) → true
//first10Last([10, 1, 2, 3], 4) → true
//first10Last([13, 10, 1, 2, 3], 5) → false

bool equalFirstLast(int array[], int size);
//Given an array of ints, return true if the array is size 1 or more,
//AND the first element and the last element are equal.
//equalFirstLast([1, 2, 3], 3) → false
//equalFirstLast([1, 2, 3, 1], 4) → true
//equalFirstLast([1, 2, 1], 3) → true

void printArray(int array[], int size);
//prints the contents of an array with a for loop
//see class notes for examples

void arrayPlus2(int array[], int size);
//Given an array of ints, add 2 to each element in the array
//return nothing. Remember arrays are automatically pass by reference
//arrayPlus2([1,2,3],3) -> [3, 4, 5]
//arrayPlus2([10, 30, 50, 79, 85], 5) -> [12, 32, 52, 81, 87]
//arrayPlus2([5], 1) -> [7]

void squareArray(int array[], int size);
//Given an array of ints, add multiplies each element in the array by itself
//return nothing. Remember arrays are automatically pass by reference
//squareArray([1,2,3],3) -> [1, 4, 9]
//squareArray([3, 5, 6, 8, 9], 5) -> [9, 25, 36, 64, 81]
//squareArray([5], 1) -> [25]


int main()
{

    bool answer;
    cout << boolalpha;

    cout << "***Testing first10Last***"<< endl << endl;
    int array1[] = {1, 2, 10};
    const int SIZE1 = 3;
    answer = first10Last(array1, SIZE1);
    cout << "Should print true: " << answer << endl;
    int array2[] = {10, 1, 2, 3};
    const int SIZE2 = 4;
    answer = first10Last(array2, SIZE2);
    cout << "Should print true: " << answer << endl;
    int array3[] = {13, 10, 1, 2, 3};
    const int SIZE3 = 5;
    answer = first10Last(array3, SIZE3);
    cout << "Should print false: " << answer << endl << endl;

    cout << "***Testing equalFirstLast***"<< endl << endl;
    int array4[] = {1, 2, 3};
    const int SIZE4 = 3;
    answer = equalFirstLast(array4, SIZE4);
    cout << "Should be false: " << answer << endl;
    int array5[] = {10, 20, 50, 60, 80, 90, 10};
    const int SIZE5 = 7;
    answer = equalFirstLast(array5, SIZE5);
    cout << "Should be true: " << answer << endl;
    int array6[] = {1};
    const int SIZE = 1;
    answer = equalFirstLast(array6, SIZE);
    cout << "Should be true: " << answer << endl << endl;

    cout << "***Testing arrayPlus2***" << endl << endl;
    int array7[] = {1, 2, 3};
    const int SIZE7 = 3;
    arrayPlus2(array7, SIZE7);
    cout << "Should print 3 4 5: " << endl;
    printArray(array7, SIZE7);
    cout << endl;
    int array8[] = {10, 30, 50, 79, 85};
    const int SIZE8 = 5;
    arrayPlus2(array8, SIZE8);
    cout << "Should print 12 32 52 81 87: " << endl;
    printArray(array8, SIZE8);
    cout << endl;
    int array9[] = {5};
    const int SIZE9 = 1;
    arrayPlus2(array9, SIZE9);
    cout << "Should print 7: " << endl;
    printArray(array9, SIZE9);
    cout << endl << endl;

    cout << "***Testing squareArray***" << endl << endl;
    int array10[] = {1, 2, 3};
    const int SIZE10 = 3;
    squareArray(array10, SIZE10);
    cout << "Should print 1 4 9: " << endl;
    printArray(array10, SIZE10);
    cout << endl;
    int array11[] = {3, 5, 6, 8, 9};
    const int SIZE11 = 5;
    squareArray(array11, SIZE11);
    cout << "Should print 9 25 36 64 81: " << endl;
    printArray(array11, SIZE11);
    cout << endl;
    int array12[] = {5};
    const int SIZE12 = 1;
    squareArray(array12, SIZE12);
    cout << "Should print 25: " << endl;
    printArray(array12, SIZE12);
    cout << endl << endl;

    cout << "***End of Tests***" << endl;

    return 0;

}

Assignment 20.4: Checking on the Drought
  • The California drought is a topic that is weighing heavily on the minds of many people in this state.
  • Let's examine some rainfall data for Cupertino to get a sense of how bad the drought is in our area.
  • For this assignment, you will be practicing arrays and File I/O.
  • First, open up Notepad or another basic text editor and create a new file called rainfallToDate.txt.
  • Then, copy and paste the following data into your file. Make sure that each value is on its own line and that there are no blank lines between values.
0.01
1.74
0.19
0.65
0.50
0.10
0.00
0.02
0.01
0.06
1.57
7.76

  • This data represents the monthly rainfall for Cupertino over the last year.
  •  Note that each data point is the total rainfall for one of the 12 months of the year. The first data point represents the total rainfall in January, 2015, and the last data point represents the total rainfall in December, 2015.
  • Save your file and then create a new text file called averageRainfall.txt.
  • Copy and paste the following data into this file. Again make sure that each data point is on its own line and that there are no blank lines between data points.
2.99
3.32
2.04
1.06
0.39
0.09
0.00
0.00
0.23
0.78
1.88
2.12

  • This data represents the average rainfall for Cupertino in inches.
  • Note that each data point is the average rainfall for one of the 12 months of the year.
  • The first data point represents the average rainfall in January, and the last data point represents the average rainfall in December.
  • Now, open CodeBlocks and create a new C++ source file named rainfall.cpp.
  • The first goal of our program is to read the input data from our two files and store the data in two separate arrays - one array for the 2015 rainfall data and one array for the average rainfall data.
  • What size should you make these arrays? Hint: How many months are there in the year? Don't forget to use a const int for the size!
  • Next Hint: What kind of data are your arrays going to hold?
  • After you have declared your arrays, you need to open each file and, using a loop, write the data into the slots of your array.
  • What kind of loop do you need here? Hint: Do you know in advance how many data points are in each file? Try a for loop.
  • You will need to open two different files and write the contents into two different arrays.
  • Therefore, you will need to close the stream for one file before you open the other file, like so:
fin.close();
fin.open("averageRainfall.txt");
  • Your program should write the results to a file in the form of a table that looks like this:


Rainfall for Cupertino: A Comparison

Month    Average    2015      Deficit

1        2.99      
0.01       2.98   
2        3.32      
1.74       1.58
3        2.04      
0.19       1.85
4        1.06      
0.65       0.41
5        0.39      
0.50       -0.11
6        0.09      
0.10       -0.01
7        0.00      
0.00       0.00
8        0.00      
0.02       -0.02
9        0.23      
0.01       0.22
10       0.78      
0.06       0.72
11       1.88      
1.57       0.31
12       2.12      
7.76       -5.64

  • To do so, you will need another loop to write the contents of your arrays to the file.
  • Note that you will need to adjust the number of tabs until your data is lined up properly as in the image above.
  • When your program creates an output file whose contents are identical to the image above, then submit your rainfall.cpp program to Catalyst.