Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++: Arrays as Function Arguments #201

Open Qingquan-Li opened 2 years ago

Qingquan-Li commented 2 years ago

Concept: To pass an array as an argument to a function, pass the name of the array.

// Function prototype:
return_type function_name(data_type[], int);
...

// Call the function:
function_name(array_name, SIZE);
...

return_type function_name(data_type array_name[], int size) {
    // body;
}

Example 1 of 2

// This program demonstrates that an array being passed to a function.

#include <iostream>
using namespace std;

// Function prototype
void showValue(int [], int);

int main() {
    const int ARRAY_SIZE = 8;
    int numbers[ARRAY_SIZE] = { 5, 10, 15, 20, 25, 30, 35, 40 };

    showValue(numbers, ARRAY_SIZE);

    return 0;
}

//*************************************************
// Definition of function showValue.              *
// This function accepts an array of integers and *
// the array's size as its arguments.             *
// The contents of the array are displayed.       *
//*************************************************

void showValue(int nums[], int size) {
    for (int index = 0; index < size; index++)
        cout << nums[index] << " ";
}

Output:

5 10 15 20 25 30 35 40

Using const Array Parameters:

You can prevent a function from making changes to an array argument by using the const key word in the parameter declaration. As a precaution, you should always use const array parameters in any function that is not intended to modify its array argument. That way, the function will fail to compile if you inadvertently write code in it that modifies the array.

...
// Function prototype
// void showValue(int [], int);
void showValue(const int [], int);
...
void showValue(const int nums[], int size) {
    for (int index = 0; index < size; index++)
        cout << nums[index] << " ";
}

Example 2 of 2

A professor gives four exams during the semester in her computer science class. At the end of the semester, she drops each student's lowest test score before averaging the scores. She has asked you to write a program that will read a students four test scores as input, and calculate the average with the lowest score dropped.

Here is the pseudocode algorithm that you developed:

Read the students four test scores.
Calculate the total of the scores.
Find the lowest score.
Subtract the lowest score from the total. This gives the adjusted total.
Divide the adjusted total by 3. This is the average.
Display the average.
// This program gets a series of test scores and
// calculates the average of the scores with the
// lowest score dropped.

#include <iostream>

using namespace std;

// Function prototypes
void get_test_scores(double[], int);
double get_total_score(const double[], int);
double get_lowest_score(const double[], int);
void print_average_score_before_dropping_lowest(const double[], int);

int main() {
    const int NUM_TESTS = 4;       // Array size
    double test_scores[NUM_TESTS]; // Array of test scores
    double total_score;
    double lowest_score;
    double average_score;

    // Get the test scores from the user input.
    get_test_scores(test_scores, NUM_TESTS);

    total_score = get_total_score(test_scores, NUM_TESTS);

    lowest_score = get_lowest_score(test_scores, NUM_TESTS);

    // Subtract the lowest score from the total score.
    total_score -= lowest_score;

    // Calculate the average. Divide by 3 because the lowest score was dropped.
    average_score = total_score / (NUM_TESTS - 1);

    cout << "The average with the lowest score dropped is "
         << average_score << endl;

    cout << "The average without the lowest score dropped is ";
    print_average_score_before_dropping_lowest(test_scores, NUM_TESTS);

    return 0;
}

void get_test_scores(double scores[], int num_tests) {
    for (int i = 0; i < num_tests; i++) {
        cout << "Please enter the score of test #"
             << (i + 1) << ": ";
        cin >> scores[i];
    }
}

double get_total_score(const double scores[], int num_tests) {
    double total = 0;
    for (int i = 0; i < num_tests; i++) {
        total += scores[i];
    }
    return total;
}

double get_lowest_score(const double scores[], int num_tests) {
    double lowest;
    lowest = scores[0];
    for (int i = 0; i < num_tests; i++) {
        if (lowest > scores[i]) {
            lowest = scores[i];
        }
    }
    return lowest;
}

// Print the average score before dropping the lowest score
void print_average_score_before_dropping_lowest(const double scores[], int num_tests) {
    double total = 0;
    for (int i = 0; i < num_tests; i++) {
        total += scores[i];
    }
    cout << total / num_tests;
}

Output:

Please enter the score of test #1: 100
Please enter the score of test #2: 92.5
Please enter the score of test #3: 88
Please enter the score of test #4: 96.5
The average with the lowest score dropped is 96.3333
The average without the lowest score dropped is 94.25