ARRAYS Lecture 2. 2 Arrays Hold Multiple values Unlike regular variables, arrays can hold multiple...

70
ARRAYS Lecture 2

Transcript of ARRAYS Lecture 2. 2 Arrays Hold Multiple values Unlike regular variables, arrays can hold multiple...

Page 1: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

ARRAYS

Lecture 2

Page 2: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

2

Arrays Hold Multiple values

Unlike regular variables, arrays can hold multiple values.

Page 3: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

3

Figure

int count Enough memory for 1 int

12345

float price Enough memory for 1 float

56.981

char letter Enough memory for 1 char

A

Page 4: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

4

Figure

Page 5: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

5

Table

Array Declaration Number of Elements

Size of Each Element

Size of the Array

char letters[25]; 25 1 byte 25 bytes short rings[100]; 100 2 bytes 200 bytes int miles[84]; 84 4 bytes 336 bytes float temp[12]; 12 4 bytes 48 bytes doubledDistance[1000]; 1000 8 bytes 8000 bytes

Page 6: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

6

Accessing Array elements

The individual elements of an array are assigned unique subscripts. These subscripts are used to access the elements.

Page 7: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

7

Program

// This program asks the user for the number of hours worked// by 6 employees. It uses a 6-element int array to store the// values.

#include <iostream.h>

void main(void){

short hours[6];

cout << "Enter the hours worked by six employees: ";cin >> hours[0];cin >> hours[1];cin >> hours[2];cin >> hours[3];

Page 8: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

8

Program continues

cin >> hours[4];cin >> hours[5];cout << "The hours you entered are:";cout << " " << hours[0];cout << " " << hours[1];cout << " " << hours[2];cout << " " << hours[3];cout << " " << hours[4];cout << " " << hours[5] << endl;

}

Page 9: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

9

Program Output with Example Input

Enter the hours worked by six employees: 20 12 40 30 30 15 [Enter]

The hours you entered are: 20 12 40 30 30 15

Page 10: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

10

Figure

Page 11: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

11

Program

// This program asks the user for the number of hours worked// by 6 employees. It uses a 6-element short array to store the// values. #include <iostream.h>

void main(void){

short hours[6];

cout << "Enter the hours worked by six employees: ";for (int count = 0; count < 6; count++)

cin >> hours[count];cout << "The hours you entered are:";for (count = 0; count < 6; count++)

cout << " " << hours[count];cout << endl;

}

Page 12: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

12

Program Output with Example Input

Enter the hours worked by six employees: 20 12 40 30 30 15 [Enter]

The hours you entered are: 20 12 40 30 30 15

Page 13: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

13

Program

// This program asks the user for the number of hours worked// by 6 employees. It uses a 6-element short array to store the// values.#include<iostream.h>

void main(void){

short hours[6];

cout << "Enter the hours worked by six employees.\n";for (int count = 1; count <= 6; count++){

cout << "Employee " << count << ": ";cin >> hours[count - 1];

}cout << "The hours you entered are\n";

Page 14: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

14

Program continues

for (count = 1; count <= 6; count++){

cout << "Employee " << count << ": ";cout << hours[count - 1] << endl;

}

}

Page 15: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

15

Program Output with Example Input

Enter the hours worked by six employees.Employee 1: 20 [Enter]Employee 2: 12 [Enter]Employee 3: 40 [Enter]Employee 4: 30 [Enter]Employee 5: 30 [Enter]Employee 6: 15 [Enter]The hours you entered areEmployee 1: 20Employee 2: 12Employee 3: 40Employee 4: 30Employee 5: 30

Employee 6: 15

Page 16: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

16

Array Initialization

Arrays may be initialized when they are declared.

Page 17: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

17

Program

// This program displays the number of days in each month.// It uses a 12-element int array.

#include <iostream.h>

void main(void){

int days[12];days[0] = 31; // Januarydays[1] = 28; // Februarydays[2] = 31; // Marchdays[3] = 30; // Aprildays[4] = 31; // Maydays[5] = 30; // Junedays[6] = 31; // July

Page 18: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

18

Program continues

days[7] = 31; // Augustdays[8] = 30; // Septemberdays[9] = 31; // Octoberdays[10] = 30; // Novemberdays[11] = 31; // Decemberfor (int count = 0; count < 12; count++){

cout << "Month " << (count + 1) << " has ";cout << days[count] << " days.\n";

}

}

