-
5
pages
-
English
-
Documents
Description
ICS 3M0 Tutorial – Introduction to Arrays What is an Array? An array is a collection of data storage locations, each having the same data type and name. These data storage locations are called elements. Why You Need Arrays in Your Programs? Say you owned a business, and you were keeping track of your expenses for 2002 and filing your receipts by month. You could have a separate folder for each month but it would be much more convenient to have a single folder with 12 compartments. Now imagine you were making a program to keep track of business expenses. You could declare 12 separate variables for each month or you could declare one array with 12 elements or storage locations. Naming and Declaring Arrays To declare an array you must first specify the data type and then its name just like when declaring variables. The only difference is you must add the number of elements (storage locations) that the array will hold in square brackets after the name. This number is called the index number or the element number. You can use all of your C++ data types in your arrays. Here are some examples of declaring arrays: int Expenses[12]; char charArray[8]; double Earnings[]; In the first example we declare an integer array named Expenses with 12 elements or 12 storage locations in which we could use in our program to keep track of business expenses. In the second example we declare a character array named charArray with 8 ...
-
Publié par
-
Langue
English