Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.

Slides:



Advertisements
Similar presentations
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Advertisements

©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
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
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
CS102 Introduction to Computer Programming Chapter 7 Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Starting Out with C++, 3 rd Edition 1 Chapter 7 – Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS102 Introduction to Computer Programming Chapter 9 Pointers.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 7: Arrays.
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.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Objectives You should be able to describe: One-Dimensional Arrays
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.
Chapter 7 – Arrays.
Computer Programming BCT 1113
Week 13 & 14 Arrays SCK1213Programming Technique I SEM1 2009/2010
Arrays Part-1 Armen Keshishian.
New Structure Recall “average.cpp” program
Multi-dimensional Array
Arrays Arrays A few types Structures of related data items
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting Out with C++, Tony Gaddis, 2 nd Ed.

7.1 Arrays Hold Multiple values Unlike regular variables, arrays can hold multiple values. C++ Programming, Namiq Sultan2

Figure 7-1 C++ Programming, Namiq Sultan3 int count Enough memory for 1 int float price Enough memory for 1 float char letter Enough memory for 1 char A

Figure 7-2 C++ Programming, Namiq Sultan4 int Days[6];

Table 7-1 C++ Programming, Namiq Sultan5

7.2 Accessing Array elements The individual elements of an array are assigned unique subscripts. These subscripts are used to access the elements. C++ Programming, Namiq Sultan6

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 int 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]; C++ Programming, Namiq Sultan7

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; } C++ Programming, Namiq Sultan8

Program Output with Example Input Enter the hours worked by six employees: [Enter] The hours you entered are: C++ Programming, Namiq Sultan9

Figure 7-7 C++ Programming, Namiq Sultan10

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 int 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; } C++ Programming, Namiq Sultan11

Program Output with Example Input Enter the hours worked by six employees: [Enter] The hours you entered are: C++ Programming, Namiq Sultan12

7.4 Array Initialization Arrays may be initialized when they are declared. C++ Programming, Namiq Sultan13

Program 7-6 // This program displays the number of days in each month. // It uses a 12-element int array. #include int main() { 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"; } C++ Programming, Namiq Sultan14

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. C++ Programming, Namiq Sultan15

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 int main() { 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; } C++ Programming, Namiq Sultan16

Program Output CharacterASCII Code A65 B66 C67 D68 E69 F70 G71 H72 I73 J74 C++ Programming, Namiq Sultan17

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}; C++ Programming, Namiq Sultan18

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}; C++ Programming, Namiq Sultan19

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

Figure 7-11 C++ Programming, Namiq Sultan21

Program 7-9 // This program displays the contents of two char arrays. #include int main() { char name1[] = "Holly"; char name2[] = {'W', 'a', 'r', 'r', 'e', 'n', '\0'}; cout << name1 << endl; cout << name2 << endl; } C++ Programming, Namiq Sultan22

Program Output Holly Warren C++ Programming, Namiq Sultan23

7.5 Processing Array Contents Individual array elements are processed like any other type of variable. C++ Programming, Namiq Sultan24

Program 7-10 // This program stores, in an array, the hours worked by 5 // employees who all make the same hourly wage. #include int main() { 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]; } C++ Programming, Namiq Sultan25

cout << "Enter the hourly pay rate for the employees: "; cin >> payRate; cout << "Here is the gross pay for each employee:\n"; for (index = 0; index < 5; index++) { float grossPay = hours[index] * payRate; cout << "Employee #" << (index + 1); cout << ": $" << grossPay << endl; } C++ Programming, Namiq Sultan26

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: $ Employee #3: $ Employee #4: $ Employee #5: $ C++ Programming, Namiq Sultan27

7.8 Arrays As Function Arguments To pass an array as an argument to a function, pass the name of the array. C++ Programming, Namiq Sultan28

Program 7-13 // This program demonstrates that an array element is // passed to a function like any other variable. #include using namespace std; void ShowValue(int Num) { cout << Num << " "; } int main() { int collection[8] = {5, 10, 15, 20, 25, 30, 35, 40}; for (int Cycle = 0; Cycle < 8; Cycle++) ShowValue(collection[Cycle]); } C++ Programming, Namiq Sultan29

