Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 – Arrays.

Similar presentations


Presentation on theme: "Chapter 7 – Arrays."— Presentation transcript:

1 Chapter 7 – Arrays

2 7.1 Arrays Hold Multiple Values
Unlike regular variables, arrays can hold multiple values.

3 Figure 7-1

4 Figure 7-2

5 Table 7-1

6 7.2 Accessing Array Elements
The individual elements of an array are assigned unique subscripts. These subscripts are used to access the elements.

7 Program 7-1 // 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];

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; }

9 Program Output with Example Input
Enter the hours worked by six employees: [Enter] The hours you entered are:

10 Figure 7-7

11 Program 7-2 // 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; }

12 Program Output with Example Input
Enter the hours worked by six employees: [Enter] The hours you entered are:

13 Program 7-3 // 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. 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";

14 Program continues for (Count = 1; Count <= 6; Count++) {
cout << "Employee " << Count << ": "; cout << Hours[Count - 1] << endl; }

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 are Employee 1: 20 Employee 2: 12 Employee 3: 40 Employee 4: 30 Employee 5: 30 Employee 6: 15

16 7.3 No Bounds Checking in C++
C++ gives you the freedom to store data past an array’s boundaries.

17 Program 7-4 // This program unsafely accesses an area of memory by writing // values beyond an array's boundary. // WARNING: If you compile and run this program, it could cause // the computer to crash. #include <iostream.h> void main(void) { short Values[3]; // An array of 3 short integers. cout << "I will store 5 numbers in a 3 element array!\n"; for (int Count = 0; Count < 5; Count++) Values[Count] = 100; cout << "If you see this message, it means the computer\n"; cout << "has not crashed! Here are the numbers:\n"; cout << Values[Count] << endl; }

18 Figure 7-8

19 7.4 Array Initialization Arrays may be initialized when they are declared.

20 Program 7-5 // 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; // January Days[1] = 28; // February Days[2] = 31; // March Days[3] = 30; // April Days[4] = 31; // May Days[5] = 30; // June Days[6] = 31; // July

21 Program continues Days[7] = 31; // August Days[8] = 30; // September
Days[9] = 31; // October Days[10] = 30; // November Days[11] = 31; // December for (int Count = 0; Count < 12; Count++) { cout << "Month " << (Count + 1) << " has "; cout << Days[Count] << " days.\n"; }

22 Program Output Month 1 has 31 days. Month 2 has 28 days.

23 Program 7-6 // 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"; }

24 Program Output Month 1 has 31 days. Month 2 has 28 days.

25 Program 7-7 // 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; }

26 Program Output Character ASCII Code --------- ---------- A 65 B 66
A 65 B 66 C 67 D 68 E 69 F 70 G 71 H 72 I 73 J 74

27 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};

28 Program 7-8 // 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; }

29 Program Output Here are the contents of the array: 1 2 4 8

30 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};

31 Initializing With Strings
When initializing a character array with a string, simply enclose the string in quotation marks: char name[] = “Warren”;

32 Figure 7-11

33 Program 7-9 // 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; }

34 Program Output Holly Warren

35 7.5 Processing Array Contents
Individual array elements are processed like any other type of variable.

36 Program 7-10 // 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]; }

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 = Hours[Index] * PayRate; cout << "Employee #" << (Index + 1); cout << ": $" << GrossPay << endl; }

38 Program Output with Example Input
Enter the hours worked by 5 employees who all earn 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: [Enter] Here is the gross pay for each employee: Employee #1: $63.75 Employee #2: $127.50 Employee #3: $191.25 Employee #4: $255.00 Employee #5: $510.00

39 Program 7-11 // 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> 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]; }

40 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; }

41 Program continues else GrossPay = Hours[Index] * PayRate;
cout << "Employee #" << (Index + 1); cout << ": $" << GrossPay << endl; }

42 Program Output with Example Input
Enter the hours worked by 5 employees who all earn 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: [Enter] Here is the gross pay for each employee: Employee #1: $127.50 Employee #2: $255.00 Employee #3: $701.25 Employee #4: $510.00 Employee #5: $892.50

43 7.6 Focus on Software Engineering: Parallel Arrays
By using he same subscript, you can build relationships between data stored in two or more arrays.

44 Program 7-12 // This program stores, in two arrays, the hours worked by 5 // employees, and their hourly pay rates. #include <iostream.h> void main(void) { int Hours[5]; float PayRate[5]; cout << "Enter the hours worked by 5 employees and their\n"; cout << "hourly rates.\n"; for (int Index = 0; Index < 5; Index++) cout << "Hours worked by employee #" << (Index + 1); cout << ": ";

