You are on page 1of 6

# LAB

FEU-EAST ASIA COLLEGE
Information Technology Education Department

7

ITES103/ITEI103:Intro to Programming
SY 2013-2014(1st Term)

Lab 7: C++ Arrays
Objectives
This lab activity aims student to:

1.
2.
3.
4.
5.

Familiarized with array implementation in a C++ program.
Familiarized with the different applications of arrays in C++ programs .
Familiarized with the different types of arrays in C++
To code and execute C++ programs.
To debug a C++ program.

Task 1: Theoretical Framework

An array is a consecutive group of memory locations.
Each group is called an element of the array.
The contents of each element are of the same type.
 For example, could be an array of int, double, char, …
We can refer to individual elements by giving the position number (index) of the element in
the array.

C++ Arrays start at 0 !!!!!!!

The first element is the 0th element!
If you declare an array of n elements, the last one is number n-1.
If you try to access an element more than number n then it is an error!

Array Subscripts

The element numbers are called subscripts.
For example: In foo[i], foo is the name of the array, and “i” is the subscript.
A subscript can be any integer expression:
 These are all valid subscripts:

foo foo[i+3] foo[a+b+c]

3. The index for ar2 could have been omitted. i++ We can declare arrays of any data type and the size of an array is only limited by the available memory.04 }. 'S'. i++). double d = { 0. 4. i++).01. ar1[i] = i.707.03. The second for loop uses the comma operator: ar2[i] << endl. Remember that each element of an array is the same kind.02. //declare an int array of size 10 for (int i=0.8. eg ar2 contains 1. 4. i <= 99. in bytes. //initialize for (int i=0.01. } This example shows us quite a bit about arrays:      The array ar1 has 100 elements indexed from 0 to 99 Array ar2 has 4 elements and each element is initialized to contain some value. The size of ar2 would be determined at compile time by the number of initialization values. char s[] = { 'J'.i<10.01. for (i = 0. for (i = 0. of its argument. we could have written float ar2[] = { 1. The sizeof() function is a standard function which returns the size. 'C' }. int main() { int facs. } Initialization • You can initialize an array when you declare it (just like with variables): int foo = { 1. //size of array is 5. 0.FEU-EAC. . 2. return 0.707}. int main() { int ar1.3.i<10. cout << "size of ar2 " << sizeof(ar2)<< endl. cout << ar2[i] << endl. 3.i++) facs[i] = i. int i..04 }. ITE Department ITES103/ITEI103: Intro to Programming 2/6 SY 2013-2014 (1st Term) Lab 6: C++ While Loop Statements #include <iostream> using namespace std.03. i < 4.i++) cout << “facs[“ << i << “] is “ << facs[i] << endl. //size of array is automatically determined //from the size of elements #include <iostream> using namespace std. 0 to 99 float ar2 = { 1.12}.02. // 100 elements. 2.6.

x++) arr[x] = 0. arr 012 0524 1379 2618 You can initialize all the elements of an array to 0 using a loop instead of setting them each individually. int arr. x<3. x++) for (int y=0. int arr. There are 3 rows. = 1. = 4. = 8. = 5. for (int x=0. = 3. Here is a table that shows you what a 2D array looks like. y++) arr[x][y] = 3*x + y. Notice that this generates a table as follows: 0 1 2 0 0 1 2 Rows 1 3 4 5 2 6 7 8 Columns  A 2-dimensional array usually stored in computer memory as 1-dimensional. = 6. x<3. for (int x=0. = 9. each with three columns.  For example. = 7. the array above may be stored as row major 0 0 1 1 Row 0 2 2 3 3 4 4 Row 1 5 5 6 6 7 7 8 8 Row 2 Index Value . You must use 2 sets of square brackets when declaring a 2D array and when using it. arr arr arr arr arr arr arr arr arr //declare a 3 x 3 array. When you do this with a 2D array you need to use 2 loops. = 2. y<3.FEU-EAC. int arr. ITE Department ITES103/ITEI103: Intro to Programming 3/6 SY 2013-2014 (1st Term) Lab 6: C++ While Loop Statements Two-Dimensional Arrays A 2D array is an array that has both rows and columns.

Sample output: Enter 5 numbers: 99 5 34 19 44 The largest number is 99 at index 0 The smallest number is 5 at index 1 . print them out.FEU-EAC. For example. print out the first. Store all numbers in an array named FindMax[]. middle and the last elements. The output should look like this: Random numbers are: 45 23 20 47 98 76 75 22 99 The first element : 45 The middle element : 98 The last element : 99 Activity 3: Write a program that reads five numbers from the keyboard and print the largest and smallest numbers. int arr. we consider the first index to be row and second index to be column. ITE Department ITES103/ITEI103: Intro to Programming 4/6 SY 2013-2014 (1st Term) Lab 6: C++ While Loop Statements  In a 2-D array. User may enter 5 to 10 integers only. // 3 rows and 4 columns 0 1 2 3 0     1     2     Activity 1: Write a program that reads a series of numbers (5-10) from the keyboard and print them in reverse order. Sample output: How many would you like to enter? (5-10): 12 Invalid How many would you like to enter? (5-10): 5 Enter your numbers: 99 5 34 19 44 Your numbers reversed: 44 19 34 5 99 Activity 2: Write a program which will generate an array of 9 integers with random integer values between 20 and 99. Store all numbers in an array named Number[].

Store the result in Array C. Sample output: Array A: Enter a number for index: 2 Enter a number for index: 4 Enter a number for index: 6 Enter a number for index: 4 Array B: Enter a number for index: 4 Enter a number for index: 5 Enter a number for index: 8 Enter a number for index: 7 Choose option (1-Addition. 2-Subtraction): 1 Array A 2 4 6 4 Array B 4 5 8 7 Array C 6 9 14 11 Press any key to continue Activity 5: A bowling team consists of three players. Array B and Array C. Then compute and display the total score for each game. Assume that each bowler has the following score: Game 1 Game 2 Game 3 Bowler # 1: 286 252 265 Bowler # 2: 212 186 215 Bowler # 3: 252 232 216 Bowler # 4: 192 201 235 Bowler # 5: 186 236 272 Total 1128 1107 1203 . ITE Department ITES103/ITEI103: Intro to Programming 5/6 SY 2013-2014 (1st Term) Lab 6: C++ While Loop Statements Activity 4: Write a program which declares three arrays called Array A. All of the arrays contain 2 rows and 2 columns. Each player bowls three games. Perform matrix addition and subtraction of Array A and Array B. Write a program that uses 2D array to store the bowling scores.FEU-EAC.

8 151000 Camry 1. Vios or Altis) and the CC of the car (2.5) in order to get the price of the car. Example of output screen: WELCOME TO TOYOTA +++++++++++++++++ Model CC Price (Php) -------- ------------ ------------- Camry 2.8 116000 Altis 1.6 140000 Vios 2.6 79000 Altis 2. or 1.00 .8 84000 Vios 1. 1.6) :3 -----MODEL INFO----VIOS.0. models and CCs for Toyota’s car as listed below.6 100000 Choose MODEL (1:Camry 2:Vios 3:Altis) :2 Choose CC (1:2. 000.0 125000 Altis 1. 1.8 3:1.6 CC.0 2:1. The program will allow users to choose the model (Camry. Php 79.8. ITE Department ITES103/ITEI103: Intro to Programming 6/6 SY 2013-2014 (1st Term) Lab 6: C++ While Loop Statements Activity 5: Use arrays to hold the prices.FEU-EAC.0 171000 Camry 1.0 88000 Vios 1.