0% found this document useful (0 votes)
11 views4 pages

Programming For Problem Solving: Experiment - 7

The document presents programming tasks related to 2-dimensional arrays in C++. It includes examples of output for a given code, programs to calculate the sum of each row and column, the sum of diagonal elements, and the transpose of a matrix. Each task is accompanied by sample code demonstrating the required functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Programming For Problem Solving: Experiment - 7

The document presents programming tasks related to 2-dimensional arrays in C++. It includes examples of output for a given code, programs to calculate the sum of each row and column, the sum of diagonal elements, and the transpose of a matrix. Each task is accompanied by sample code demonstrating the required functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAMMING FOR PROBLEM SOLVING

EXPERIMENT - 7
Task 1: What is the output of the following?
void main()
{
int a[2][3] = {1, 2, 3, 4, 5};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
cout<<a[i][j];
}
OUTPUT: 123450

Task 2: Write a program to take values for 2-dimensional array and to


give choice to the user for performing below operations:

1. Sum of elements of each row.


PROGRAM:
#include <iostream>
using namespace std;
#define m 4
#define n 4
void row_sum(int arr[m][n])
{
int i,j,sum = 0;
cout << "\nFinding Sum of each row:\n\n";
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[i][j];
}
cout
<< "Sum of the row "
<< i << " = " << sum
<< endl;
sum = 0;
}
}
void column_sum(int arr[m][n])
{
int i,j,sum = 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];
}
cout
<< "Sum of the column "
<< i << " = " << sum
<< endl;
sum = 0;
}
}
int main()
{
int i,j;
int arr[m][n];
int x = 1;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j] = x++;
row_sum(arr);
column_sum(arr);
return 0;
}

2. Sum of diagonal elements.


PROGRAM:
#include <iostream>
#define SIZE 5
using namespace std;

int main()
{
int matrix[SIZE][SIZE];
int sum_left =0, sum_right = 0;
cout << "Enter elements into the matrix \n";
for(int i=0; i<SIZE ; i++){
for(int j=0; j<SIZE; j++){
cin >> matrix[i][j];
if(i==j)
sum_left += matrix[i][j];
if((i+j) == SIZE-1)
sum_right += matrix[i][j];
}
}
cout << "Sum of Left Diagonal: "<< sum_left << endl;
cout << "Sum of Right Diagonal: "<< sum_right << endl;
return 0;
}

3. Finding transpose of matrix


PROGRAM:
#include <iostream>
using namespace std;

int main() {
int a[10][10], transpose[10][10], row, column, i, j;

cout << "Enter rows and columns of matrix: ";


cin >> row >> column;

cout << "\nEnter elements of matrix: " << endl;

// Storing matrix elements


for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}

// Printing the a matrix


cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];
if (j == column - 1)
cout << endl << endl;
}
}

// Computing transpose of the matrix


for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j) {
transpose[j][i] = a[i][j];
}

// Printing the transpose


cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < column; ++i)
for (int j = 0; j < row; ++j) {
cout << " " << transpose[i][j];
if (j == row - 1)
cout << endl << endl;
}

return 0;
}

You might also like