how to take the elements of an array from user in c++ code example

Example 1: take input from user in array c++

// declare and initialize an array without defining size
int x[] = {19, 10, 8, 17, 9, 15};

Example 2: c++ array programs

#include <iostream>
using namespace std;

int main() {
    
    // initialize an array without specifying size
    double numbers[] = {7, 5, 6, 12, 35, 27};

    double sum = 0;
    double count = 0;
    double average;

    cout << "The numbers are: ";

    //  print array elements
    // use of range-based for loop
    for (const double &n : numbers) {
        cout << n << "  ";

        //  calculate the sum
        sum += n;

        // count the no. of array elements
        ++count;
    }

    // print the sum
    cout << "\nTheir Sum = " << sum << endl;

    // find the average
    average = sum / count;
    cout << "Their Average = " << average << endl;

    return 0;
}

Tags:

Cpp Example