Arrays. Arrays are objects that help us organize large amounts of information.

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
A First Book of ANSI C Fourth Edition
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
ICS103 Programming in C Lecture 11: Arrays I
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Computer Programming for Engineers
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Two-Dimensional Arrays
Two-Dimensional Arrays
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Dr Tripty Singh Arrays.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
ICS103 Programming in C Lecture 12: Arrays I
Arrays.
Presentation transcript:

Arrays

Arrays are objects that help us organize large amounts of information

Arrays An array is an ordered list of values An array of size N is indexed from zero to N-1 scores The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9

Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) That expression represents a place to store a single integer and can be used wherever an integer variable can be used

Arrays For example, an array element can be assigned a value, printed, or used in a calculation: scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2; printf ("Top = %d”, scores[5]);

Arrays The values held in an array are called array elements The element type can be a primitive type or a structure. Therefore, we can create an array of integers, or an array of characters, or an array of String objects, etc.

Declaring Arrays The scores array could be declared as follows: int scores[10] ; The type of the variable scores is an array of integers The variable scores is set to a new blank array that can hold 10 integers

Declaring Arrays Some examples of array declarations: double prices[500] ; int factor[12], age[6]; char codes[30] ;

Bounds Checking Once an array is created, it has a fixed size That is, the index value must be in bounds (0 to N-1)

Bounds Checking For example, if the array codes can hold 100 values, it can be indexed using only the numbers 0 to 99 If count has the value 100, then the following reference will cause a problem: for (int index=0; index <= 100; index++) codes[index] = index*50 + epsilon; problem

Initializer Lists An initializer list can be used to instantiate and initialize an array in one step Examples: int units[] = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476}; char letterGrades[] = {'A', 'B', 'C', 'D', ’F'};

Initializer Lists Note that when an initializer list is used: no size value is specified The size of the array is determined by the number of items in the initializer list An initializer list can only be used only in the array declaration

Array Input/ Output We typically use for loops for any kind of array processing. To input an array, one by one: for (i=0; i<10 ; i++ ) { printf(“ Enter element %d : “, i ); scanf ( “ %d “, &scores[i] ); }

Array Output To display an array, one element per line: for (i=0; i<10 ; i++ ) { printf(“ scores [%d] : %d\n“, i, scores[i] ); }

Example double X[ ]= {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5} int i=5; printf(“%f”, x[4]); printf(“%f”, x[i]+1); printf(“%f”, x[i+1]); printf(“%f”, x[2*i]);

Example double X[ ]= {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5} int i=5; printf(“%f”, x[2*i-3]); printf(“%f”, x[(int) x[4]]); printf(“%f”, x[i++]); printf(“%f”, x[--i]); x[i]= x[i+1];

Example int Squre[10], I; for (I=0; I<10; I++) Squre[I]=I * 2 ;

Example Find the output of the following program: #include int main() { int q[]= {3, 1, 4, 2}; float n[]={3.0, 1.0, 2.0, 7.0}, s=0; for(int j=0; j<3; j++) s+= n[q[j]-1]; printf(“%f”, s); return 0; }

Find the output for(j=0; j<7;j++) A[j]=7-j; for(p=6; p>3; p--){ K=p-1; While(k>2){ T = A[p-k]; A[p-k] = A[7-p+k] A[7-p+k]= T } printf(“%d”, A[p]); }

Trace the program int X[7] = {12, 25, 4, 9, 0, -1, 12} for (int j=5; j>=0; ) { printf(“%d”, X[j+1]); j--; }

Find the output int A[7], sum =0; for(int j=0; j<7;j++) A[j]=7-j*3; for(int p=6; p>3; p--){ printf(“%d”, A[p]); sum=sum + A[p]; if(A[p]<2) printf(“%d”, p); } double Avg = sum / 7; printf(“%f”, Avg);

Example Write a program that reads in an array of 10 elements, gets their average, and then display the deviation of each element from that average.

Example Write a program that reads in an array of 10 integers (range , strict) and then draws a bar chart of the values: 10********** 5***** 15*************** ….

Two-Dimensional Arrays A one-dimensional array stores a list of elements A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions

Declaration int A[3][3] double B[3][3] float C[6][6]

Using To use 2-dimensional array we use 2 for-loop as for(i=0; i<3;i++) for(j=0; j<3; j++) A[i][j]=i*j;

Trace the program int A [3][3]= {{1, 7, 12}, {-2, 0, 4}, {45, 5, 4}} for(int i=1; i<3; i++) for(int j = 1; j<3; j=j+i) printf(“%d”, A[i][j]);

Program Write a program that read two matrices of size 5x5. and add two matrices to another one which will be displayed.

Example Write a C program to find the average of vector elements in 2-D matrix. For example, you are given matrix A[5][5] and index 3, so you will print the average of elements in raw 3

The End