/** * 2D Vector Sum * CS 2240 * University of Vermont * Clayton Cafiero * 2020-Jun-26 */ #include #include #include "randi.h" #define SIZE 100 #define RANGE 10 /** * Calculate the sum of all elements in a 2D array * @param a std::vector> * @return int */ int twoDArraySum(std::vector> a) { int result = 0; for (int i = 0; i < a.size(); i++) { std::vector row = a[i]; for (int j = 0; j < a[i].size(); j++) { result = result + row[j]; } } return result; } int main() { std::vector> twoDArray; std::vector a = {1, 1, 1, 1}; twoDArray.push_back(a); twoDArray.push_back(a); twoDArray.push_back(a); twoDArray.push_back(a); int result = twoDArraySum(twoDArray); assert(result == 16); // Create a 2D vector of random integers // This will be a SIZE x SIZE vector // Integer values will be between -RANGE and RANGE for (int i = 0; i < SIZE; i++) { std::vector a; // populate array with SIZE random integers between -RANGE and RANGE randPopulateArray(a, SIZE, RANGE); twoDArray.push_back(a); } result = twoDArraySum(twoDArray); std::cout << result << std::endl; return 0; }