Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.

Slides:



Advertisements
Similar presentations
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
Advertisements

4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –Tables with rows and columns –Specify.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Systems Programming Concepts
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
chap8 Chapter 8 Arrays (reference: Deitel ’ s chap 6)
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig04_03.cpp (1 of 2) 1 // Fig. 4.3: fig04_03.cpp 2 // Initializing an array. 3 #include 4 5.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Arrays Declarations CSCI N305
آرايه ها اصول كامپيوتر 1.
C Arrays Systems Programming.
Multidimensional Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Dale Roberts, Lecturer IUPUI
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays Outline Introduction Arrays Declaring Arrays
C++ Programming Lecture 16 Arrays – Part III
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
4.9 Multiple-Subscripted Arrays
Arrays Strings and Parameter Passing CSCI N305
Arrays Kingdom of Saudi Arabia
To refer to an element, specify
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Arrays.
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Dale Roberts, Lecturer IUPUI
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname [ position number ] –First element at position 0 –n element array named c : c[0], c[1]... c[n – 1] BBS514 Structured Programming (Yapısal Programlama)1

2 Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4]

Array Elements Array elements are like normal variables c[0] = 3; printf( "%d", c[0] ); We can perform operations in subscript. e.g. If x equals 3 c[5-2] == c[3] == c[x] c[x+1] == c[4] c[x-1] == c[2] BBS514 Structured Programming (Yapısal Programlama)3

Declaring Arrays When declaring arrays, specify –Name –Type of array –Number of elements arrayType arrayName[ numberOfElements ]; –Examples: int c[10]; float myArray[3284]; Declaring multiple arrays of same type –Format similar to regular variables –Example: int b[100], x[27]; BBS514 Structured Programming (Yapısal Programlama)4

Examples Using Arrays Initializers int n[5] = { 1, 2, 3, 4, 5 }; –If not enough initializers, rightmost elements become 0 int n[5] = { 0 } All elements 0 –If too many a syntax error is produced syntax error –C arrays have no bounds checking If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; –5 initializers, therefore 5 element array BBS514 Structured Programming (Yapısal Programlama)5

Initializing an Array #include int main() { int n[100], i; for (i=0; i < 100; i++) n[i] = i; for (i=0; i < 100; i++) printf(“Element %d has value %d.\n”,i, n[i]); return 1; } BBS514 Structured Programming (Yapısal Programlama)6

Examples BBS514 Structured Programming (Yapısal Programlama)7 Reading values into an array int i, x[100]; for (i=0; i < 100; i=i+1) { printf("Enter an integer: "); scanf("%d",&x[i]); } Summing up all elements in an array int sum = 0; for (i=0; i<=99; i=i+1) sum = sum + x[i];

Examples (contd.) Shifting the elements of an array to the left. /* store the value of the first element in a * temporary variable */ temp = x[0]; for (i=0; i < 99; i=i+1) x[i] = x[i+1]; //The value stored in temp is going to be the value of //the last element: x[99] = temp; BBS514 Structured Programming (Yapısal Programlama)8

Examples Finding the location of a given value ( item ) in an array. BBS514 Structured Programming (Yapısal Programlama)9 i = 0; while ((i<100) && (x[i] != item)) i = i + 1; if (i == 100) loc = -1; // not found else loc = i; // found in location i

BBS514 Structured Programming (Yapısal Programlama)10 1/* Histogram printing program */ 2 3#include 4#define SIZE int main() 7{7{ 8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 9 int i, j; printf( "%s%13s%17s\n", "Element", "Value", "Histogram" ); for ( i = 0; i <= SIZE - 1; i++ ) { 14 printf( "%7d%13d ", i, n[i]) ; for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */ 17 printf( "%c", '*' ); printf( "\n" ); 20 } return 0; 23}

Program Output BBS514 Structured Programming (Yapısal Programlama)11 Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *

Passing Arrays to Functions Passing arrays –To pass an array argument to a function, specify the name of the array without any brackets int myArray[ 24 ];//declaration in main myFunction( myArray, 24 ); //calling the function Array size is usually passed to function –Arrays passed call-by-reference –Name of array is address of first element –Function knows where the array is stored Modifies original memory locations Passing array elements –Passed by call-by-value –Pass subscripted name (i.e., myArray[ 3 ] ) to function BBS514 Structured Programming (Yapısal Programlama)12

Passing Arrays to Functions Function prototype void modifyArray( int b[], int arraySize ); Parameter names optional in prototype int b[] could be written int [] int arraySize could be simply int BBS514 Structured Programming (Yapısal Programlama)13

Example BBS514 Structured Programming (Yapısal Programlama)14 int sum(int a[], int n) { int j, s=0; for (j=0; j <n ; j++) s = s+ a[j]; return s; } Note: a[] is a notational convenience. In fact int a[]  int *a Calling the function: int total, x[100]; total = sum(x, 100); total = sum(x, 88); total = sum(&x[5], 50);

