Question

Write a C++ Function that adds up 4 diagonal values in a 2-D Array that are...

Write a C++ Function that adds up 4 diagonal values in a 2-D Array that are diagonal of each other

Homework Answers

Answer #1
#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;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ Function that finds the 4 values that are adjacent of one another (Vertical,...
Write a C++ Function that finds the 4 values that are adjacent of one another (Vertical, Diagonal, or Horizontal) in a 15x15 2-D Array that sums up to the largest value in the entire array and that returns/ prints out those 4 values and their sum.
(Javascript) Write a function that accepts an array. The function adds its value with the value...
(Javascript) Write a function that accepts an array. The function adds its value with the value of the character before to it to generate a number. It returns a string of numbers.
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
(a) Write a function in C++ called readNumbers() to read data into an array from a...
(a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of doubles The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows is exceeded. (b) Write a program with the...
C programing Write a function that takes an array of integers and applies another function (f(x)=...
C programing Write a function that takes an array of integers and applies another function (f(x)= 3x+4) on each element and then stores the result in another array and finally return it once it is done.
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of...
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of the matrix.
(C++) Write a function called triple that takes an array of integers as a parameter as...
(C++) Write a function called triple that takes an array of integers as a parameter as well as the length of the array. It should triple each value in the array. The function should not return anything. (Note that the contents of the array WILL be modified.)
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Write a function that receives an array of floats of 5 elements. The function (called sqrt_float)...
Write a function that receives an array of floats of 5 elements. The function (called sqrt_float) should return an array of floats that contains the square root of each value in the first array. If the value of the square root is less than 2, then replace it with the value 0. Print the new values back in the main function after calling the function.