Answer the question
In order to leave comments, you need to log in
How to correctly pass a two-dimensional array to a function?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int size = 5;
int sum;
int prn(int sum) {
std::cout << " = " << sum << "\n";
}
void foo(int arr[][size]) {
int i = 0;
while(i < size) {
sum += arr[i][i];
i++;
}
std::cout << "array:";
prn(sum);
}
int main() {
srand(time(NULL));
std::cout << "Write the Length of the Array: ";
std::cin >> size;
int arr[size][size] = {};
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
arr[i][j] = rand() % 10;
}
}
foo(arr);
}
void foo(int arr[][5]) { ... }
int arr[5][5] = {};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question