Exp_2_cs
Exp_2_cs
To learn how to work with two-dimensional arrays in C++ by writing a program to input
elements into a matrix, display the matrix, and calculate the sum of all its elements.
Theory:
Procedure:
1. Declare the array dimensions: Start by declaring integers rows and columns for the matrix
dimensions.
2. Prompt user input: Ask the user to enter the number of rows and columns for the matrix.
3. Initialize the 2D array: Define a two-dimensional array using the dimensions provided by the
user.
4. Input matrix elements:
o Use nested for loops to iterate through rows and columns.
o Take user input for each matrix element and store it in the appropriate position in the
array.
5. Display the matrix:
o Use nested for loops to traverse the matrix.
o Print each element in a grid format.
6. Calculate the sum:
o Initialize a variable sum to 0.
o Use nested loops to traverse all elements in the matrix, adding each to the sum variable.
7. Display the sum: Print the total sum of the elements.
8. Test the program: Run the program with different matrix dimensions and values to validate its
functionality.
9. Analyze the results: Verify the matrix display and sum calculation for correctness.
10. End the program: Ensure proper memory management and clean termination.
Result:
The program successfully implements the concept of two-dimensional arrays. It allows the user
to input elements into a matrix, displays the matrix in tabular form, and calculates the sum of its
elements.
The program demonstrates how to handle two-dimensional arrays in C++. For a matrix with
dimensions 2 × 3, the user can input values row by row. The program then displays the matrix in
a grid format and calculates the sum of all elements. For example, with input [10, 20, 30, 40,
50, 60], the matrix is displayed as: