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

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.
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
Write an application that uses multithreading to compute the sum of the integers in an array...
Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT