Question: I need this converted to C++. I did not specify what I wanted the code to be written in before. I tried to converte it
I need this converted to C++. I did not specify what I wanted the code to be written in before. I tried to converte it myself but it wasn't compiling correctly.
ArraySum::java class ArraySum { static void main(std::vector &args);
//sums up all the numbers in 2D array and prints private: static void sum(std::vector> &numbers); }; Sample Output : 2 array_Renamed sum = 70
class RectangularVectors { public: static std::vector> ReturnRectangularIntVector(int size1, int size2) { std::vector> newVector(size1); for (int vector1 = 0; vector1 < size1; vector1++) { newVector[vector1] = std::vector(size2); }
return newVector; } };
//.cpp file code:
void ArraySum::main(std::vector &args) { //initialize col and row sizes int MAX_ROW = 3, MAX_COL = 5; //declare 2D array with the above sizes //ORIGINAL LINE: int numbers[][] = new int[MAX_ROW][MAX_COL]; std::vector> numbers = RectangularVectors::ReturnRectangularIntVector(MAX_ROW, MAX_COL); //assign the values to 2D array numbers[0] = std::vector{ 1, 2, 3, 4, 5 }; numbers[1] = std::vector{ 6, 7, 8, 9, 10 }; numbers[2] = std::vector{ 1, 2, 3, 4, 5 };
//ask the sum to print sum(numbers); }
void ArraySum::sum(std::vector> &numbers) { int sum = 0; for (int i = 0; i < numbers.size(); i++) { for (int j = 0; j < numbers[i].length; j++) { sum += numbers[i][j]; } } std::wcout << L"2D array sum = " << sum << std::endl; }
MAX_ROW = 3;
MAX_COL = 5;
Write a sum function that takes a two dimensional array with MAX_ROW and MAX_COL as its row/column sizes. Sum adds all the elements in the array and returns the sum.
Write a main function that initializes a two dimensional array int numbers[ MAX_ROW ] [ MAX_COL ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}. Then it calls the sum function to find the sum of all the numbers in the array. It prints the sum to the user.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
