Write a C++ Function that adds up 4 diagonal values in a 2-D Array that are diagonal of each other
#include<iostream> using namespace std; int add4Diagonal(int arr[][4]){ int sum = 0; for(int i = 0;i<4;i++){ sum += arr[i][i]; } return sum; } int main() { int arr[4][4] = {{1,2,3,4}, {5,6,7,8}, {9,0,1,2}, {3,4,5,6}}; cout<<"Sum of 4 diagonal values = "<<add4Diagonal(arr)<<endl; return 0; }
int add4Diagonal(int arr[][4]){ int sum = 0; for(int i = 0;i<4;i++){ sum += arr[i][i]; } return sum; }
Get Answers For Free
Most questions answered within 1 hours.