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

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
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 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Chapter 11 – Structured Data
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
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.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CS102 Introduction to Computer Programming Chapter 11 Structured Data.
Chapter 8 Arrays and Strings
Starting Out with C++, 3 rd Edition 1 Chapter 7 – Arrays.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
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)
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
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.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
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.
Starting Out with C++, 3 rd Edition Array problem solving in C++ Dr Ahmed Telba.
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.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
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.
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.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
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)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
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.
Structured Data.
Chapter 7 – Arrays.
Week 13 & 14 Arrays SCK1213Programming Technique I SEM1 2009/2010
New Structure Recall “average.cpp” program
Arrays Kingdom of Saudi Arabia
Starting Out with Programming Logic & Design
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Presentation transcript:

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 float price Enough memory for 1 float char letter Enough memory for 1 char A

4 Figure

5 Table

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

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

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 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 // 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 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 Array Initialization  Arrays may be initialized when they are declared.

17 Program // This program displays the number of days in each month. // It uses a 12-element int array. #include 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

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

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.

20 Program // This program displays the number of days in each month. // It uses a 12-element int array. #include 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"; }

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.

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

23 Program Output CharacterASCII Code A65 B66 C67 D68 E69 F70 G71 H72 I73 J74

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

25 Program // This program has a partially initialized array. #include 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; }

26 Program Output Here are the contents of the array:

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

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

29 Figure

30 Program // This program displays the contents of two char arrays. #include void main(void) { char name1[] = "Holly"; char name2[] = {'W', 'a', 'r', 'r', 'e', 'n', '\0'}; cout << name1 << endl; cout << name2 << endl; }

31 Program Output Holly Warren

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

33 Program // This program stores, in an array, the hours worked by 5 // employees who all make the same hourly wage. #include 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]; }

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

35 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: $510.00

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

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

38 Program continues else grossPay = hours[index] * payRate; cout << "Employee #" << (index + 1); cout << ": $" << grossPay << endl; }

39 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: $ Employee #2: $ Employee #3: $ Employee #4: $ Employee #5: $892.50

40 Program // This program stores, in two arrays, the hours worked by 5 // employees, and their hourly pay rates. #include // 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 << ": ";

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

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: [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: $ Employee #3: $210.00

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

44 Table

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;

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

47 Program // This program demonstrates that an array element is passed // to a function like any other variable. #include 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]); }

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

49 Program Output

50 Program // This program demonstrates an array being passed to a function. #include 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] << " "; }

51 Program Output

52 Program // This program demonstrates an array being passed to a function. #include 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] << " "; }

53 Program Output

54 Program // This program uses a function that can display the contents // of an integer array of any size. #include 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); }

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

56 Program Output

57 Program // 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; 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";

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

59 Program Output The array values are: After calling doubleArray, the values are:

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

61 Program // This program demonstrates a two-dimensional array. #include 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.

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

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

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

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

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

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

68 Program (continued) 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.

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

Questions