how to print the elements of an array in c++ code example
Example 1: print an array c++
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
for (const int &n : numbers) {
cout << n << " ";
}
cout << "\nThe numbers are: ";
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}
Example 2: c++ array programs
#include <iostream>
using namespace std;
int main() {
double numbers[] = {7, 5, 6, 12, 35, 27};
double sum = 0;
double count = 0;
double average;
cout << "The numbers are: ";
for (const double &n : numbers) {
cout << n << " ";
sum += n;
++count;
}
cout << "\nTheir Sum = " << sum << endl;
average = sum / count;
cout << "Their Average = " << average << endl;
return 0;
}