1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input 100...... Example –test scores,employees,temperatures.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Introduction to C Programming
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
CHAPTER 10 ARRAYS II Applications and Extensions.
HST 952 Computing for Biomedical Scientists Lecture 9.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Searching and Sorting Arrays
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 8 ARRAYS Continued
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Chapter 8 Arrays and Strings
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Arrays.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Objectives You should be able to describe: One-Dimensional Arrays
Searching Arrays Linear search Binary search small arrays
Computer Programming BCT 1113
Arrays … The Sequel Applications and Extensions
7 Arrays.
Chapter 8 Arrays Objectives
Chapter 9 One-Dimensional Arrays
Review of Arrays and Pointers
Starting Out with Programming Logic & Design
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
7 Arrays.
Chapter 8 Arrays Objectives
Presentation transcript:

1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures

2 Array structured data type array ‑ group of variables referred to by one name,composed of elements element or member – each item in the array –referred to by one name with an index: a[0],a[1],... index or subscript – references array element

3 Array Declaration type name[size]; type - type of each member in array –Can be of any type name - name of the array size - number of members in the array(constant) Dimension each array before it is used, one time only

4 Example float arr[20]; float testScores[30]; Or declare size as a constant const int MAX_TESTS = 100; int tests[MAX_TESTS];

5 C++ Examples int main() { float days[365]; char code[12]; int buf[100];

6 Storage float testScores[10];

7 Accessing Array Elements reference array element using subscript or index First element – subscript is 0 Last element – subscript is one less than size –int buf[100]; –buf[0] - first element of buf –buf[99] - last element of buf no checking for "Out of Bounds"

8 Valid subscripts can use variables,expression, or constants as subscripts angle[0] = 4.93; angle[1] = -15.2; val[i+2] = val[i] + val[i+1]; x = val[j] cout << score[k] <<score[k+1]<<score[k+2];

9 Array elements Each array element is treated exactly as a variable. buf[5] = 30 scores[1] = 95; i = buf[8] * 92; cout << buff[80]; if (a[i] < 14) cout << array [3]; x = y * nums[14]; cin >> sc[3]

10 Cannot operate on arrays as unit. The following are invalid: array = 0; cout << scores cin >> array arraya = arrayb;

11 Counted Loops and Arrays counted loop - based on reaching a fixed number of repetitions for (initialize;test;increment) for (i = 0; i < 10; i++)...

12 To read in an array: const int N = ?; int test[N]; int index; for (index = 0; index < N; index++) { cout << "Enter test “ <<index; cin >> test[index]; }

13 Printing an array const int N = ?; int test[N]; int index; for (index = 0; index < N; index++) { cout << test[index]; } Or with a label: cout << "test[“ << index <<"] is “<< test[index];

14 To set the entire array to 0: const int N = ?; int test[N]; int index; for (index = 0; index < N; index++) { test[index] = 0; }

15 To sum an array: const int N = ?; int test[N]; int index; sum = 0; for (index = 0; index < N; index++) { sum = sum + test[index]; }

16 To print an array in reverse order: const int N = ?; int test[N]; int index; for (index = N-1; index >= 0; index--) { cout << test[index]; } Or with a label: cout << "test[“ << index <<"] is “<< test[index];

17 To copy one array to another: must have same # of elements int array1[N], array2[N]; for (index = 0; index < N; index++) array1[index] = array2[index].

18 Reading any number of values into an array: string name [50]; //must allocate maximum size string tempName; int numNames; numNames = 0; infile >> tempName; while (infile && numNames < 50) { name[numNames] = tempName; numNames = numNames + 1; infile >> tempName; }

19 Multidimensional arrays arrays can have any number of dimensions Two dimensional –int hiTemp[52][7]; –Must use two subscripts to access –hiTemp[6][3] = 56.6; Three dimensional –int graph[10][20][30]; etc.

20 float table[3][4];

21 Stored in row major order table[0][0] table[0][1] table[0][2] table[0][3] table[1][0] table[1][1] table[1][2] table[1][3] table[2][0] table[2][1] table[2][2] table[2][3]

22 Initialize int table[10][6]; int row,col; for (row = 0; row < 10; row++) for (col = 0; row < 6; row++) table[row][col] = 0;

23 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow Sequential or Linear Search

24 int LinearSearch(/*in */ int array[], int size, int key) bool found; int ix; found = false; ix = 0; while (ix < size && !found) { if (array[ix] == key) found = true; else ix = ix + 1 } if (found) return ix; else return -1; }

25 BINARY SEARCH requires sorted list – (Ex: phone books, dictionaries). keep dividing list in half, compare against middle argument eliminates one half of the elements after each comparison. efficient

26 Simple Binary Search Algorithm: locates the middle element and compares it with the search key, till a match is found or no elements are left for comparison. The array: How to search for x: 1. Compare with the middle element, m 2. If x = m, done 3. If x > m, continue searching in the right half 4. If x < m, continue the search in the left half

27 void BinSearch( string list[], string item, int length, int& index, bool& found) { int first, last, middle; first = 0; // lower bound on list last = length - 1; // upper bound on list found = FALSE; while (last >= first && !found) { middle = (first + last)/2; if (item < list[middle] last = middle - 1; else if (item > list[middle]) first = middle + 1; else found = true; } index = middle; }

28 Average Number of Iterations

29 Records or Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT, unlike arrays, members also have names - field names, and may be different types; member

30 Declaration 1. define template - form of the structure; struct RecordName type member_1; type member_2; … endrecord 2. Declare variable RecordName varname;

31 Example: struct Employee { integer idNumber; string name; float salary; }; essentially creates a new variable type Employee emp1; Employee newEmp;

32 Reference use. Operator => recordName.membername example: newEmp.idNumber = 527; cout << newEmp.name;

33 Pointers Contains the address of another memory location DataType* Variable; int* iptr; & - address of * - location pointed to by

34 int i; int* iptr; iPtr = &i; *iptr = 5; iptri 5