Page 19: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

19

Program Output

Month 1 has 31 days.Month 2 has 28 days.Month 3 has 31 days.Month 4 has 30 days.Month 5 has 31 days.Month 6 has 30 days.Month 7 has 31 days.Month 8 has 31 days.Month 9 has 30 days.Month 10 has 31 days.Month 11 has 30 days.

Month 12 has 31 days.

Page 20: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

20

Program

// This program displays the number of days in each month.// It uses a 12-element int array.#include <iostream.h>

void main(void){

int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};for (int count = 0; count < 12; count++){

cout << "Month " << (count + 1) << " has ";cout << days[count] << " days.\n";

}}

Page 21: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

21

Program Output

Month 1 has 31 days.Month 2 has 28 days.Month 3 has 31 days.Month 4 has 30 days.Month 5 has 31 days.Month 6 has 30 days.Month 7 has 31 days.Month 8 has 31 days.Month 9 has 30 days.Month 10 has 31 days.Month 11 has 30 days.

Month 12 has 31 days.

Page 22: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

22

Program

// This program uses an array of ten characters to store the// first ten letters of the alphabet. The ASCII codes of the// characters are displayed.#include <iostream.h>

void main(void){

char letters[10] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};

cout << "Character" << "\t" << "ASCII Code\n";cout << "--------" << "\t" << "----------\n";for (int count = 0; count < 10; count++){

cout << letters[count] << "\t\t";cout << int(letters[count]) << endl;

}}

Page 23: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

23

Program Output

Character ASCII Code--------- ----------

A 65B 66C 67D 68E 69F 70G 71H 72I 73

J 74

Page 24: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

24

Partial Array Initialization

When an array is being initialized, C++ does not require a value for every element.

int numbers[7] = {1, 2, 4, 8};

Page 25: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

25

Program

// This program has a partially initialized array.

#include <iostream.h>

void main(void){

int numbers[7] = {1, 2, 4, 8}; // Initialize the // first 4 elements.

cout << "Here are the contents of the array:\n";for (int index = 0; index < 7; index++)

cout << numbers[index] << endl;}

Page 26: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

26

Program Output

Here are the contents of the array:124800

0

Page 27: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

27

Implicit Array Sizing

It is possible to declare an array without specifying its size, as long as you provide an initialization list.

float ratings[] = {1.0, 1.5, 2.0, 2.5, 3.0};

Page 28: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

28

Initializing With Strings

When initializing a character array with a string, simply enclose the string in quotation marks:

char name[] = “Warren”;

Page 29: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

29

Figure

Page 30: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

30

Program

// This program displays the contents of two char arrays.#include <iostream.h>

void main(void){

char name1[] = "Holly";char name2[] = {'W', 'a', 'r', 'r', 'e', 'n', '\0'};

cout << name1 << endl;cout << name2 << endl;

}

Page 31: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

31

Program Output

Holly

Warren

Page 32: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

32

Processing Array Contents

Individual array elements are processed like any other type of variable.

Page 33: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

33

Program

// This program stores, in an array, the hours worked by 5// employees who all make the same hourly wage.

#include <iostream.h>

void main(void){

int hours[5];float payRate;

cout << "Enter the hours worked by 5 employees who all\n";cout << "earn the same hourly rate.\n";for (int index = 0; index < 5; index++){

cout << "Employee #" << (index + 1) << ": ";cin >> hours[index];

}

Page 34: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

34

Program continues

cout << "Enter the hourly pay rate for all the employees: ";cin >> payRate;cout << "Here is the gross pay for each employee:\n";cout.precision(2);cout.setf(ios::fixed | ios::showpoint);for (index = 0; index < 5; index++){

float grossPay = hours[index] * payRate;cout << "Employee #" << (index + 1);cout << ": $" << grossPay << endl;

}}

