Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Introduction to C Programming
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Chapter 8: Arrays.
C Language.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Arrays and Strings.
Introduction to arrays
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
One Dimensional Arrays
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Multiple-Subscripted Array
Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
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.
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.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Array, Structure and Union
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
 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.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
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.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
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.
1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used 50 minutes 4 or 5 questions write full programs.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Windows Programming Lecture 03. Pointers and Arrays.
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?
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Engineering Problem Solving with C++, Etter/Ingber
CISC181 Introduction to Computer Science Dr
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Matrices.
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

Etter/Ingber Arrays and Matrices

Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of the same data type 4 An element of an array is accessed using the array name and an index or subscript 4 The name of the array is the address of the first element and the subscript is the offset 4 In C, the subscripts always start with 0 and increment by 1

Etter/Ingber Definition and Initialization 4 An array is defined using a declaration statement. data type array_name[size]; –allocates memory for size elements –subscript of first element is 0 –subscript of last element is size-1 –size must be a constant

Etter/Ingber Example int list[10]; –allocates memory for 10 integer variables –subscript of first element is 0 –subscript of last element is 9 –C does not perform any bounds checking on arrays list[0] list[1] list[9]

Etter/Ingber Initializing Arrays 4 Arrays can be initialized at the time they are declared. Examples: double taxrate[3] ={0.15, 0.25, 0.3}; char list[5] = {h,e,l,l,o}; double vector[100] = {0.0}; /*assigns zero to all 100 elements*/ int s[] = {5,0,-5}; /*the size of a s is 3 */

Etter/Ingber Assigning values to an array for loops are often used to assign values to an array Example: int list[10], i; for(i=0; i<10; i++) { list[i] = i; }

Etter/Ingber Input from a data file Arrays are often used to store information from a data file Example int k; double time[10], motion[10]; FILE *sensor3; sensor3 = fopen(sensor3.dat, r); for(k=0; k<10; k++) { fscanf(sensor3, %lf %lf,&time[k], &motion[k]); }

Etter/Ingber Practice! Show the contents of the arrays defined in each of the following sets of statements. int x[10] = {-5, 4, 3}; char letters[] = {'a', 'b', 'c'}; double z[4];

Etter/Ingber Function Arguments 4 Individual elements of an array can be passed as regular arguments. Example int main(void) { /* Declare variables and functions */ void fun donothing(int, int); int array[5] = {1,2,3,4,5}; donothing(array[2], array[4]);.

Etter/Ingber Passing Entire Arrays as Arguments to Functions 4 Arrays are always pass by reference 4 The array name is the address of the first element 4 The maximum size of the array must be specified at the time the array is declared. The actual number of array elements that are used will vary, so the actual size of the array is usually passed as another argument to the function

Etter/Ingber Example int main(void) { /* Declare variables and functions */ FILE *exp1; double max (double array[], int actual_size); double x[100]; int count=0; exp1 = fopen(exp1.dat, r); while((fscanf(exp1, %f, &x[count])) == 1) { count++; } printf(Maximum value: %f \n, max(x, count)); fclose(exp1); return 0; }//end main

Etter/Ingber Selection Sort void selection_sort(double x[], int n) { /* Declare variables and functions */ int max_pos, i; int find_max_pos(double x[], int n, int i); void swap(double x[], int p1, int p2); for(i=0; i<n-1; i++) { max_pos = find_max_pos(x, n, i); swap(x, i, max_pos); } }//end selection_sort

Etter/Ingber Modify! Write the function definitions for the functions è int find_max_pos(double x[], int n, int i); è void swap(double x[], int p1, int p2);

Etter/Ingber Matrices 4 A matrix is a set of number arranged in a grid with rows and columns. A matrix is defined using a type declaration statement. –data type array_name[row_size][column_size]; –int matrix[3][4]; row[0] row[1] row[2] in memory row0row1row2

Etter/Ingber Accessing Array Elements 4 int matrix[3][4]; –matrix has 12 integer elements –matrix[0][0]element in first row, first column –matrix[2][3]element in last row, last column –matrix is the address of the first element –matrix[1] is the address of the second row

Etter/Ingber 2-Dimensional Arrays as Arguments to Functions Example: void transpose(int b[NROWS][NCOLS], int bt[NCOLS][NROWS]) { /* Declare Variables. */ int i, j; /* Transfer values to the transpose matrix. */ for(i=0; i<NROWS; i++) { for(j=0; j<NCOLS; j++) { bt[j][i] = b[i][j]; } return; }