#include <iostream>usingnamespace std;
int main()
{
// Array declarationint blocks[7]; // locations numbered 0 to 6
blocks[3] = 4;
blocks[5] = blocks[3] + 8;
// A loop to initialize the array -- right to leftfor(int i = 6; i >= 0 ; i--) {
blocks[i] = i;
cout << "Setting blocks[" << i << "] to " << blocks[i] << "\n";
}
// Print contents of the array, separated by commas.//cout << blocks << "\n"; // This just prints the address (hexadecimal)for(int i = 0; i < 7; i++) {
cout << blocks[i];
if(i < 6) // not on last element
{
cout << ",";
}
}
cout << "\n";
// Let's try a different wayfor(int i = 0; i < 7; i++) {
if(i > 0) // not on first element
{
cout << ",";
}
cout << blocks[i];
}
cout << "\n";
// Array initializer syntax -- can specify values as part of // declaration.int numbers[4] = { 70, 60, 50, 40 };
for(int i = 0; i < 4; i++) {
if(i > 0) cout << ",";
cout << numbers[i];
}
cout << "\n";
// Error: Too many initializers:// int tooMany[4] = { 1, 2, 3, 4, 5, 6 };// Not enough initializers? The rest are set to zero.int notEnough[4] = { 1 , 2 };
for(int i = 0; i < 4; i++) {
if(i > 0) cout << ",";
cout << notEnough[i];
}
cout << "\n";
// Can initialize whole array to zero this way.int tally[100] = { 0 };
return0;
}
#include <iostream>usingnamespace std;
int main()
{
// Size of the array must be known at compile time.// Suppose I want to ask a user for the ages of his/her children.int numChildren = 50;
cout << "How many children? ";
cin >> numChildren;
constint MAX_CHILDREN = 10; // constantif(numChildren > MAX_CHILDREN) {
cout << "ERROR, only " << MAX_CHILDREN << " allowed.\n";
return0;
}
//int ages1[numChildren]; // not known at compile time, so some// compilers will flag an error here.int ages[MAX_CHILDREN]; // okay to use constant, because it's// known at compile-time.for(int i = 0; i < numChildren; i++) {
cout << "Age of child " << i << "? ";
cin >> ages[i];
}
return0;
}