Page 35: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

35

Program Output with Example Input

Enter the hours worked by 5 employees who allearn the same hourly rate.Employee #1: 5 [Enter]Employee #2: 10 [Enter]Employee #3: 15 [Enter]Employee #4: 20 [Enter]Employee #5: 40 [Enter]Enter the hourly pay rate for all the employees: 12.75

[Enter]Here is the gross pay for each employee:Employee #1: $63.75Employee #2: $127.50Employee #3: $191.25Employee #4: $255.00

Employee #5: $510.00

Page 36: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

36

Program

// This program stores, in an array, the hours worked by 5// employees who all make the same hourly wage. It then// displays the gross pay, including any overtime.

#include <iostream.h>

// Constant for defining the array size

void main(void){

int hours[5];float payRate;

cout << "Enter the hours worked by 5 employees who all\n";cout << "earn the same hourly rate.\n";for (int index = 0; index < 5; index++){

cout << "Employee #" << (index + 1) << ": ";cin >> hours[index];

}

Page 37: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

37

Program continues

cout << "Enter the hourly pay rate for all the employees: ";cin >> payRate;cout << "Here is the gross pay for each employee:\n";cout.precision(2);cout.setf(ios::fixed | ios::showpoint);for (index = 0; index < 5; index++){

float grossPay, overTime;if (hours[index] > 40){

// Calculate pay for 40 hours.grossPay = 40 * payRate; // Calculate overtime pay.overTime = (hours[index] - 40) * 1.5 * payRate;// Add regular pay and overtime pay.grossPay += overTime;

}

Page 38: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

38

Program continues

elsegrossPay = hours[index] * payRate;

cout << "Employee #" << (index + 1);cout << ": $" << grossPay << endl;

}}

Page 39: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

39

Program Output with Example InputEnter the hours worked by 5 employees who allearn the same hourly rate.Employee #1: 10 [Enter]Employee #2: 20 [Enter]Employee #3: 50 [Enter]Employee #4: 40 [Enter]Employee #5: 60 [Enter]

Enter the hourly pay rate for all the employees: 12.75 [Enter]Here is the gross pay for each employee:Employee #1: $127.50Employee #2: $255.00Employee #3: $701.25Employee #4: $510.00Employee #5: $892.50

Page 40: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

40

Program

// This program stores, in two arrays, the hours worked by 5

// employees, and their hourly pay rates.

#include <iostream.h>

// Constant for defining the array size

const int numEmps = 5;

void main(void)

{

int hours[numEmps];

float payRate[numEmps];

cout << "Enter the hours worked by “ << numEmps

<< “ employees and their\n";

cout << "hourly rates.\n";

for (int index = 0; index < numEmps; index++)

{

cout << "hours worked by employee #" << (index + 1);

cout << ": ";

Page 41: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

41

Program continues

cin >> hours[index];cout << "Hourly pay rate for employee #";cout << (index + 1) << ": ";cin >> payRate[index];

}cout << "Here is the gross pay for each employee:\n";cout.precision(2);cout.setf(ios::fixed | ios::showpoint);for (index = 0; index < numEmps; index++){

float grossPay = hours[index] * payRate[index];cout << "Employee #" << (index + 1);cout << ": $" << grossPay << endl;

}}

Page 42: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

42

Program Output with Example Input

Enter the hours worked by 5 employees and their hourly rates.hours worked by employee #1: 10 [Enter]Hourly pay rate for employee #1: 9.75 [Enter]hours worked by employee #2: 15 [Enter]Hourly pay rate for employee #2: 8.62 [Enter]hours worked by employee #3: 20 [Enter]Hourly pay rate for employee #3: 10.50 [Enter]hours worked by employee #4: 40 [Enter]Hourly pay rate for employee #4: 18.75 [Enter]hours worked by employee #5: 40 [Enter]Hourly pay rate for employee #5: 15.65 [Enter]

Here is the gross pay for each employee:Employee #1: $97.50Employee #2: $129.30Employee #3: $210.00

Page 43: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

43

Thou Shalt Not Assign

You cannot use the assignment operator to copy one array’s contents to another.