Program Output C++ Programming, Namiq Sultan30

Program 7-14 // This program demonstrates an array being passed to a function. #include // Definition of function showValues. This function accepts an array void showValues(int nums[]) { for (int index = 0; index < 8; index++) cout << nums[index] << " "; } int main() { int collection[8] = {5, 10, 15, 20, 25, 30, 35, 40}; showValues(collection); // Passing address of array collection } C++ Programming, Namiq Sultan31

Program Output C++ Programming, Namiq Sultan32

Program 7-16 // This program uses a function that display the contents // of an integer array of any size. #include void showValues(int nums[], int elements){ for (int index = 0; index < elements; index++) cout << nums[index] << " "; } int main(){ 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); } C++ Programming, Namiq Sultan33

Program Output C++ Programming, Namiq Sultan34

Program 7-17 // This program uses a function that doubles the contents // of the elements within an array. #include void doubleArray(int [], int);// Function prototype const int arraySize = 12; int main() { 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"; C++ Programming, Namiq Sultan35

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; } C++ Programming, Namiq Sultan36

Program Output The array values are: After calling doubleArray, the values are: C++ Programming, Namiq Sultan37

Examples C++ program to find the largest number in a given array. Repeat (1) using a function that accepts an array and returns the largest element. C++ program to find the largest two number s in a given array. Construct an array of Fibonacci series. Sort an array of three elements. HW: Reverse the elements of a given array. Count the repetition of number 0 in a given array. Find the sum of smallest and largest elements of a given array. C++ Programming, Namiq Sultan38

7.10 Two-dimensional Arrays A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data. C++ Programming, Namiq Sultan39

Program 7-18 // This program demonstrates a two-dimensional array. #include int main() { float sales[3][4]; // 2D array, 3 rows and 4 columns. float totalSales = 0;// To hold the total sales. int dir, qtr;// Loop counters. cout << "This program calculate the total sales of\n"; cout << "all the company's divisions.\n"; cout << "Enter the following sales information:\n\n"; C++ Programming, Namiq Sultan40

// 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. } C++ Programming, Namiq Sultan41

// Nested loops to add all the elements. for (div = 0; div < 3; div++) for (qtr = 0; qtr < 4; qtr++) totalSales += sales[div][qtr]; cout << "The total sales for the company are: $"; cout << totalSales << endl; } C++ Programming, Namiq Sultan42

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] C++ Programming, Namiq Sultan43

Output continues Division 3, Quarter 1: $ [Enter] Division 3, Quarter 2: $ [Enter] Division 3, Quarter 3: $ [Enter] Division 3, Quarter 4: $ [Enter] The total sales for the company are: $ C++ Programming, Namiq Sultan44

Two-dimensional Array Initialization Both of the following declarations perform the same initialization: int Hours[3][2]={{8,5},{7,9},{6,3}}; int Hours[3][2]={8,5,7,9,6,3}; It is helpful to write the declaration as: int Hours[3][2]={8,5, 7,9, 6,3}; C++ Programming, Namiq Sultan45

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. C++ Programming, Namiq Sultan46

Program 7-19 // This program demonstrates a function that accepts a two-dimensional // array as an argument. #include void showArray(int [][4], int); // Function prototype int main() { int Table[3][4] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; C++ Programming, Namiq Sultan47

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); Cout<<“The contents of Table2 are:\n”; showArray(Table2, 4); } C++ Programming, Namiq Sultan48

// 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<< Array[x][y] << “\t“; cout << endl; } C++ Programming, Namiq Sultan49

7.10 Arrays of Strings A two-dimensional array of characters can be used as an array of C-strings. C++ Programming, Namiq Sultan50

Program 7-20 C++ Programming, Namiq Sultan51 // 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 int main() { 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"; } }

C++ Programming, Namiq Sultan52 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.

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]; C++ Programming, Namiq Sultan53

Homework Add two 3x3 matrices and put the result in a third matrix. Exchange the elements of first column and second column of a given matrix. Find the sum of elements of upper-right triangle of a matrix. Print the lower-left triangle of a matrix. Write a program to print transpose of a matrix. C++ Programming, Namiq Sultan54