Friday, December 15, 2023

A c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list.

Arrays are useful when you know the number of elements you need to store in advance and want to access them using an index. Since arrays are stored in contiguous memory, accessing elements by index is very fast. Arrays are also useful when you need to perform mathematical or statistical operations on the data, as there are many libraries and functions available for working with arrays.

On the other hand, lists are useful when you need to store a collection of elements that can grow or shrink dynamically. Since lists are stored in non-contiguous memory, adding or removing elements is fast. Lists are also useful when you need to insert or delete elements from the middle of the collection, as this can be done efficiently with a list.

Here are some specific scenarios where you might use an array or a list:

  • Use an array to store a fixed number of data items that you need to access quickly and efficiently, such as pixel data for an image or sensor data from a device.
  • Use a list to store a collection of items that can change in size, such as user input for a form or a list of contacts.
  • Use an array when you need to perform mathematical or statistical operations on the data, such as calculating the mean or standard deviation.
  • Use a list when you need to insert or delete elements from the middle of the collection, such as sorting a list of names or filtering a list of search results.
  • Use an array when you have a small amount of data that needs to be stored and accessed quickly, and a list when you have a large amount of data that needs to be stored and manipulated dynamically.

Overall, the choice between an array and a list depends on the specific requirements of your program. If you need a fixed-size collection of elements that you can access quickly by index, use an array. If you need a dynamic collection of elements that can be easily added or removed, use a list.

#include <iostream>
#include <array>
#include <list>

using namespace std;

int main() {
    // Declare an array of integers with 5 elements
    array arr = {1, 2, 3, 4, 5};
    
    // Declare a list of integers with the same elements
    list lst = {1, 2, 3, 4, 5};
    
    // Print the array
    cout << "Array: ";
    for (int i = 0; i < arr.size(); i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    
    // Print the list
    cout << "List: ";
    for (int n : lst) {
        cout << n << " ";
    }
    cout << endl;
    
    // Change the second element of the array to 10
    arr[1] = 10;
    
    // Change the second element of the list to 10
    auto it = lst.begin();
    advance(it, 1);
    *it = 10;
    
    // Print the modified array
    cout << "Modified Array: ";
    for (int i = 0; i < arr.size(); i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    
    // Print the modified list
    cout << "Modified List: ";
    for (int n : lst) {
        cout << n << " ";
    }
    cout << endl;
    
    return 0;
}


//===========
OUTPUT:
Array: 1 2 3 4 5 
List: 1 2 3 4 5 
Modified Array: 1 10 3 4 5 
Modified List: 1 10 3 4 5 

In this program, we declare an array arr and a list lst, both containing the same elements (1, 2, 3, 4, 5). We then print the elements of the array and list using loops. We then modify the second element of both the array and list to 10, and print them again.

The main difference between an array and a list is that an array has a fixed size and is stored in contiguous memory, while a list can grow and shrink dynamically and its elements are stored in non-contiguous memory. This means that accessing elements in an array is faster than accessing elements in a list, but adding or removing elements in a list is faster than in an array.

In the program, we can see that modifying an element in an array is done by simply indexing into it and changing the value, while modifying an element in a list requires finding the position of the element using an iterator and then changing its value.

No comments:

Post a Comment