Don’t forget the quiz!!! (see mine below)
What is an array?
An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
Which index would you use to access the 2nd variable in an array?
The index, or subscript within the subscript operator, would be 1.
What can you say about the size of an array?
The number of elements in an array, also known as array size, can be fixed or dynamically changed.
How can you initialize an array?
int prime[5]; // hold the first 5 prime numbers
prime[0] = 2;
prime[1] = 3;
prime[2] = 5;
prime[3] = 7;
prime[4] = 11;
or
int prime[5] = { 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array
or
// Initialize all elements to 0
int array[5] = { };
or
int prime[5] { 2, 3, 5, 7, 11 }; // use uniform initialization to initialize the fixed array (c++11)
or
int array[] = { 0, 1, 2, 3, 4 }; // let initializer list set length of the array
How can you get the size of an array in bytes?
int main()
{
int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << sizeof(array) << ‘\n’; // will print the size of the array in bytes
return 0;
}
How can you use the function sizeof in order to calculate the number of elements in an array?
int main()
{
int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << “The array has: " << sizeof(array) / sizeof(array[0]) << " elements\n”;
return 0;
}
What happens when you try to write outside the bounds of the array?
C++ does not do any checking to make sure that your indices are valid for the length of your array.
When writing outside the bounds of the array, value will be inserted into memory where the 6th element would have been had it existed.
When this happens, you will get undefined behavior… this could overwrite the value of another variable, or cause your program to crash.
Quiz
1) Declare an array to hold the high temperature (to the nearest tenth of a degree) for each day of a year (assume 365 days in a year). Initialize the array with a value of 0.0 for each day.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double temperature[365] = { 0.0 };
cout << setprecision(1) << fixed; //set the precision of cout so that 0.0 gets printed
cout << temperature[0] << endl;
return 0;
}
2) Set up an enum with the names of the following animals: chicken, dog, cat, elephant, duck, and snake. Put the enum in a namespace. Define an array with an element for each of these animals, and use an initializer list to initialize each element to hold the number of legs that animal has.
Write a main function that prints the number of legs an elephant has, using the enumerator.
#include <iostream>
#include <iomanip>
using namespace std;
namespace Animals
{
enum Animals
{
CHICKEN, // 0
DOG, // 1
CAT, // 2
ELEPHANT, // 3
DUCK, // 4
SNAKE, // 5
MAX_NUMBER_OF_ANIMALS // 6
};
}
int main()
{
int animalLegs[] = {Animals::CHICKEN, Animals::DOG, Animals::CAT, Animals::ELEPHANT, Animals::DUCK, Animals::SNAKE}; // initializer list
animalLegs[Animals::CHICKEN] = 2;
animalLegs[Animals::DOG] = 4;
animalLegs[Animals::CAT] = 4;
animalLegs[Animals::ELEPHANT] = 4;
animalLegs[Animals::DUCK] = 2;
animalLegs[Animals::SNAKE] = 0;
cout << animalLegs[Animals::ELEPHANT] << endl;
return 0;
}