Full Transcript

# Lab 3: Arrays ## 1. Objectives The objectives of this lab are for students to: * Understand the array data structure, * Be able to use arrays in C++, * Understand the use of pointers in accessing array elements. ## 2. Array Basics ### Definition An array is a collection of elements of...

# Lab 3: Arrays ## 1. Objectives The objectives of this lab are for students to: * Understand the array data structure, * Be able to use arrays in C++, * Understand the use of pointers in accessing array elements. ## 2. Array Basics ### Definition An array is a collection of elements of the same type placed in contiguous memory locations. Arrays provide a convenient way to group variables of the same type. ### Declaration To declare an array in C++, you specify the type of the elements, the name of the array, and the number of elements in square brackets: ```cpp int numbers; // Declares an integer array of 5 elements ``` ### Initialization Arrays can be initialized when they are declared: ```cpp int numbers = {1, 2, 3, 4, 5}; ``` If the number of initializers is less than the number of elements, the remaining elements are initialized to zero. ### Accessing Elements Array elements are accessed using their index, starting from 0: ```cpp int firstNumber = numbers; // Accesses the first element numbers = 10; // Assigns 10 to the second element ``` ### Array Name as a Pointer The name of the array can be used as a pointer to the first element of the array. For example: ```cpp int numbers = {1, 2, 3, 4, 5}; int* ptr = numbers; // ptr points to the first element of numbers ``` You can then use pointer arithmetic to access array elements: ```cpp int secondNumber = *(ptr + 1); // Accesses the second element ``` ## 3. Exercises ### Exercise 1: Array Initialization and Access 1. Declare an integer array named `grades` with a size of 10. 2. Initialize the array with the following values: 85, 92, 78, 65, 88, 95, 76, 82, 89, 91. 3. Write a loop to print each element of the array along with its index. Expected output: ``` Grade at index 0: 85 Grade at index 1: 92 Grade at index 2: 78 Grade at index 3: 65 Grade at index 4: 88 Grade at index 5: 95 Grade at index 6: 76 Grade at index 7: 82 Grade at index 8: 89 Grade at index 9: 91 ``` ### Exercise 2: Calculate Average 1. Using the `grades` array from Exercise 1, calculate the average grade. 2. Print the calculated average with an appropriate message. Expected output: ``` Average grade: 84.1 ``` ### Exercise 3: Array and Pointers 1. Declare an integer array named `data` with a size of 5 and initialize it with any values. 2. Create a pointer that points to the first element of the `data` array. 3. Use pointer arithmetic to print each element of the array. Expected output (example): ``` Element 0: 10 Element 1: 20 Element 2: 30 Element 3: 40 Element 4: 50 ``` ### Exercise 4: Dynamic Array Allocation 1. Write a program that asks the user to enter the size of an array. 2. Dynamically allocate an array of integers with the specified size using the `new` operator. 3. Prompt the user to enter values for each element of the array. 4. Calculate the sum of all elements in the array. 5. Print the sum. 6. Deallocate the dynamically allocated memory using the `delete[]` operator. Expected output (example): ``` Enter the size of the array: 3 Enter element 0: 5 Enter element 1: 10 Enter element 2: 15 Sum: 30 ``` ## 4. Submission * Create a folder named `Lab3_YourName`. * Save all `.cpp` files in the folder. * Compress the folder into a `.zip` file. * Submit the `.zip` file on the course website.