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

Array What it is. How to use it How to declare it How to initialize it.
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.
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.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays. Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related.
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.
 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.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
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.
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.
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.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing 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.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Chapter 7 Pointers and C-Strings
Chapter 6 Arrays in C++ 2nd Semester King Saud University
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 7 – Arrays.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Arrays Part-1 Armen Keshishian.
New Structure Recall “average.cpp” program
Chapter 7: Arrays.
آرايه ها اصول كامپيوتر 1.
DATA HANDLING.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
One-Dimensional Array Introduction Lesson xx
7 Arrays.
CNG 140 C Programming (Lecture set 8)
Introduction to Programming
7 Arrays.
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.
Lecture 12 Oct 16, 02.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
String What it is Why it’s useful
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
4.1 Introduction Arrays A few types Structures of related data items
Arrays Kingdom of Saudi Arabia
Arrays, Part 1 of 2 Topics Definition of a Data Structure
7 Arrays.
Arrays in Java.
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
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Programming Fundamental
Arrays, Part 1 of 2 Topics Definition of a Data Structure
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 100 value grades[0] grades[1] grades[2] grades[3] grades[4] 85 50 65 88 To refer to a particular element in an array we specify the name of the array and the position in the array. This array has 5 elements. name position or subscript

Referencing elements in an array 100 grades[0] grades[1] grades[2] grades[3] grades[4] 85 50 65 88 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 Note that in a 5 element array the subscript goes from 0 to 4.

Declaring an array Must tell compiler name, size and data type int grades[5]; char letters[26], numbers[10]; 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. const int array_size 5 defines array_size to be a constant variable that cannot be changed. It will always hold the number 5. constant variables may be used to declare the size of an array since they don’t change.

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 When there are fewer initializers than elements in the array the remaining elements are 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!! grades[5] =2 is error because there is no index 5 only to 4

Exercises Answer the following questions regarding an array called fractions. Define a constant variable array_size and initialize it to 10. const int array_size = 10; 2) Declare an array with array_size elements of type float and initialize the elements to 0.0. float T[array_size]={0.0}; 3) Name the fourth element from the beginning of the array. T[3]; 4) Assign the value 1.667 to the 9th element in the array. T[8]=1.667; 5) Print all the elements of the array using a for repetition structure. for(i=0;i< array_size-1;i++) cout<<“The element number “ << i+1<< “ is “<<T[i];

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; 1) k must be a constant variable. Add const since it’s the size of the array 2) too many initializers 3) boundary condition problem. a[5] = 0; is changing memory location that is not part of the array.

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 cout << grades[i] << endl; Manipulating array with for loop

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 cout << grades[i] << endl; To change this program to handle 100 grades instead of 5 just need to change one line.

Compare Example numHits++; if (first == firstGuess) numHits++; else if ((first == secondGuess) || (first == thirdGuess)) numMatches++; if (second == secondGuess) else if ((second == firstGuess) || (second == thirdGuess)) if (third == thirdGuess) else if ((third == firstGuess) || (third == secondGuess)) What if you wanted to change the game to handle 5 or 10 letter sequences? This excerpt is from homework 3. This is part of the function the compare routine does.

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 *