An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.

Slides:



Advertisements
Similar presentations
A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Advertisements

UNIT IV.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Chapter 7: Arrays In this chapter, you will learn about
Arrays.
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.
One Dimensional Arrays
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
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 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
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 9 Introduction to Arrays
Arrays.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Java Unit 9: Arrays Declaring and Processing Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
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.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Pascal Programming Arrays and String Type.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Arrays.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
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 9 Introduction to Arrays Fundamentals of Java.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
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?
Array in C# Array in C# RIHS Arshad Khan
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
Arrays in C.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 8 Arrays Objectives
EKT150 : Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Topics discussed in this section:
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 8 Arrays Objectives
CS149D Elements of Computer Science
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared int a[10] which declares an array of 10 integers whose indices go from 0-9

 The length of the array is set when it is created. It cannot change.  Individual array elements are accessed via an index.  Array index numbering starts at 0. Length of Arrays

Arrays An array: an object that can hold a fixed number of values of the same type. Array to the right contains 4 int values a0 A declaration has the basic form [arraylength] ; int x[10] ; Elements of the array are numbered 0, 1, 2, …, x.length–1; length is a variable, not a function, so don’t put () after it. The type of the array to the right is int Here is a variable that contains the name of the array. x a0 int[] Make everything as simple as possible, but no simpler. Einstein

Arrays a Assign 2*x[0], i.e. -8, to x[3] Assign 6 to x[2] int k= 3; x[k]= 2* x[0]; x[k-1]= 6; x[2]= 5; x[0]= -4; a Assign 5 to array element 2 and -4 to array element 0 x[2] is a reference to element number 2 of array x

Array initializers Instead of int c[5]; c[0]= 5; c[1]= 4; c[2]= 7; c[3]= 6; c[4]= 5; Use an array initializer: int c[5]= {5, 4, 7, 6, 5}; a0 array initializer: gives the values to be in the array initially. The values must all have the same type, in this case, int. The length of the array is the number of values in the list Computer science has its field called computational complexity; mine is called computational simplicity. Gries

Processing Array Elements Access individual elements of an array using: –the name (handle) of the array –a number (index or subscript) that tells which of the element of the array What value is stored in count[2] ? count[0] [1] [2] [3]

Processing Array Elements Often a for( ) loop is used to process each of the elements of the array in turn. for (int i = 0; i < NUM_STUDENTS; i++) {); } The loop control variable, i, is used as the index to access array components

Some exercises Write a program which finds the maximum of an array Write a program which finds the average of a list of integers

Largest of 10 Integers #Include main() { int i, a,max; printf(“enter number \n”); scanf(“%d”,&a); max = a; for (i=1;I < 10; i++) { printf(“enter number \n”); scanf(“%d”,&a); If (a > max) max = a; }; printf(“largest is %d”,max); }

Largest of 10 Integers #Include main() { int i, max; int a[10]; printf(“enter number \n”); scanf(“%d”,&a[0]); max = a[0]; for (i=1;I < 10; i++) { printf(“enter number \n”); scanf(“%d”,&a[i]); If (a[i] > max) max = a[i]; }; printf(“largest is %d”,max); }

Largest of 10 Integers #Include main() { int i, max; int a[10]= {7,3,4,28,4,56,12,3,44,7} max = a[0]; for (i=1;I < 10; i++) If (a[i] > max) max = a[i]; }; printf(“largest is %d”,max); }

 Arrays with multiple dimensions can also be created.  They are created and initialized in the same way as single dimensioned arrays. Multi-dimensional Arrays type arrayName[rowlength] [collength]; declaration syntax: intgrades[20][5]; for(int i = 0; i< 20; i++) for(int j = 0; j<5; j++) grades[i][j] = 100;

Exercises Write Programs to 1: given an array of 20 Integers print out all those > 15 and print out their positions. 2: Given an array of 20 integers, delete the 5 th element so that: Before etc After etc

Exercise Write a program which takes an array and reverses it.

We could use scanf and a for loop #include main() { int a[10] int i for ( i = 0;i < 10 ; i++) { printf(“enter a number”); scanf(“%d”,&a[i]); } for ( i = 0;i < 10 ; i++) printf(“the %d th number in the arrray is %d \n”,i,a[i]); for ( i = 9;i >= 0 ; i--) printf(“the %d th number in the arrray is %d \n”,i,a[i]); }

Exercise Write a program to enter 10 numbers into an array and to print out the maximum element of the array.

We could use scanf and a for loop #include main() { int a[10] int I; Int max; Printf(“enter first number”); scanf(“%d”,&a[0]); max = a[0]; for ( i = 1;i < 10 ; i++) { printf(“enter a number”); scanf(“%d”,&a[i]); If (a[i] > max) max = a[i]; } printf(“the max number in the arrray is %d \n”,max); }