Tutorial - Arrays
5 Pages
English

Tutorial - Arrays

-

Downloading requires you to have access to the YouScribe library
Learn all about the services we offer

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 ...

Subjects

Informations

Published by
Reads 18
Language English
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. Youcould declare 12 separate variables for each month or you could declare one array with 12elementsor 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 elements.The last example is a double type array named Earnings but it has no number in the square brackets, this means it is anunsizedarray. With unsized arrays, the size will be calculated as you insert values into it.
Initializing Arrays You can assign values to an array in many different ways.Here is an example: If I wanted an array to hold the values 1 to 10 I could do it several ways. Method 1 int Example1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Method 2 int Example2[10]; Example2[0] = 1; Example2[1] = 2; Example2[2] = 3; . . . Example2[8] = 9; Example2[9] = 10; Method 3 int Example3[10]; for (int i=0; i<10; i++) {  Example3[i]= i + 1; Method 4 int Example4[] = {1,2,3,4,5,6,7,8,9,10}; In the first example I declared the values 1 to 10 on the same line as the array declaration.In the second example I declared the array to have 10 elements and then assigned each value to an array index number separately on its own line.For the third example I set up a for loop to add 1 to the array element number each time as well as add 1 to the value being stored each time.The fourth method uses an unsized array.No number is specified so the size is calculated while the values are stored in it.In this example we store the numbers 1 through 10 in the unsized array.
About Array Elements You may have noticed in the second method when we stored the values 1 through 10 in the array the index numbers (numbers in square brackets) started at 0 and only went to 9 even though we set the array to 10.This is important to learn and understand.All arrays start at 0 and end at 1 less the number is was declared as.This doesn’t change the number of elements in the array.It just means that you need to remember that your arrays begin at 0. String Arrays Strings can also be used with arrays but we will only touch on them. In C++ there really are no strings, just arrays of characters.A String is anything that contains one or more characters strung together. To declare a String array of 50 characters we use the char data type. char string[50]; This would declare a string with the length of 50 characters.This works very similar String variables except you don’t need the String.h header. Youcan also use index numbers to print specific characters in the string. Sample Program #1 The following is a program to store expenses for a year by month. These sample programs have no practical use but serve as good examples to demonstrate the use of arrays. /* Program to demonstrate one-dimensional arrays.  ICS3M0 Tutorial - Introduction to Arrays  Expenses.cpp*/ #include <iostream.h> int main() {  /*Declare array to hold expenses */  floatexpenses[12];
 /*Input data from keyboard into array */  for(int count = 1; count < 13; count++) { cout <<"Enter expenses or month " << count << ": ";  cin>> expenses[count];  }  /*Print array contents */  for(count = 1; count < 13; count++) { cout << "Month " << count << " = $" << expenses[count] << endl;  }  return(0); } In the program we declare a float type array with 13 elements and set up two loops; one to get and store all the expenses from user and one to print all the stored expenses.In both loops we set up a counter and rather then individually writing the index numbers we made it expenses[count] because our loop was adding 1 each time and we were counting by 1 up to 10. Sample Program #2 This program demonstrates using character arrays.Remember these sample programs have no practical use but serve as good examples to demonstrate the use of arrays. /* Program to demonstrate using character arrays.  ICS3M0 Tutorial - Introduction to Arrays  charArrays.cpp*/ #include <iostream.h> int main() {  charcharArray[7] = { 'F','r','i','e','n','d','s' };  for(int i= 0; i<7; i++) { cout << "charArray["<<i<<"] has a value of " << charArray[i] << endl;  }
cout << "My Favourite comedy is " << charArray << endl;  return(0); } On the line 8 we declare a character array with 7 elements and assign them values on the same line.We then set up a loop to repeat a statement and print each array element on a line.At the end we have a statement followed by the array name which prints all of the stored information, and in our case, the word ‘Friends’.Notice that the index numbers began at 0 and ended at 6. Why Use Arrays Over Individual Variables? It is almost obvious why programmers would choose to use arrays rather then individual variables.With arrays, you can group like values with a single name.Say you needed a program to randomly generate 1000 different numbers.One can easily create an array to store 1000 random values but to declare 1000 variables and initialize each to a random number would be a tremendous amount of work. Arrays make tasks like these easier.