Functions

Working with functions

// Project 05-01
// Name: Product of three

#include <iostream>
using namespace std;

int multiply(int val1, int val2, int val3);

int main() {

    int v1 = 5;
    int v2 = 2;
    int v3 = 10;

    cout << "Solution is: " << multiply(v1,v2,v3) << endl;

    return 0;
}

int multiply(int val1, int val2, int val3) {
    return val1 * val2 * val3;
}
// Project 05-02
// Name: SumBuildIn

#include <iostream>

using namespace std;

int sumArray(int arr[], int size);

int main() {

    int myArray[] {
        2, 5, 2, 10, 20, 15, 100, 25
    };

    int total = sumArray(myArray, (sizeof(myArray)/sizeof(myArray[0])));
    // Debugging sizeof
    // cout << "size of array: " << (sizeof(myArray)/sizeof(myArray[0])) << endl;
    // End Debugging
    cout << "Sum of the elements in myArray is " << total << endl;

    return 0;
}

int sumArray(int arr[], int size) {
int total = 0;
for (int i = 0; i < size ; i++) {
    total += arr[i];
    }
    return total;
    }
// Project 05-03
// SumArrayObject

#include <iostream>
#include <array>

using namespace std;

int sumArray(array<int,10> myArray);

int main() {

    array<int, 10>myArray= { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

    int total = sumArray(myArray);

    cout << "The sum of the ArrayObject's members is: " << total << endl;

    return 0;
}

int sumArray(array<int, 10> myArray) {
    int total = 0;
    for (auto element : myArray) {
        total += element;
    }
    return total;
}
// Project 05-04
// SumArrayObject2

#include <iostream>
#include <array>

using namespace std;

int sumArray(array<int,10> myArray);
void sumArray(array<int,10> myArray, int& solution);

int main() {

    array<int, 10>myArray= { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

    int total = sumArray(myArray);
    
    int solution=0;
    sumArray(myArray, solution);

    cout << "Project 05-03: " << endl;
    cout << "The sum of the ArrayObject's members is: " << total << endl;

    cout << "Project 05-04: " << endl;
    cout << "The sum of the ArrayObject's members by reference is: " << solution << endl;

    return 0;
}

int sumArray(array<int, 10> myArray) {
    int total = 0;
    for (auto element : myArray) {
        total += element;
    }
    return total;
}

void sumArray(array<int, 10> myArray, int& solution) {
    solution = 0;
    for (auto element : myArray) {
        solution += element;
    }
}