TU-Programmieren_2/exercise3/task1.main.cpp
2025-04-09 10:22:44 +02:00

39 lines
1.2 KiB
C++

/// @file
/// @brief Task1: "single-file" excutable C++ program
#include <iostream>
#include <vector>
/// @brief Calculate the sum of all integer values in a std::vector<std::vector<int>>
/// @param data A vector of vectors containing the values to be summed
/// @return Sum of all values in data
double sum(const std::vector<std::vector<int>>& data) {
double sum = 0;
for (const auto& row : data) {
for (const auto& value : row) {
sum += value;
}
}
return sum;
}
/// @brief main function (entry point for executable) conducting the following tasks in this order
/// - create and prepare a local variable of type std::vector<std::vector<int>>
/// holding 9 int values in this arrangement:
/// 1, 2, 3
/// 4, 5, 6
/// 7, 8, 9
/// - call your function 'sum' and provide the prepared variable as argument to the call
/// - capture the result of your function call in a local variable and print it to the console
int main() {
std::vector<std::vector<int>> data = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
double result = sum(data);
std::cout << "Sum: " << result << std::endl;
return 0;
}