Lab 3
Lab 3
Lvalues and rvalues are fundamental to C++ expressions. Put simply, an lvalue is an object
reference and an rvalue is a value. The difference between lvalues and rvalues plays a role in the
writing and understanding of expressions.
An lvalue is an expression that yields an object reference, such as a variable name, an array
subscript reference, a dereferenced pointer, or a function call that returns a reference. An lvalue
always has a defined region of storage, so you can take its address.
An rvalue is an expression that is not an lvalue. Examples of rvalues include literals, the results
of most operators, and function calls that return nonreferences. An rvalue does not necessarily
have any storage associated with it.
Strictly speaking, a function is an lvalue, but the only uses for it are to use it in calling the
function, or determining the function’s address. Most of the time, the term lvalue means object
lvalue, and this book follows that convention.
C++ borrows the term lvalue from C, where only an lvalue can be used on the left side of an
assignment statement. The term rvalue is a logical counterpart for an expression that can be used
only on the righthand side of an assignment. For example:
#define rvalue 42
int lvalue;
lvalue = rvalue;
An array in C++ is a group of identically typed elements that are kept in a single block of
memory. On the other hand, the jagged array is a sort of array where each row's number of
columns can vary. "Arrays of arrays" is another name for jagged arrays. We will look at
the definition, usage, and examples of jagged arrays in C++ in this post.
Each row of an array in a jagged array may contain a varied number of columns. Because it
comprises numerous arrays, each with a different number of elements, the jagged array is
sometimes known as an "array of arrays". The jagged array's elements are all arrays in and of
itself.
1. int jaggedArray[3][];
2. jaggedArray[0] = new int[2] {1, 2};
3. jaggedArray[1] = new int[3] {3, 4, 5};
4. jaggedArray[2] = new int[4] {6, 7, 8, 9};
In the last example, we generated a jagged array made up of three separate arrays, the first of
which contained two elements, the second three, and the third four.
A jagged array can be created in C++ by utilizing a two-dimensional array of pointers. The
array's elements are all pointers to arrays of numbers. Using the new keyword, we can
dynamically create memory for each row of the jagged array.
1. int main()
2. {
3. int **jaggedArray;
4. int rows = 3;
5. jaggedArray = new int *[rows];
6. jaggedArray[0] = new int[2] {1, 2};
7. jaggedArray[1] = new int[3] {3, 4, 5};
8. jaggedArray[2] = new int[4] {6, 7, 8, 9};
9.
10. // Accessing jagged array elements
11. cout<< "jaggedArray[0][0] = " <<jaggedArray[0][0] <<endl;
12. cout<< "jaggedArray[1][1] = " <<jaggedArray[1][1] <<endl;
13. cout<< "jaggedArray[2][2] = " <<jaggedArray[2][2] <<endl;
14.
15. // Deallocating memory
16. delete[] jaggedArray[0];
17. delete[] jaggedArray[1];
18. delete[] jaggedArray[2];
19. delete[] jaggedArray;
20.
21. return 0;
22. }
In the example above, we used a two-dimensional array of pointers to construct a jagged array.
Using the new keyword, we allocated memory for each row of the jagged array. The square
bracket notation has also been used to access the jagged array's components.
Using the delete keyword, we have finally dealtlocated the memory set aside for the jagged
array. To prevent memory leaks, it is crucial to deallocate the memory allocated for the jagged
array.
Example 1:
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int* jaggedArray[3]; // declare a jagged array containing 3 rows
7.
8. // allocate memory for the rows and assign their addresses to jaggedArray
9. jaggedArray[0] = new int[2] {1, 2};
10. jaggedArray[1] = new int[3] {3, 4, 5};
11. jaggedArray[2] = new int[4] {6, 7, 8, 9};
12.
13. // print the elements of the jagged array
14. for(int i = 0; i< 3; i++) {
15. for(int j = 0; j < i+2; j++) {
16. cout<<jaggedArray[i][j] << " ";
17. }
18. cout<<endl;
19. }
20.
21. // deallocate memory for the rows
22. for(int i = 0; i< 3; i++) {
23. delete[] jaggedArray[i];
24. }
25.
26. return 0;
27. }
Output:
12
345
6789
This result demonstrates that a jagged array in C++ was successfully generated and printed.
Explanation:
In this example, we've built a jagged array with three arrays, the first of which has two elements,
the second of which has three elements, and the third of which has four elements. For each row
of the jagged array, memory has been allocated using the new keyword.
After that, the elements of the jagged array were printed using a nested for loop. The inner
loop iterates through the columns of each row, while the outer loop iterates over the rows of the
jagged array. In order to limit the output of the inner loop to the number of elements in each row,
we utilized i+2 as the halting condition. In order to prevent memory leaks, we have
finally dealtlocated the RAM allotted for each row using the delete[] operator.
Example 2:
AD
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. void *jaggedArray[3];
7. int intArray[] = {1, 2};
8. char charArray[] = {'a', 'b', 'c'};
9. float floatArray[] = {1.1, 2.2, 3.3, 4.4};
10.
11. jaggedArray[0] = &intArray;
12. jaggedArray[1] = &charArray;
13. jaggedArray[2] = &floatArray;
14.
15. // Accessing jagged array elements
16. int *intPtr = static_cast<int*>(jaggedArray[0]);
17. cout<< "intArray[0] = " <<intPtr[0] <<endl;
18. cout<< "intArray[1] = " <<intPtr[1] <<endl;
19.
20. char *charPtr = static_cast<char*>(jaggedArray[1]);
21. cout<< "charArray[0] = " <<charPtr[0] <<endl;
22. cout<< "charArray[1] = " <<charPtr[1] <<endl;
23.
24. float *floatPtr = static_cast<float*>(jaggedArray[2]);
25. cout<< "floatArray[0] = " <<floatPtr[0] <<endl;
26. cout<< "floatArray[1] = " <<floatPtr[1] <<endl;
27.
28. return 0;
29. }
Output:
intArray[0] = 1
intArray[1] = 2
charArray[0] = a
charArray[1] = b
floatArray[0] = 1.1
floatArray[1] = 2.2
Explanation:
In the above example, we've made a jagged array that contains arrays of various data kinds. The
arrays are kept in the jagged array via a void* pointer. Each array has had memory allocated for
it, and the appropriate member of the jagged array has been given access to each array's address.
By casting the void* pointer to the proper data type, we can gain access to the jagged array's
components. For instance, using the square bracket syntax and casting the void* pointer to
an int* pointer, we may access the first element of the intArray. In order to demonstrate that the
program ran well, we have finally returned 0.
Informative links:
https://www.educative.io/answers/how-to-use-a-jagged-2d-dynamic-array-with-variable-
column-sizes
https://www.tutorialspoint.com/how-to-use-use-an-array-of-pointers-jagged-in-c-cplusplus
Exercises:
1. Question: Write a C++ program to create a jagged array to store the names of students in
different classes. Each class may have a different number of students. After creating the
array, print the names of students in each class.
2. Question: Create a C++ program that uses a jagged array to store the scores of multiple
students in different subjects. Calculate and display the average score for each student
and the average score for each subject.
3. Scenario: You are developing a seating arrangement program for a theater. Each row in
the theater has a different number of seats. How would you represent this using a jagged
array, and how would you find the total number of seats in the theater?
Question: How would you use a jagged array to represent the seating arrangement in the
theater, and what code would you write to calculate the total number of seats in the
theater?