for (int count=0; count < 4; count++)

newVal[count] = oldVal[count];

Page 44: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

44

Table

Expression Value

O ld V alues[0 ] 10 (Contents of Element 0 of O ld V alues)

O ld V alues[1 ] 100 (Contents of Element 1 of O ld V alues)

O ld V alues[2 ] 200 (Contents of Element 2 of O ld V alues)

O ld V alues[3 ] 300 (Contents of Element 3 of O ld V alues)

N ew V alues 8012 (Memory Address of N ew V alues)

O ld V alues 8024 (Memory Address of O ld V alues)

Page 45: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

45

Printing the Contents of an Array

To display the contents of an array, you must use a loop to display the contents of each element.

int array[5] = { 10, 20, 30, 40, 50 };for (int count = 0; count < 5; count++)

cout << array[count] << endl;

Page 46: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

46

Arrays As Function Arguments

To pass an array as an argument to a function, pass the name of the array.

Page 47: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

47

Program

// This program demonstrates that an array element is passed// to a function like any other variable.#include <iostream.h>

void ShowValue(int); // Function prototype

void main(void){

int collection[8] = {5, 10, 15, 20, 25, 30, 35, 40};

for (int Cycle = 0; Cycle < 8; Cycle++)ShowValue(collection[Cycle]);

}

Page 48: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

48

Program continues

//************************************// Definition of function showValue. *// This function accepts an integer argument. *// The value of the argument is displayed. *//************************************void ShowValue(int Num){

cout << Num << " ";

}

Page 49: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

49

Program Output

5 10 15 20 25 30 35 40

Page 50: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

50

Program

// This program demonstrates an array being passed to a function.

#include <iostream.h>

void showValues(int []); // Function prototype

void main(void)

{

int collection[8] = {5, 10, 15, 20, 25, 30, 35, 40};

showValues(collection); // Passing address of array collection

}

//***********************************************

// Definition of function showValues. *

// This function accepts an array of 8 integers *

// as its argument. The contents of the array *

// is displayed. *

//***********************************************

void showValues(int nums[])

{

for (int index = 0; index < 8; index++)

cout << nums[index] << " ";

}

Page 51: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

51

Program Output

5 10 15 20 25 30 35 40

Page 52: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

52

Program

// This program demonstrates an array being passed to a function.

#include <iostream.h>

void showValues(int []); // Function prototype

void main(void)

{

int set1[8] = {5, 10, 15, 20, 25, 30, 35, 40};

int set2[8] = {2, 4, 6, 8, 10, 12, 14, 16};

showValues(set1);

cout << endl;

showValues(set2);

}

//***********************************************

// Definition of function showValues. *

// This function accepts an array of 8 integers *

// as its argument. The contents of the array *

// is displayed. *

//***********************************************

void showValues(int nums[])

{

for (int index = 0; index < 8; index++)

cout << nums[index] << " ";

}

Page 53: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

53

Program Output

5 10 15 20 25 30 35 40

2 4 6 8 10 12 14 16

Page 54: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

54

Program

// This program uses a function that can display the contents// of an integer array of any size.#include <iostream.h>

void showValues(int [], int); // Function prototype

void main(void){

int set1[8] = {5, 10, 15, 20, 25, 30, 35, 40};int set2[4] = {2, 4, 6, 8};int set3[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

showValues(set1, 8);cout << endl;showValues(set2, 4);cout << endl;showValues(set3, 12);

}

Page 55: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

55

Program continues

//***********************************************// Definition of function showValues. *// This function displays the contents of the *// array passed into nums. The value passed *// into elements is the number of elements in *// the nums array. *//***********************************************

void showValues(int nums[], int elements){

for (int index = 0; index < elements; index++)cout << nums[index] << " ";

}

Page 56: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

56

Program Output

5 10 15 20 25 30 35 402 4 6 8

1 2 3 4 5 6 7 8 9 10 11 12

Page 57: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

57

Program

// This program uses a function that doubles the contents of// the elements within an array.#include <iostream.h>

void doubleArray(int [], int); // Function prototypeconst int arraySize = 12;

void main(void){

int set[arraySize] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};cout << "The arrays values are:\n";for (int index = 0; index < arraySize; index++)

cout << set[index] << " ";cout << endl;doubleArray(set, arraySize);cout << "After calling doubleArray, the values are:\n";

Page 58: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

58

Program continues

for (int index = 0; index < arraySize; index++)cout << set[index] << " ";

cout << endl;}

