Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Short C++ Review CS 302 – Data Structures. Call by value/reference.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
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.
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.
Multiple-Subscripted Array
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Arrays CS 308 – Data Structures. One-Dimensional Arrays A list of values with the same data type that are stored using a single group name (array name).
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
A First Book of ANSI C Fourth Edition
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Pointers CSE 5100 Data Structures and Algorithms.
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.
計算機程式語言 Lecture 07-1 國立臺灣大學生物機電系 7 7 Arrays.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Data Structures (Terminology) Static Set at compile time Dynamic Set at run time.
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.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
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 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
 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.
UNIT 10 Multidimensional Arrays.
Copyright 2003 Scott/Jones Publishing Alternate Version of Starting Out with C++, Third Edition Chapter 8 Arrays.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Arrays.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
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 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Objectives You should be able to describe: One-Dimensional Arrays
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
C++ Review Data Structures.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
CS 1430: Programming in C++.
Engineering Problem Solving with C++, Etter/Ingber
Lec 14 Oct 23, 02.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Presentation transcript:

Arrays CSE 5100 Data Structures and Algorithms

One-Dimensional Arrays  A list of values with the same data type that are stored using a single group name (array name).  General array declaration statement:  General array declaration statement: data-type array-name[number-of-items];  The number-of-items must be specified before declaring the array. const int SIZE = 100; float arr[SIZE];

b Individual elements of the array can be accessed by specifying the name of the array and the element's index: arr[3] b Warning: indices assume values from 0 to number-of-items -1!! One-Dimensional Arrays (cont.)

arr[0]arr[1]arr[2]arr[3]arr[4] Skip over 3 elements to get the starting location of element 3 The array name arr identifies the starting location of the array Start here element 3

1D Array Initialization  Arrays can be initialized during their declaration int arr[5] = {98, 87, 92, 79, 85}; int arr[5] = {98, 87} - what happens in this case??  What is the difference between the following two declarations ?  What is the difference between the following two declarations ? char codes[] = {'s', 'a', 'm', 'p', 'l', 'e'}; char codes[] = "sample"; codes[0]codes[1]codes[2]codes[3]codes[4]codes[5]codes[6] sample\0

Two-dimensional Arrays  A two-dimensional array consists of both rows and columns of elements.  General array declaration statement:  General array declaration statement: data-type array-name[number-of-rows][number-of- columns];

 The number-of-rows and number-of- columns must be specified before declaring the array. const int ROWS = 100; const int COLS = 50; float arr2D[ROWS][COLS];  Individual elements of the array can be accessed by specifying the name of the array and the element's row, column indices. arr2D[3][5] Two-dimensional Arrays (cont.)

2D Array Initialization  Arrays can be initialized during their declaration  Arrays can be initialized during their declaration int arr2D[3][3] = { {98, 87, 92}, {79, 85, 19}, {32, 18, 2} };  The compiler fills the array row by row (elements are stored in the memory in the same order).

1D Arrays as Arguments  Individual array elements are passed to a function in the same manner as other variables. max = find_max(arr[1], arr[3]); b To pass the whole array to a function, you need to specify the name of the array only!!

# include # include float find_average(int [], int); void main() { const numElems = 5; const numElems = 5; int arr[numElems] = {2, 18, 1, 27, 16}; int arr[numElems] = {2, 18, 1, 27, 16}; cout << "The average is " << find_average(arr, numElems) << endl; cout << "The average is " << find_average(arr, numElems) << endl;} float find_average(int vals[], int n) { int i; int i; float avg; float avg; avg=0.0; avg=0.0; for(i=0; i<n; i++) for(i=0; i<n; i++) avg += vals[i]; avg += vals[i]; avg = avg/n; avg = avg/n; return avg; return avg;} 1D Arrays as Arguments

 Important: this is essentially "call by reference": a)The name of the array arr stores the address of the first element of the array arr[0] (i.e., &arr[0]). b)Every other element of the array can be accessed by using its index as an offset from the first element. 1D Arrays as Arguments (cont.) arr[0]arr[1]arr[2]arr[3]arr[4] The starting address of arr array is &arr[0 ]. This is passed to the function find_average()

2D Arrays as Arguments  Individual array elements are passed to a function in the same manner as other variables.  Individual array elements are passed to a function in the same manner as other variables. max = find_max(arr2D[1][1], arr2D[1][2]);  To pass the whole array to a function, you need to specify the name of the array only!!  The number of columns must be specified in the function prototype and function header.

#include #include float find_average(int [][2], int, int); void main() { const numRows = 2; const numRows = 2; const numCols = 2; const numCols = 2; int arr2D[numRows][numCols] = {2, 18, 1, 27}; int arr2D[numRows][numCols] = {2, 18, 1, 27}; float average; float average; average = find_average(arr2D, numRows, numCols); average = find_average(arr2D, numRows, numCols); cout << "The average is " << average << endl; cout << "The average is " << average << endl;} 2D Arrays as Arguments

float find_average(int vals[][2], int n, int m) { int i,j; int i,j; float avg; float avg; avg=0.0; avg=0.0; for(i=0; i<n; i++) for(i=0; i<n; i++) for(j=0; j<m; j++) for(j=0; j<m; j++) avg += vals[i][j]; avg += vals[i][j]; avg = avg/(n*m); avg = avg/(n*m); return avg; return avg;} 2D Arrays as Arguments

 Important: this is essentially "call by reference": a)The name of the array arr2D stores the address of arr2D[0] (i.e., &arr2D[0]) b)arr2D[0] stores the address of the first element of the array arr2D[0][0] (&arr2D[0][0]) c)Every other element of the array can be accessed by using its indices as an offset from the first element. 2D Arrays as Arguments (cont.)