Array What it is. How to use it How to declare it How to initialize it.

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

Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Arrays Chapter 6.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 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
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
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.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
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.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
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.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
CSC 113: Computer Programming (Theory = 03, Lab = 01)
New Structure Recall “average.cpp” program
Chapter 7: Arrays.
آرايه ها اصول كامپيوتر 1.
7 Arrays.
Arrays Kingdom of Saudi Arabia
4.1 Introduction Arrays A few types Structures of related data items
Arrays Kingdom of Saudi Arabia
7 Arrays.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
C++ winter2008(by:J.Razjouyan)
Arrays Arrays A few types Structures of related data items
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Array What it is. How to use it How to declare it How to initialize it.
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.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Array What it is. How to use it How to declare it How to initialize it

What is an array? Consecutive group of memory locations that all have the same name and data type grades[0] grades[1] grades[2] grades[3] grades[4] name position or subscript value

Referencing elements in an array grades[0] grades[1] grades[2] grades[3] grades[4] To calculate the sum of the first 2 elements int sum = grades[0] + grades[1]; To change the value of the last element to 92 grades[4] = 92; position = subscript

Declaring an array Must tell compiler name, size and data type int grades[5]; char letters[26], numbers[10]; #define ARRAY_SIZE 5 int grades[ARRAY_ SIZE]; const int array_size 5; int grades[array_size]; May only use constants to declare the size of an array.

Initializing an array int counters[5]; // Initialize array for (int i = 0; i < 5; i++) counters[i] = 0; Note that i goes from 0 to 4 for a 5 element array not 1 to 5.

Initializing an array with a declaration int grades [5] = {100, 85, 50, 65, 88} or int grades [] = {100, 85, 50, 65, 88} char numbers[] = {‘0’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’} int n[10] = {0} // entire array is initialized to 0

Common programming errors Forgetting to initialize an array Too many initializers int grades [5] = {100, 85, 50, 65, 88, 99} // error!! Forgetting that the subscript range is 0 to (size - 1) and not 0 to size. int grades[5]; grades[5] = 2; // error!!

Exercises Answer the following questions regarding an array called fractions. 1) Define a constant variable array_size and initialize it to 10. 2) Declare an array with arraysize elements of type float and initialize the elements to ) Name the fourth element from the beginning of the array. 4) Assign the value to the 9th element in the array. 5) Print all the elements of the array using a for repetition structure.

More Exercises Find the error in the following code segments 1) int k = 4; char a1[k]; 2) int a2[5] = {1, 2, 3, 4, 5, 6} 3) int a[5]; for (int i = 0; i <= 5; i++) a[i] = 0;

scalability int grades[5]; // Initialize array for (int i = 0; i < 5; i++) grades[i] = 0; // Read in 5 grades. for (i = 0; i < 5; i++) { cout << "Enter next grade: "; cin >> grades[i]; } // Display grades for (i = 0; i < 5; i++) { cout << grades[i] << endl; }

Scalability const int array_size = 5; int grades[array_size]; // Initialize array for (int i = 0; i < array_size; i++) grades[i] = 0; // Read in 5 grades. for (i = 0; i < array_size; i++) { cout << "Enter next grade: "; cin >> grades[i]; } // Display grades for (i = 0; i < array_size; i++) { cout << grades[i] << endl; }

if (first == firstGuess) numHits++; else if ((first == secondGuess) || (first == thirdGuess)) numMatches++; if (second == secondGuess) numHits++; else if ((second == firstGuess) || (second == thirdGuess)) numMatches++; if (third == thirdGuess) numHits++; else if ((third == firstGuess) || (third == secondGuess)) numMatches++; What if you wanted to change the game to handle 5 or 10 letter sequences? Compare Example

Compare using arrays const int length = 3; // To handle 5 or 10 just change this char computerLetters [length]; char usersGuess[length]; int numHits = 0, numMatches = 0; for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (computerLetters[i] == usersGuess[j]) if (i == j) numHits++; else numMatches++; }

Survey Example Twenty students were asked to rate the quality of the food in the cafeteria on a scale of 1 to 10 with 10 meaning excellent. Place the twenty responses in an array of integers and summarize the results of the poll. Initialize responses array with student’s responses. Create another array to use to summarize their responses. subscript 1 in the array will contain a count of the number of students responses = 1, subscript 2 will contain a count of the number of student responses = 2, etc. Display responses in a table with 2 columns: Rating Frequency

Survey code const int numResponses = 20; const int maxRange = 10; int responses [numResponses] = {1, 2, 6, 4, 8, 5, 9, 7, 10, 6, 5, 8, 7, 3, 4, 6, 7, 8, 5, 6}; int frequency[maxRange + 1] = {0}; int pos; for (pos = 0; pos < numResponses; pos++) { frequency[responses[pos]]++; } cout << "Rating " << " Frequency" << endl; for (pos = 1; pos <= maxRange; pos++) { cout << setw(3) << pos << setw (11) << frequency[pos] << endl; }

survey code scalability To change number of students surveyed const int numResponses = 40; To change scale of responses from 1 to 10 to 1 to 5. const int maxRange = 5;

Exercise Change the survey code to also print a histogram as follows: Rating Frequency Histogram 1 1 * 2 1 * 3 1 * 4 2 ** 5 3 *** 6 4 **** 7 3 *** 8 3 *** 9 1 * 10 1 *

arrays as function parameters pass the name of the array without any brackets. pass array size (optional) always passed by reference

arrays as function parameters Pass the name of the array without the brackets void PrintResults(int results[ ]); // function prototype void PrintResults(int results[ ]) // function definition { } calling sequence: const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency);

arrays as function parameters Pass array size void PrintResults(int results[ ], int size); void PrintResults(int results[ ], int size) { cout << "Rating " << " Frequency" << endl; for (int pos = 1; pos <= size; pos++) { cout << setw(3) << pos << setw (11) << results[pos] << endl; } calling sequence: const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency, maxRange);

arrays as function parameters always passed by reference void PrintResults(int results[ ], int size); void PrintResults(int results[ ], int size) { results[5] = 10; // does change caller’s data } calling sequence: const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency, maxRange); cout << results[5] << endl; // displays 10;

Use const to prevent function changes void PrintResults(const int results[ ], int size); void PrintResults(const int results[ ], int size) { results[5] = 10; // compiler error!! } calling sequence: // this doesn’t change const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency, maxRange);

Entire arrays are passed by reference but not single elements const int maxRange = 10; int frequency[maxRange + 1] = {0}; void PrintResults( int results[ ], int size); calling sequence: PrintResults (frequency); results array was passed by reference. PrintResults could change it. void PrintResults (int element); calling sequence: PrintResults (frequency[4]); The 5th element of the array was passed by value not by reference. PrintResults cannot change it.

Exercises 1) Write a function called FindAverage that accepts an array of integers as an input parameter and returns the average. 2) Find the errors in the following code: int PrintSum( const int values[ ], int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += values[i]; } values[0] = 100; values[size] = sum; }