BBS514 Structured Programming (Yapısal Programlama)15 1/* Passing arrays and individual array elements to functions */ 2 3#include 4#define SIZE 5 5 6void modifyArray( int [], int ); /* appears strange */ 7void modifyElement( int ); 8 9int main() 10{ 11 int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i; printf( "Effects of passing entire array call " 14 "by reference:\n\nThe values of the " 15 "original array are:\n" ); for ( i = 0; i <= SIZE - 1; i++ ) 18 printf( "%3d", a[ i ] ); printf( "\n" ); 21 modifyArray( a, SIZE ); /* passed call by reference */ 22 printf( "The values of the modified array are:\n" ); for ( i = 0; i <= SIZE - 1; i++ ) 25 printf( "%3d", a[ i ] ); printf( "\n\n\nEffects of passing array element call " 28 "by value:\n\nThe value of a[3] is %d\n", a[ 3 ] ); 29 modifyElement( a[ 3 ] ); 30 printf( "The value of a[ 3 ] is %d\n", a[ 3 ] ); 31 return 0; 32} Entire array’s passed call-by- reference, and can be modified Array element is passed call-by- value, and cannot be modified

BBS514 Structured Programming (Yapısal Programlama) void modifyArray( int b[], int size ) 35{ 36 int j; for ( j = 0; j <= size - 1; j++ ) 39 b[ j ] *= 2; 40} 41 42void modifyElement( int e ) 43{ 44 printf( "Value in modifyElement is %d\n", e *= 2 ); 45} Effects of passing entire array call by reference: The values of the original array are: The values of the modified array are: Effects of passing array element call by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6

Two Dimensional Arrays Multiple subscripted arrays –Tables with rows and columns ( m by n array) –Like matrices: specify row, then column BBS514 Structured Programming (Yapısal Programlama)17 Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript

Two Dimensional Arrays Initialization –int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; –Initializers grouped by row in braces –If not enough, unspecified elements set to zero int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Referencing elements –Specify row, then column printf( "%d", b[ 0 ][ 1 ] ); BBS514 Structured Programming (Yapısal Programlama)

Examples Reading values into a two-dimensional array: int a[10][20]; for (row=0; row < 10; row = row+1){ for(col=0; col < 20; col = col+1) { printf(“Enter a number: "); scanf("%d",&a[row][col]); } BBS514 Structured Programming (Yapısal Programlama)19

BBS514 Structured Programming (Yapısal Programlama)20 1/* Two-dimensional array example 2 */ 3#include 4#define STUDENTS 3 5#define EXAMS 4 6 7int minimum( int [][ EXAMS ], int, int ); 8int maximum( int [][ EXAMS ], int, int ); 9double average( int [], int ); 10void printArray( int [][ EXAMS ], int, int); 11 12int main() 13{ 14 int student; 15 int studentGrades[ STUDENTS ][ EXAMS ] = 16 { { 77, 68, 86, 73 }, 17 { 96, 87, 89, 78 }, 18 { 70, 90, 86, 81 } }; printf( "The array is:\n" ); 21 printArray( studentGrades, STUDENTS, EXAMS ); 22 printf( "\n\nLowest grade: %d\nHighest grade: %d\n", 23 minimum( studentGrades, STUDENTS, EXAMS ), 24 maximum( studentGrades, STUDENTS, EXAMS ) ); for ( student = 0; student <= STUDENTS - 1; student++ ) 27 printf( "The average grade for student %d is %.2f\n", 28 student, 29 average( studentGrades[ student ], EXAMS ) ); return 0; 32} Each row is a particular student, each column is the grades on the exam.

BBS514 Structured Programming (Yapısal Programlama) /* Find the minimum grade */ 35int minimum( int grades[][ EXAMS ], 36 int pupils, int tests ) 37{ 38 int i, j, lowGrade = 100; for ( i = 0; i <= pupils - 1; i++ ) 41 for ( j = 0; j <= tests - 1; j++ ) 42 if ( grades[ i ][ j ] < lowGrade ) 43 lowGrade = grades[ i ][ j ]; return lowGrade; 46} 47 48/* Find the maximum grade */ 49int maximum( int grades[][ EXAMS ], 50 int pupils, int tests ) 51{ 52 int i, j, highGrade = 0; for ( i = 0; i <= pupils - 1; i++ ) 55 for ( j = 0; j <= tests - 1; j++ ) 56 if ( grades[ i ][ j ] > highGrade ) 57 highGrade = grades[ i ][ j ]; return highGrade; 60} 61 62/* Determine the average grade for a particular exam */ 63double average( int setOfGrades[], int tests ) 64{

BBS514 Structured Programming (Yapısal Programlama)22 65 int i, total = 0; for ( i = 0; i <= tests - 1; i++ ) 68 total += setOfGrades[ i ]; return ( double ) total / tests; 71} 72 73/* Print the array */ 74void printArray( int grades[][ EXAMS ], 75 int pupils, int tests ) 76{ 77 int i, j; printf( " [0] [1] [2] [3]" ); for ( i = 0; i <= pupils - 1; i++ ) { 82 printf( "\nstudentGrades[%d] ", i ); for ( j = 0; j <= tests - 1; j++ ) 85 printf( "%-5d", grades[ i ][ j ] ); 86 } 87}

Program Output BBS514 Structured Programming (Yapısal Programlama)23 The array is: [0] [1] [2] [3] studentGrades[0] studentGrades[1] studentGrades[2] Lowest grade: 68 Highest grade: 96 The average grade for student 0 is The average grade for student 1 is The average grade for student 2 is 81.75