//**************************************************// Definition of function doubleArray. *// This function doubles the value of each element *// in the array passed into nums. *// The value passed into size is the number of *// elements in the nums array. *//**************************************************void doubleArray(int nums[], int size){

for (int index = 0; index < size; index++)nums[index] *= 2;

}

Page 59: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

59

Program Output

The array values are:1 2 3 4 5 6 7 8 9 10 11 12After calling doubleArray, the values are:

2 4 6 8 10 12 14 16 18 20 22 24

Page 60: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

60

Two-dimensional Arrays

A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data.

Page 61: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

61

Program

// This program demonstrates a two-dimensional array.

#include <iostream.h>

void main(void){

float sales[3][4]; // 2D array, 3 rows and 4 columns.float totalSales = 0; // To hold the total sales.int dir, qtr; // Loop counters.

Page 62: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

62

Program continues

cout << "This program will calculate the total sales of\n";cout << "all the company's divisions.\n";cout << "Enter the following sales information:\n\n";

// Nested loops to fill the array with quarterly// sales figures for each division.for (div = 0; div < 3; div++){

for (qtr = 0; qtr < 4; qtr++){

cout << "Division " << (div + 1);cout << ", Quarter " << (qtr + 1) << ": $";cin >> sales[div][qtr];

}cout << endl; // Print blank line.

}

Page 63: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

63

Program continues

// Nested loops to add all the elements.for (div = 0; div < 3; div++)

for (qtr = 0; qtr < 4; qtr++)totalSales += sales[div][qtr];

cout.precision(2);cout.setf(ios::fixed | ios::showpoint);cout << "The total sales for the company are: $";cout << totalSales << endl;

}

Page 64: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

64

Program Output with Example Input

This program will calculate the total sales ofall the company's divisions.Enter the following sales information:

Division 1, Quarter 1: $31569.45 [Enter]Division 1, Quarter 2: $29654.23 [Enter]Division 1, Quarter 3: $32982.54 [Enter]Division 1, Quarter 4: $39651.21 [Enter]

Division 2, Quarter 1: $56321.02 [Enter]Division 2, Quarter 2: $54128.63 [Enter]Division 2, Quarter 3: $41235.85 [Enter]Division 2, Quarter 4: $54652.33 [Enter]

Page 65: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

65

Output continues

Division 3, Quarter 1: $29654.35 [Enter]Division 3, Quarter 2: $28963.32 [Enter]Division 3, Quarter 3: $25353.55 [Enter]Division 3, Quarter 4: $32615.88 [Enter]

The total sales for the company are: $456782.34

Page 66: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

66

Arrays of Strings

A two-dimensional array of characters can be used as an array of C-strings.

Page 67: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

67

Program

// This program displays the number of days in each month.// It uses a two-dimensional character array to hold the // names of the months and an int array to hold the number// of days.#include <iostream.h>void main(void){

char months[12][10] = {"January", "February", "March", "April", "May", "June",

"July", "August", "September”, "October", "November","December"};int days[12] = { 31, 28, 31, 30,

31, 30, 31, 31, 30, 31, 30, 31};

for (int count = 0; count < 12; count++){

cout << months[count] << " has ";cout << days[count] << " days.\n";

} }

Page 68: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

68

Program (continued)

Program OutputJanuary has 31 days.February has 28 days.March has 31 days.April has 30 days. May has 31 days. June has 30 days. July has 31 days.August has 31 days. September has 30 days. October has 31 days. November has 30 days.December has 31 days.

Page 69: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

69

Three Dimensional Arrays and Beyond

C++ allows you to create arrays with virtually any number of dimensions.

Here is an example of a three-dimensional array declaration:

float seat[3][5][8];

Page 70: ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.

Questions