0% found this document useful (0 votes)
0 views

Exp_2_cs

The experiment focuses on understanding two-dimensional arrays in C++ by creating a program that inputs matrix elements, displays the matrix, and calculates the sum of its elements. It outlines the theory behind 2D arrays, the procedure for implementing the program, and the expected results. The program successfully demonstrates the handling of 2D arrays, allowing user interaction and validation of functionality.

Uploaded by

Aadarsh Raj1693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Exp_2_cs

The experiment focuses on understanding two-dimensional arrays in C++ by creating a program that inputs matrix elements, displays the matrix, and calculates the sum of its elements. It outlines the theory behind 2D arrays, the procedure for implementing the program, and the expected results. The program successfully demonstrates the handling of 2D arrays, allowing user interaction and validation of functionality.

Uploaded by

Aadarsh Raj1693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment-2: Understanding Two-Dimensional Arrays in C++

Aim of the Experiment:

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:

A two-dimensional array in C++ is a collection of elements organized in rows and columns,


forming a grid-like structure. Each element is accessed using two indices: one for the row and
one for the column. Two-dimensional arrays are widely used for storing tabular data or matrices
in computational problems.

1. The syntax for declaring a 2D array is: datatype array_name[rows][columns];.


2. Nested loops are used for traversing rows and columns.
3. Operations like summing elements involve iterating through each element of the matrix.

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:

You might also like