Question

In: Computer Science

Assume an MxN array has been filled, write a function to calculate the sum of each...

Assume an MxN array has been filled, write a function to calculate the sum of each column which should be stored into a third 1D array of size N. code in C++

Solutions

Expert Solution

#include <iostream> 
using namespace std;
#define m 4
#define n 4
// give any value to m and n
void column_sum(int arr[m][n],int arr2[n]) 
{ 
  
    int i,j,sum = 0; 
    int k=0;
  
    cout << "\nFinding Sum of each column:\n\n"; 
  
     
    for (i = 0; i < m; ++i) { 
        for (j = 0; j < n; ++j) { 
  
           
            sum = sum + arr[j][i]; 
            
        } 
        arr2[k]=sum;
        k++;
        cout 
            << "Sum of the column "
            << i << " = " << sum 
            << endl; 
        
       
        sum = 0; 
    } 
    cout<<"The content of 1d array is:\n";
    for(i=0;i<n;i++){
        cout<< arr2[i] ;
    }
}
int main() 
{ 
    int i,j; 
    int arr[m][n]; 
    int arr2[n];
    int x = 1; 
    for (i = 0; i < m; i++) 
        for (j = 0; j < n; j++) 
            arr[i][j] = x++; 
  
    column_sum(arr,arr2); 
  
    return 0; 
} 

Related Solutions

Using C++: Write a function that recursively calculates the sum of an array. The function should...
Using C++: Write a function that recursively calculates the sum of an array. The function should have 2 parameters, the array and an integer showing the number of elements (assume they are all integers) in the array. The function will use a recursive call to sum the value of all elements. You must prompt for a series of integers to be entered in the array. As your program reads the numbers in, increment a count so it knows how many...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a procedure to reverse an array. Assume staring address has been passed in ESI, the...
Write a procedure to reverse an array. Assume staring address has been passed in ESI, the number of units in ECX, the unit size in EBX
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. Need it in 10 minutes, please.
c++ program to calculate the sum of the rows and the columns in a multidimensional array
c++ program to calculate the sum of the rows and the columns in a multidimensional array
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT