Question

In: Computer Science

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

Solutions

Expert Solution

#include<iostream>
using namespace std;
int main()
{
    int i, j, rows, columns, sum=0;
    cout<<"Enter the number of rows : ";
    cin>>rows;
    cout<<"Enter the number of columns : ";
    cin>>columns;
    int a[rows][columns],b[rows],c[columns];
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            cout<<"Enter the element in row "<<i+1<<" and column "<<j+1<<" : ";
            cin>>a[i][j];
        }
    }
    for(i=0;i<rows;i++)
    {
        sum=0;
        for(j=0;j<columns;j++)
        {
            sum=sum+a[i][j];
        }
        b[i]=sum;
    }
    for(i=0; i<rows; i++)
        {
                sum = 0;
                for(j= 0;j<columns; j++)
                {
                        sum = sum + a[j][i];
                }
                c[i]=sum;
        } 
        sum=0;
        for(i=0;i<rows;i++)
        {
            cout<<"\nThe sum of elements in row "<<i+1<<" : "<<b[i];
            sum=sum+b[i];
        }
        cout<<"\nThe sum of all row in the matrix is : "<<sum<<"\n";
        sum=0;
        for(i=0;i<columns;i++)
        {
            cout<<"\nThe sum of elements in column "<<i+1<<" : "<<c[i];
            sum=sum+c[i];
        }
        cout<<"\nThe sum of all columns in the matrix is : "<<sum;
}

The above program will take the input as number of row and number of columns and then it will asks for the user to the elements for each row and colums and then it calculate the sum of every row and store it in the array and then it will calculate the sum of columns and then store hem in an array and then it will print the sum of every row and also the sum of elements of all rows and the then prints the sum of each columns and then also prints the sum of all columns.

SCREENSHOT OF THE OUTPUT :


Related Solutions

Write a program that read an array consists of 4 rows and 4 columns, then computes...
Write a program that read an array consists of 4 rows and 4 columns, then computes and print the sum of the elements above and below the main diagonal. (WITH C++)
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then: fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1). compute the sum of elements at the second row. compute the sum of elements at the third column. compute...
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
A) Suppose owls is a MATLAB array with 251 rows and 51 columns representing the number...
A) Suppose owls is a MATLAB array with 251 rows and 51 columns representing the number of owls counted in the 251 counties in Texas had over the years 1960-2010. Write code to define a MATLAB variable to find the median number of owls counted in each county. B) Suppose cattle is a MATLAB array with 251 rows and 51 columns representing the number of cattle in the 251 counties in Texas had over the years 1950-2000. Write code to...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as collumns.
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and...
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and 30 columns. Row represents sections of a course and column represents the students, value inside each position of the array is the final exam grade for each students. Fill the array with random numbers between 40 and 100. Calculate the total, average, maximum, minimum for each section. Please do it simple. code
Given an m × n matrix (or 2-dimensional array) whose rows and columns are sorted, so...
Given an m × n matrix (or 2-dimensional array) whose rows and columns are sorted, so A[i][j]≤ A[i][j+1] and A[i][j]≤ A[i+1][j] Write an algorithm that searches for a specific value in the matrix.
"Create a program that displays a table consisting of four rows and five columns. The first...
"Create a program that displays a table consisting of four rows and five columns. The first column should display the numbers 1 through 4. The second and sub-sequent columns should display the result of multiplying the number in the first column by the numbers 2 through 5. If necessary, create a new project named Introductory14 Project, and save it in the Cpp8\Chap08 folder. Enter the C++ instructions into a source file named Introductory14.cpp. Also enter appropriate comments and any additional...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT