Topic 10: One-Dimensional Arrays

In C++, arrays are a collection of variables of the same type stored in contiguous memory locations. A one-dimensional array is the simplest form of an array and is essentially a list of elements that can be accessed by their index.

Declaring and Initializing One-Dimensional Arrays

To declare an array, specify the type of its elements, followed by the array name and its size enclosed in square brackets. Here's an example:

Example: Declaring an Array
  • 
    int myArray[5];
                                        

You can also initialize an array at the time of declaration:

Example: Initializing an Array
  • 
    int myArray[5] = {1, 2, 3, 4, 5};
                                        

Accessing Array Elements

Array elements are accessed using their index, which starts from 0. For example, myArray[0] refers to the first element, myArray[1] to the second element, and so on.

Example: Accessing Array Elements
  • 
    #include <iostream>
    using namespace std;
    
    int main() {
        int myArray[5] = {1, 2, 3, 4, 5};
        cout << "The first element is: " << myArray[0] << endl;
        cout << "The third element is: " << myArray[2] << endl;
        return 0;
    }
                                        

Array Looping

To process all elements of an array, you can use a loop. The for loop is commonly used for this purpose:

Example: Looping Through an Array
  • 
    #include <iostream>
    using namespace std;
    
    int main() {
        int myArray[5] = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; ++i) {
            cout << "Element at index " << i << " is: " << myArray[i] << endl;
        }
        return 0;
    }
                                        

Modifying Array Elements

Array elements can be modified by assigning new values to them using their index:

Example: Modifying Array Elements
  • 
    #include <iostream>
    using namespace std;
    
    int main() {
        int myArray[5] = {1, 2, 3, 4, 5};
        myArray[2] = 10;  // Changing the third element to 10
        cout << "The new value of the third element is: " << myArray[2] << endl;
        return 0;
    }
                                        

Practice Questions

  1. How is memory allocated for a single-dimensional array in C++?
  2. What is the difference between declaring an array and initializing an array?
  3. What is the role of the size in an array declaration?
  4. What happens if you try to access an array element outside its defined range?
  5. What is the index of the first and last element of a single-dimensional array of size n?
  6. Can you change the size of an array once it has been declared in C++? Why or why not?
  7. What happens if you partially initialize an array in C++?
  8. How can you find the size of an array in C++?
  9. What are the limitations of single-dimensional arrays in C++?
  1. Write a C++ program that declares an array of 5 integers and calculates the sum of all elements in the array.
    • Declare an array with 5 integers.
    • Use a loop to input values into the array from the user.
    • Calculate the sum of the elements in the array.
    • Display the sum.
  2. Write a C++ program that finds the maximum value in an array of 10 integers entered by the user.
    • Declare an array of size 10.
    • Use a loop to input 10 integers into the array.
    • Traverse the array to find the maximum value.
    • Display the maximum value.
  3. Write a C++ program that takes an array of 7 integers and prints the array in reverse order.
    • Declare an array of size 7.
    • Input the elements of the array.
    • Use a loop to print the elements of the array in reverse order.
  4. Write a C++ program that counts how many even and odd numbers are present in an array of 8 integers entered by the user.
    • Declare an array of size 8.
    • Input the values into the array.
    • Use a loop to count how many numbers are even and how many are odd.
    • Display the count of even and odd numbers.
  5. Write a C++ program that copies elements from one array to another. The first array should have 5 integers, and the second array should hold the same elements as the first.
    • Declare two arrays of size 5.
    • Input values into the first array.
    • Copy the elements from the first array to the second array using a loop.
    • Display the elements of the second array.
  6. Write a C++ program that searches for a specific element entered by the user in an array of 6 integers. If the element is found, display its position; otherwise, display a message that the element is not found.
    • Declare an array of size 6.
    • Input values into the array.
    • Prompt the user to enter a number to search for.
    • Use a loop to search for the number in the array.
    • If found, display the index; if not, display a "not found" message.
  7. Write a C++ program that sorts an array of 5 integers in ascending order.
    • Declare an array of size 5.
    • Input values into the array.
    • Sort the array in ascending order using any sorting algorithm (e.g., bubble sort).
    • Display the sorted array.
    • eclare an array of size 4 with type float.
    • Input values into the array.
    • Calculate the average of the elements.
    • Display the average.
  8. Write a C++ program that removes duplicate elements from an array of 7 integers and displays the array with unique elements.
    • Declare an array of size 7.
    • Input values into the array.
    • Use logic to remove any duplicate elements.
    • Display the array with only unique elements.
    • Example: numbers entered is 1 2 2 3 4 4 5 then array with unique elements is 1 2 3 4 5
  9. Write a program to input the following values in an array named volts: 11.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, and 17.59.
    • After the data has been entered, have your program display the values.
    • Repeat above, but after the data has been entered, have your program display it in the following form:
      Book inventory
  10. Write a function that returns the index of the largest element in an array of integers. If there are more such elements than one, return the largest index. Use the following header: int indexOfLargestElement(double array[], int size)
    • Write a test program that prompts the user to enter 15 numbers
    • Invoke the function to return the index of the largest element
    • Displays the index
  11. Write two overloaded functions that return the average of elements in an array with the following headers:
    int average(int array[], int size);
    double average (double array[], int size);
    • Write a test program that prompts the user to enter 10 values
    • Invoke the functions
    • Display the average of these values.
Coming Soon...