45 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 < 5; Index++) { float GrossPay = Hours[Index] * PayRate[Index]; cout << "Employee #" << (Index + 1); cout << ": $" << GrossPay << endl;

46 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: [Enter] Hours worked by employee #4: 40 [Enter] Hourly pay rate for employee #4: [Enter] Hours worked by employee #5: 40 [Enter] Hourly pay rate for employee #5: [Enter] Here is the gross pay for each employee: Employee #1: $97.50 Employee #2: $129.30 Employee #3: $210.00

47 7.7 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];

48 Figure 7-12

49 Table 7-2

50 7.8 Arrays As Function Arguments
To pass an array as an argument to a function, pass the name of the array.

51 Program 7-13 // 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]); }

52 Program continues void ShowValue(int Num) {
cout << Num << " "; }

53 Program Output

54 Program 7-14 // 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); } void ShowValues(int Nums[]) for (int Index = 0; Index < 8; Index++) cout << Nums[Index] << " ";

55 Program Output

56 Program 7-15 // 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); } void ShowValues(int Nums[]) for (int Index = 0; Index < 8; Index++) cout << Nums[Index] << " ";

57 Program Output

58 Program 7-16 // 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); ShowValues(Set3, 12); }

59 Program continues void ShowValues(int Nums[], int Elements) {
for (int Index = 0; Index < Elements; Index++) cout << Nums[Index] << " "; }

60 Program Output

61 Program 7-17 // This program uses a function that doubles the contents of // the elements within an array. #include <iostream.h> void DoubleArray(int [], int); // Function prototype const 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";

62 Program continues for (int Index = 0; Index < ArraySize; Index++)
cout << Set[Index] << " "; cout << endl; } void DoubleArray(int Nums[], int Size) { for (int Index = 0; Index < Size; Index++) Nums[Index] *= 2;

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

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

65 Figure 7-14

66 Program 7-18 // This program demonstrates a two-dimensional array.
#include <iostream.h> #include <iomanip.h> void main(void) { float Sales[3][4]; // 2D array, 3 rows and 4 columns. float TotalSales = 0; // To hold the total sales. int Row, Col; // Loop counters.

67 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 (Row = 0; Row < 3; Row++) { for (Col = 0; Col < 4; Col++) cout << "Division " << (Row + 1); cout << ", Quarter " << (Col + 1) << ": $"; cin >> Sales[Row][Col]; } cout << endl; // Print blank line.

68 Program continues // Nested loops to add all the elements.
for (Row = 0; Row < 3; Row++) for (Col = 0; Col < 4; Col++) TotalSales += Sales[Row][Col]; cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << "The total sales for the company are: $"; cout << TotalSales << endl; }

69 Program Output with Example Input
This program will calculate the total sales of all the company's divisions. Enter the following sales information: Division 1, Quarter 1: $ [Enter] Division 1, Quarter 2: $ [Enter] Division 1, Quarter 3: $ [Enter] Division 1, Quarter 4: $ [Enter] Division 2, Quarter 1: $ [Enter] Division 2, Quarter 2: $ [Enter] Division 2, Quarter 3: $ [Enter] Division 2, Quarter 4: $ [Enter]

70 Output continues Division 3, Quarter 1: $29654.35 [Enter]
The total sales for the company are: $

71 7.10 Arrays of Strings A two-dimensional array of characters can be used as an array of strings.

72 Passing Two-dimensional Arrays to Functions
When a two-dimensional array is passed to a function, the parameter type must contain a size declarator for the number of columns.

73 Program 7-19 // This program demonstrates a function that accepts a
// two-dimensional array as an argument. #include <iostream.h> #include <iomanip.h> void ShowArray(int [][4], int); // Function prototype void main(void) { int Table1[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int Table2[4][4] = {{10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120}, {130, 140, 150, 160}}; cout << "The contents of Table1 are:\n"; ShowArray(Table1, 3);

74 Program continues cout << "The contents of Table2 are:\n";
ShowArray(Table2, 4); } // Function Definition for ShowArray. // This function accepts a two-dimensional integer array as an // argument. The array must have four columns. The second // argument, Rows, specifies the number of rows in the array. The // function displays the contents of the array. void ShowArray(int Array[][4], int Rows) { for (int X = 0; X < Rows; X++) for (int Y = 0; Y < 4; Y++) cout << setw(4) << Array[X][Y] << " "; cout << endl;

75 Program Output The contents of Table1 are: 1 2 3 4 5 6 7 8 9 10 11 12
The contents of Table2 are:

76 Program 7-20 // 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};

77 Program continues for (int Count = 0; Count < 12; Count++) {
cout << Months[Count] << " has "; cout << Days[Count] << " days.\n"; }

78 Program Output January 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.


Download ppt "Chapter 7 – Arrays."

Similar presentations


Ads by Google