C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana.
Advertisements

Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 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.
Multiple-Subscripted Array
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Systems Programming Concepts
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2007 Pearson Education, Inc. All rights reserved C Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
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.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming Lecture 14 Arrays – Part I 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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays Declarations CSCI N305
C Arrays.
C Arrays Systems Programming.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
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
CS-161 Computer Programming Lecture 14: Arrays I
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
6 C Arrays.
7 Arrays.
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.
Lecture 14: Problems with Lots of Similar Data
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
4.1 Introduction Arrays A few types Structures of related data items
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Presentation transcript:

C Arrays Systems Programming

Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range Example  Passing Arrays to Functions –Call by Reference  Multiple-Subscripted Arrays –Double-Subscripted Array Example

Systems Programming: Arrays 3 ArraysArrays Arrays :: Structures of related data items – constant –Static entity, namely, the size of an array must be a constant throughout the program. – –A group of consecutive memory locations with the same name and type.  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 4 ArraysArrays   To refer to an array element, specify – –the array name – –the position number {in C this is an offset}   Format: arrayname [ position number ] – –First element at position 0 – –n element array named c: c[ 0 ], c[ 1 ]...c[ n – 1 ] c[ 0 ], c[ 1 ]...c[ n – 1 ]  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 5 Fig element array  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 6 ArraysArrays   Array elements are like normal variables. c[ 0 ] = 3; printf( "%d", c[ 0 ] );   One can perform operations within the subscript. Examples: c[ x- 2 ] = 6; c[i+j- 4] = c[x-2]; c[i+j- 4] = c[x-2]; c[intfcn(i)] = 0; c[intfcn(i)] = 0;  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays Defining Arrays   When defining arrays, specify – –Name – –Type of array – –Number of elements arrayType arrayName[ numberOfElements ]; – –Examples: int c[ 10 ]; float myArray[ 3284 ];   Defining multiple arrays of same type – –Format similar to regular variables – –Example: int b[ 100 ], x[ 27 ];  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 8 Initializing Array  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 initializers, a syntax error occurs –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  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 9 Fig. 6.5 Array Example #define directive tells compiler to replace all instances of the word SIZE with 10 SIZE is replaced with 10 by the compiler, so array s has 10 elements  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 10 ArraysArrays Dangerous in C because: – –There is no default initialization. – –There is no bounds checking for subscripts out-of-range.

Systems Programming: Arrays 11 ‘Scary’ Out-of-Range Example /* Bizarre Example of subscripting out of range */   int main () { int i, m, n, j; int a[100], k, p; j = 77; k = 88; p = 99; for (i= -1; i<=103; i++) { a[i] = 2*i; if (i > 98) printf("i =%d, a[i] = %d\n", i, a[i]); } printf("j = %d, n = %d, m = %d, i = %d, k = %d, p = %d\n", j, n, m, i, k, p); printf("%d %d %d %d %u\n", a[-1], a[99], a[100], a[102], a[i]); return 0; } p a[-2] k a[-1] a[0] a[1] a[99] j a[100] n a[101] m a[102] i a[103] i =99, a[i] = 198 i =100, a[i] = 200 i =101, a[i] = 202 i =102, a[i] = 204 i =206, a[i] = 15 j = 200, n = 202, m = 204, i = 207, k = -2, p =

Systems Programming: Arrays Passing Arrays to Functions   To pass an array argument to a function, specify the name of the array without any brackets.   The array size is usually passed to the function. int myArray[ 24 ]; myFunction( myArray, 24 );  by-reference  Arrays are passed by-reference. – –The name of the array is associated with the address of the first array element. – –The function knows where the array is stored and it can m odify the original memory locations.  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays Passing Arrays to Functions   Individual array elements –by value –Are passed by value. – myArray[ 3 ] ) –Pass the subscripted name (i.e., myArray[ 3 ] ) to function.   Function prototype void modifyArray( int b[], int arraySize ); – –Parameter names are optional in prototype. int b[]int []int b[] could be written int [] int arraySizeintint arraySize could be simply int  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 14 Passing Arrays to Functions /* Arrays are passed using Call by Reference */ #include #define SIZE 6 void flip (float fray [], int fsize) { float temp; int i,j; i = fsize - 1; for (j = 0; j < fsize/2 ; j++) { temp = fray[j]; fray[j] = fray[i]; fray[i] = temp; i--; } return; }

Systems Programming: Arrays 15 Passing Arrays to Functions int main () { float var[SIZE]; int i,j; for (i=0; i < SIZE; i++) { var[i] = 1.0/pow (2.0,i); printf(" %5.3f", var[i]); } printf("\n"); for (j=0; j < 2; j++) { flip (var, SIZE); for (i=0; i < SIZE; i++) printf(" %5.3f", var[i]); printf("\n"); } $./passray

Systems Programming: Arrays Multiple-Subscripted Arrays   Multiple subscripted arrays – –Tables with rows and columns ( m by n array) – –Like matrices: specify row, then column   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 ] );  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 17 Fig Double-Subscripted array  2007 Pearson Ed -All rights reserved.   Three rows and four columns.

Systems Programming: Arrays 18 Double-Subscripted Array Example Each row in the array corresponds to a single student’s set of grades  2007 Pearson Ed -All rights reserved.

Systems Programming: Arrays 19 average function is passed a row of the array  2007 Pearson Ed -All rights reserved. Double-Subscripted Array Example

Systems Programming: Arrays 20 Double-Subscripted Array Example

Systems Programming: Arrays 21  2007 Pearson Ed -All rights reserved. Double-Subscripted Array Example

Systems Programming: Arrays 22  2007 Pearson Ed -All rights reserved. Double-Subscripted Array Example

Systems Programming: Arrays 23  2007 Pearson Ed -All rights reserved. Double-Subscripted Array Example

Systems Programming: Arrays 24 An enum and switch Example /* A program that uses enumerated types, switch and a sentinel to terminate input */ #define SENTINEL 10 int main () { int day; /* enum starts assigning positional integers beginning with 0 */ enum days {SUN, MON, TUES, WED, THUR, FRI, SAT}; scanf("%d", &day); while( day != SENTINEL) { switch (day) { case MON: case WED: case FRI: printf("%d - Go to class\n", day); break;

Systems Programming: Arrays 25 An enum and switch Example case TUES: printf("%d - Sleep in until 10\n", day); break; case THUR: printf("%d - Do laundry\n", day); break; case SAT: printf("%d - Go to gym. ", day); printf("Go out to a movie\n"); break; case SUN: printf("%d - Study lots!\n", day); break; default: printf("%d - This invalid input. Try again.", day); break; } scanf("%d", &day); } printf("Sentinal encountered.\n"); return 0; }