Welcome to Our Last Class!

Announcements

  • Final Exam: Tuesday from 4:00pm to 6:00pm in this classroom!
    • No makeups - the exam must be taken on the day and at the time given
    • How to study: Old Quizzes, Midterms, Programs (In and Out of Class)
    • Final will be cumulative from first day of class through today's lesson
    • Will select one function from EACH of your fun with functions worksheet
    • Will select one function from your fun with arrays worksheet (today's activity 21.2)
    • Your program will be one of the two programs I have assigned for homework tonight.
    • Start studying now!
  • Review session from 2:45-3:45pm on Tuesday (December 12) in room ATC 312
  • Last lab due Friday at midnight
    • Arrays
  • Last homework due Saturday at midnight
    • One of these two programs will appear on your final exam!



Review Activity

1. With a partner, declare an array of doubles named weights and assign it the values 135.6, 150.9, 187.8, 205.9 in TWO WAYS (static and non-static initializaton)!

2. Write a for loop to print out the values in the weights array.


3. Fill in the missing lines of code below to read in the contents of essay.txt word-by-word using a loop:

int numWords = 0;
string word;

fin.open("essay.txt");

//code to check for failure

while(                        ) {  

    numWords++;

}

cout << "There are " << numWords << " words in your essay";


4. Fill in the missing lines of code below to read in the contents of essay.txt line-by-line using a loop:

int numLines = 0;

string line;

fin.open("essay.txt");

//code to check for failure

while(                        ) {  

    numLines++;

}

    cout << "There are " << numLines << " lines in your essay";

Arrays as Function Parameters

  • When writing a function with an array parameter, we place an empty [] after the parameter name:
    void print(int values[], int size);
    
  • We pass the size of the array to the function so the function knows the size of the array
    • There is no size() member function for an array
  • When we call the function, we do NOT include the []:
    print(data, size); // function call
    
  • Instead, we pass in the name of the array.
  • Unlike other parameters, you can pass the array into the function and then alter the array inside of the function without needing to return a new array.
  • For example, what do you think will be the result of running the following program?
#include <iostream>
using namespace std;
void fillArray(int data[], int size) {
    for (int i = 0; i < size; i++) {
       data[i] = i;
    }
} 
        int main() {
            const int SIZE = 5;
            int data[SIZE];
            fillArray(data, SIZE);
            for (int i = 0; i < SIZE; i++) {
                cout << data[i] << endl;
            }
        }
  • Note the use of size parameter
  • The programmer must keep track of the size when working with arrays
  • Note that arrays cannot be function return types
  • Returning an array with a return statement is not necessary


Using the const Modifier

  • Normally, a function can change the values of array elements
  • We can prevent the modification using the const modifier:
    void print(const int values[], int size);
    
  • The compiler will issue an error message if you try to change the value of an array element
  • If a function with a constant array parameter calls another function using the const array parameter as an argument, the called function must use a const array parameter as well
  • Otherwise, the compiler will issue an error
  • The following program shows arrays used with functions


Activity 21.1: Exploring Arrays (10 pts)
  • In this exercise we explore declaring, allocating and assigning values to arrays containing lists of data.
  • Create a source code file called myarrays.cpp:
  • Add the following function to the code:
    void print(const int values[], int size) {
        for (int i = 0; i < size; i++) {
            cout << values[i] << " ";
        }
        cout << endl;
    }
    
  • Compile your code to make sure it has correct syntax.

    If you have problems, ask a classmate or the instructor for help as needed.

  • Declare and initialize an array for a list of 10 integer scores after the current arrays using the following code:
    const int NUM_SCORES = 10;
    int scores[NUM_SCORES] = {90, 91, 92, 93, 94, 95, 96, 97, 98, 99};
    
  • After declaring and initializing the array, call the print() function using the code:
    cout << "Integer scores:\n";
    print(scores, NUM_SCORES);
  • Compile and run the program to make sure you made the changes correctly. When you run the program, the output should look like:
    Integer scores:
    90 91 92 93 94 95 96 97 98 99
    
  • Declare and initialize an array of double values holding the the temperature values 25.7, 30.3, and 40.9
  • Write another print() function with two parameters: one for the array of double values and one for the size of the array.
    • Note that you may also name this function print. The compiler will know they are different functions as their parameters are different.
    • This is called function overloading (two functions with the same name, but different parameters in the same program)
  • After declaring and initializing the array, call the print() function.
  • Compile and run the program to make sure you made the changes correctly. When you run the program, the output should look like:
    Integer scores:
    90 91 92 93 94 95 96 97 98 99
    Double temperatures:
    25.7 30.3 40.9
    
  • Declare and allocate an array of char values and assign it the vowels a, e, i, o and u.
  • After declaring and initializing the array, write another print function to display it. Again, this method should be named print(). Then, call this method in main.
  • Compile and run the program to make sure you made the changes correctly. When you run the program, the output should look like this:
    Integer exam scores:
    90 91 92 93 94 95 96 97 98 99
    Double temperatures:
    25.7 30.3 40.9
    Char vowels:
    a e i o u 
  • Finally write a function called addExtraCredit with the following signature:

void addExtraCredit(int scores[], int size){

    //fill in method body here

}

  • This method should add 5 points of extra credit to each value in the scores array.
    • Hint: Add 5 to each array element using a for loop to iterate through all values in the scores array.
  • Write the function and then call it on the scores array like so:

addExtraCredit(scores, NUM_SCORES);

  • Now print out the scores array by calling the print function to verify that the extra credit got added correctly.
  • Your output should now look like the following:
Integer exam scores:
90 91 92 93 94 95 96 97 98 99
Double temperatures
25.7 30.3 40.9
Char vowels:
a e i o u
Exams scores with extra credit
95 96 97 98 99 100 101 102 103 104

  • Submit your program to Canvas when you are finished.


Activity 21.2: Arrays and Functions Worksheet (10pts)

  • Copy and paste the starter code into a new file called funFunctions3.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 Canvas.
/**
 *
 * 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()
{
    int result;
    bool answer;
    string value;
   
    cout << boolalpha; //displays bools as true and false
   
    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;

}


Activity 21.3: Practice Quiz 8 (10 pts)
  • Complete this practice quiz and upload it to Canvas when you are finished.
  • Note that you will need to copy and paste it into a new document in order to be able to complete the quiz.

Upcoming Assignments
  • Assignment 21 due Saturday at midnight on Canvas
  • Lab 9 due Friday at midnight

~ Good Luck Studying! ~