DSA Lab Report 2
DSA Lab Report 2
LAB REPORT NO 2
CEN 2019
OBJECTIVE:
• To understand how to declare, initialize and access arrays
• To Implement Arrays in C++
Arrays
Arrays are like structures in that they both group a number of items into a larger unit. But
while a structure usually groups items of different types, an array groups items of the same
type. More importantly, the items in a structure are accessed by name, while those in an
array are accessed by an index number.
Using an index number to specify an item allows easy access to a large number of items.
Arrays exist in almost every computer language. Arrays in C++ are similar to those in other
languages, and identical to those in C. The use of arrays may allow us to simplify our
processing. We can use arrays to help read and analyze repetitive data with a minimum of
coding. An array and a loop can make the program smaller.
It is learned that Arrays contain a number of data items of the same type. This type can be a
simple data type, a structure, or a class. The items in an array are called elements In C++, an
array is a variable that can store multiple values of the same type. For example, suppose a
class has 27 students, and we need to store the grades of all of them. Instead of creating 27
separate variables, we can simply create an array: double grade[27];
Declaring Arrays
To declare an array in C++, the programmer specifies the type of the elements and the
number of elements required by an array as follows:
This is called a single-dimension array. The arraySize must be an integer constant greater
than zero and type can be any valid C++ data type. For example, to declare a 10element
2. double balance[10];
2
`
Lab Tasks:
Task 1:
Code
3
`
Console
4
`
Code
5
`
Console
Conclusion:
The first program demonstrates the creation and management of a student database using
C++ structures. It provides functionalities for adding students, displaying information for a
specific student, and calculating the average GPA of all students. The program incorporates
error handling to ensure valid user input and prevent unexpected behavior.
The second program showcases matrix multiplication in C++. It defines a function to multiply two
3x3 matrices and stores the result in a separate matrix. The program allows the user to enter the
elements of both matrices and displays the resulting product matrix.