Sequences and Vectors in c++ language

Sequences and Vectors in c++ language

The variables we have used to this point can assume only one value at a time. As we have seen, we can use individual variables to create some interesting and useful programs; Consider which averages five numbers entered by the user.

#include <iostream>
int main() 
{
    double n1, n2, n3, n4, n5;
    std::cout << "Please enter five numbers: ";
    // Allow the user to enter in the five values.
    std::cin >> n1 >> n2 >> n3 >> n4 >> n5;
    std::cout << "The average of " << n1 << ", " << n2 << ", "
                    << n3 << ", " << n4 << ", " << n5 << " is "
                    << (n1 + n2 + n3 + n4 + n5)/5 << '\n';
}
//Output
Please enter five numbers: 9 3.5 0.2 100 15.3
The average of 9.0, 3.5, 0.2, 100.0, 15.3 is 25.6

Suppose the number of values to average must increase from five to 25. Averaging 1,000 numbers using this approach is impractical.

#include <iostream>
int main() 
{
    double sum = 0.0, num;
    const int NUMBER_OF_ENTRIES = 5;
    std::cout << "Please enter " << NUMBER_OF_ENTRIES << " numbers: ";
    for (int i = 0; i < NUMBER_OF_ENTRIES; i++) {
         std::cin >> num;
         sum += num;
       }
    std::cout << "The average of " << NUMBER_OF_ENTRIES << " values is "
             << sum/NUMBER_OF_ENTRIES << '\n';
}
//Output
Please enter 5 numbers: 9 3.5 0.2 100 15.3
The average of the 5 values is 25.6
  • The programmer may want to display the values in some more persistent way;
  • A more sophisticated program may need to process the values in a different way;

Vectors in C++ Language

A vector has a name, and we may access the values it contains via their position within the block of memory managed by the vector.

Declaring and Using Vectors

If you include the directive

using std::vector

may declare a vector object that can hold integers as simply as

std::vector<int> vec_a;

can declare a vector with a particular initial size as follows:

std::vector<int> vec_b(10);

may declare a vector object of a given size and specify the initial value of all of its elements

std::vector<int> vec_c(10, 8);

We may declare a vector and specify each and every element separately

std::vector<int> vec_d{10, 20, 30, 40, 50};
boxes represent indices, or positions, within the vector AndroWep-Tutorials
boxes represent indices, or positions, within the vector AndroWep-Tutorials
std::vector<int> list(3); // Declare list to be a vector of three ints
list[0] = 5 ; // Make the first element 5
list[1] = -3 ; // Make the second element -3
list[2] = 12 ; // Make the last element 12
std::cout << list[1] << '\n'; // Print the element at index 1

This code fragment shows how the square brackets allow us to access an individual element based on that element’s position within the vector. The number within the square brackets indicates the distance from the beginning of the vector.

std::vector<int> list;
std::vector<double> collection{ 1.0, 3.5, 0.5, 7.2 };
std::vector<char> letters{ 'a', 'b', 'c' };

Traversing a Vector

The action of moving through a vector visiting each element is known as traversal. for loops are ideal for vector traversals.

for (int i = 0; i < 10; i++)
std::cout << a[i] << '\n';

for (int i = 9; i >= 0; i--)
std::cout << a[i] << '\n';

std::vector<int> set(1000);
for (int i = 0; i < 1000; i++)
set[i] = i;
#include <iostream>

#include <vector>

int main() {
    double sum = 0.0;
    const int NUMBER_OF_ENTRIES = 5;
    std::vector < double > numbers(NUMBER_OF_ENTRIES);
    std::cout << "Please enter " << NUMBER_OF_ENTRIES << " numbers: ";
    // Allow the user to enter in the values.
    for (int i = 0; i < NUMBER_OF_ENTRIES; i++) {
        std::cin >> numbers[i];
        sum += numbers[i];
    }
    std::cout << "The average of ";
    for (int i = 0; i < NUMBER_OF_ENTRIES - 1; i++)
        std::cout << numbers[i] << ", ";
    // No comma following last element
    std::cout << numbers[NUMBER_OF_ENTRIES - 1] << " is " <<
        sum / NUMBER_OF_ENTRIES << '\n';
}
Please enter 5 numbers: 9 3.5 0.2 100 15.3
The average of 9.0, 3.5, 0.2, 100.0, 15.3 is 25.6

We need only change the definition of the NUMBER_OF_ENTRIES constant to allow the program to handle any number of values.

Vectors and Functions

#include <iostream>

#include <vector>

/*
 * print(v)
 * Prints the contents of an int vector
 * v is the vector to print
 */
void print(std::vector < int > v) {
    for (int elem: v)
        std::cout << elem << " ";
    std::cout << '\n';
}
/*
 * sum(v)
 * Adds up the contents of an int vector
 * v is the vector to sum
 * Returns the sum of all the elements
 * or zero if the vector is empty.
 */
int sum(std::vector < int > v) {
    int result = 0;
    for (int elem: v)
        result += elem;
    return result;
}
int main() {
    std::vector < int > list {
        2,
        4,
        6,
        8,
    };
    // Print the contents of the vector
    print(list);
    // Compute and display sum
    std::cout << sum(list) << '\n';
    // Zero out all the elements of list
    int n = list.size();
    for (int i = 0; i < n; i++)
        list[i] = 0;
    // Reprint the contents of the vector
    print(list);
    // Compute and display sum
    std::cout << sum(list) << '\n';
}
2 4 6 8
20
0 0 0 0
0

shows that a vector formal parameter is declared just like a non-vector formal parameter.