Chapter 7 – Arrays.

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
Advertisements

Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Chapter 9: Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
CS102 Introduction to Computer Programming Chapter 7 Arrays.
Starting Out with C++, 3 rd Edition 1 Chapter 7 – Arrays.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
GE 211 Programming in C++ Array &Matrix Dr. Ahmed Telba.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Starting Out with C++, 3 rd Edition 1 Chapter 7 – Arrays.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
STARTING OUT WITH STARTING OUT WITH Class 2 2 Review of course requirements: Homework due Thursday at beginning of class-- no lates accepted Programs.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
Starting Out with C++, 3 rd Edition 1 Chapter 7 – Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
Starting Out with C++, 3 rd Edition 1 Chapter 7 – Arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Structured Data.
Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their own range (or domain) of data and their own set of.
CO1401 Program Design and Implementation
Chapter 7: Arrays.
Chapter 9: Pointers.
Chapter 10 – Characters and Strings
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Arrays exist in almost every computer language.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Chapter 3. Expressions and Interactivity
Week 13 & 14 Arrays SCK1213Programming Technique I SEM1 2009/2010
Chapter 1. Introduction to Computers and Programming
Arrays Part-1 Armen Keshishian.
Chapter 5. Looping.
New Structure Recall “average.cpp” program
Multi-dimensional Array
Chapter 7: Arrays.
C++ Data Types Simple Structured Address Integral Floating
Chapter 9: Pointers.
7 Arrays.
Chapter 5. Looping.
Arrays Kingdom of Saudi Arabia
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Chapter 8 – Searching and Sorting Arrays
Starting Out with Programming Logic & Design
Standard Version of Starting Out with C++, 4th Edition
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Chapter 7 – Arrays

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

Figure 7-1

Figure 7-2

Table 7-1

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

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

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

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

Figure 7-7

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

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

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

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

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

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

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

Figure 7-8

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

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

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

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

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

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

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

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

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

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

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

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

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

Figure 7-11

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

Program Output Holly Warren

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

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

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

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: 12.75 [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

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

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

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

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: 12.75 [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

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.

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 << ": ";

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;

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.50 Employee #2: $129.30 Employee #3: $210.00

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

Figure 7-12

Table 7-2

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

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

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

Program Output 5 10 15 20 25 30 35 40

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] << " ";

Program Output 5 10 15 20 25 30 35 40

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] << " ";

Program Output 5 10 15 20 25 30 35 40 2 4 6 8 10 12 14 16

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

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

Program Output 5 10 15 20 25 30 35 40 2 4 6 8 1 2 3 4 5 6 7 8 9 10 11 12

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

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;

Program Output The array values are: 1 2 3 4 5 6 7 8 9 10 11 12 After calling DoubleArray, the values are: 2 4 6 8 10 12 14 16 18 20 22 24

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.

Figure 7-14

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.

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.

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

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: $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]

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

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

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.

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

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;

Program Output The contents of Table1 are: 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 The contents of Table2 are: 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160

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

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

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.