Today’s Material Arrays Definition Declaration Initialization

Slides:



Advertisements
Similar presentations
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Advertisements

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 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
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 (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
A First Book of ANSI C Fourth Edition
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
ICS103 Programming in C Lecture 11: Arrays I
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.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
EGR 2261 Unit 10 Two-dimensional Arrays
Programming application CC213
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
Lecture 7 Arrays 1. Concept of arrays Array and pointers
CHP-2 ARRAYS.
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
C programming---Arrays
EKT120 : Computer Programming
Arrays Declarations CSCI N305
Numeric Arrays Numeric Arrays Chapter 4.
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Module 2 Arrays and strings – example programs.
JavaScript: Functions.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CNG 140 C Programming (Lecture set 8)
Lecture 10 Arrays.
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
CS111 Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
EKT120: Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Multidimensional array
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Multi-Dimensional Arrays
Dr Tripty Singh Arrays.
INC 161 , CPE 100 Computer Programming
Multi-Dimensional Arrays
Lecture 14: Problems with Lots of Similar Data
Multidimensional Arrays Section 6.4
C++ Array 1.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ICS103 Programming in C Lecture 12: Arrays I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Dr. Khizar Hayat Associate Prof. of Computer Science
ICS103: Programming in C Searching, Sorting, 2D Arrays
Visit for more Learning Resources
Presentation transcript:

Today’s Material Arrays Definition Declaration Initialization Manipulation Multi-dimensional arrays

Multiple Variables of Same Type Sometimes, we need to use a number of variables of the same type, for the same purpose. For example, we may hold each student's grade (an integer value in range 0 - 100) in variables grade1, grade2, grade3, ... This may require many int variables. Program would have to handle these variables one by one. There's a better way: Use "arrays"

Arrays Data structures that hold multiple variables of the same data type The simplest kind of array has just one dimension Elements of a one-dimensional array are conceptually arranged one after another in a single row Declaration: #define N 10 … int A[N]; A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] First index = 0 Last index = N-1 = 9

Array Initialization You can initialize the elements of an array during declaration int A[10]={8, 4, 10, 2, 5, 6, 7, 8, 9, 4}; If the initializer is shorter than the array, remaining elements of the array are filled with 0 int A[10]={1, 2, 3, 4}; /* Initial value of A[10] is {1, 2, 3, 4, 0, 0, 0, 0, 0, 0} */ If an initializer is present, the length of the array may be omitted int A[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; /* A has 10 elements A[0]..A[9] */

Array Manipulation Accessing elements of an array is called indexing or subscripting An index or subscript is the element’s position in the array The elements of an array are allocated contiguously (no gaps) Each individual element of an array is numbered consecutively, starting from 0

Array Manipulation Example: #define MAX_NUM_STUDENTS 5 … int grades[MAX_NUM_STUDENTS]; grades[0] = 98; grades[1] = 87; grades[2] = 92; grades[3] = 79; grades[4] = 85;

  Array Manipulation Warning! C does not perform range-checking of indices (i.e., index values are not explicitly tested for validity with respect to the array size before applying the [ ] operator #define MAX_NUM_STUDENTS 5 … int grades[MAX_NUM_STUDENTS]; grades[53] = 98; grades[5] = 98;  

Array Manipulation It is common to process the elements of an array in a loop, with one element processed per loop iteration The most convenient iteration statement for this is the for statement, since the loop header clearly encapsulates both the initialization and the use of an index variable: grades[0] = 0; grades[1] = 0; grades[2] = 0; grades[3] = 0; grades[4] = 0; int i; for(i = 0; i < MAX_NUM_STUDENTS; i++) grades[i] = 0;

Array Example, 1/3: Read #include<stdio.h> #define SIZE 5 int main(void){ int i; double a[SIZE]; printf(“Enter the %d array elements: ", SIZE); /* Read the array elements */ for(i = 0; i < SIZE; i++) scanf("%lf", &a[i]); return 0; } Enter the 5 array elements: 1.2 3.4 5.6 7.8 9.0

Array Example, 2/3: Print a[0] = 1.20 a[1] = 3.40 a[2] = 5.60 #include<stdio.h> #define SIZE 5 int main(void) { int i; double a[SIZE] = { 1.2, 3.4, 5.6, 7.8, 9.0 }; /* Display array elements */ for(i = 0; i < SIZE; i++) printf("a[%d] = %.2lf\n", i, a[i]); return 0; } a[0] = 1.20 a[1] = 3.40 a[2] = 5.60 a[3] = 7.80 a[4] = 9.00

Array Example, 3/3: Max max = 9.00 #include<stdio.h> #define SIZE 5 int main(void) { int i; double a[SIZE] = { 1.2, 3.4, 5.6, 7.8, 9.0 }; double max = 0.0; /* Find max value in array */ for(i = 0; i < SIZE; i++) if (a[i] > max) max = a[i]; printf("max = %.2lf\n", max); return 0; } max = 9.00

Array Example, Combined #include<stdio.h> #define SIZE 5 int main(void) { int i; double a[SIZE]; double max = 0.0; printf("Enter the %d array elements\n", SIZE); for(i = 0; i < SIZE; i++) /* Read the array elements */ scanf("%lf", &a[i]); for(i = 0; i < SIZE; i++) /* Display array elements */ printf("a[%d] = %.2lf\n", i, a[i]); for(i = 0; i < SIZE; i++) /* Find max value in array */ if (a[i] > max) max = a[i]; printf("max = %.2lf\n", max); return 0; }

Arrays as Function Parameters(1) A function can take an array as a parameter. We specify this as follows: int Sum(int A[], int n){ int i; int sum = 0; for (i=0; i<n; i++) sum += A[i]; return sum; } int main(){ int B[5]={3, 5, 1, 2, 3}; printf(“Total: %d\n”, Sum(B, 5)); return 0;

Arrays as Function Parameters(2) Changing the elements of an array parameter changes the values of the array argument int Junk(int A[]){ A[0] = 8; A[1] = 9; A[2] = 10; } /* end-Junk */ int main(){ int B[4]={3, 5, 1, 2}; Junk(B); printf(“B[0]:%d, B[1]: %d, B[2]: %d, B[3]: %d\n”, B[0], B[1], B[2], B[3]); return 0; } /* end-main */

Multi-Dimensional Arrays An array may have any number of dimensions E.g., 2-dimensional array (a matrix) is declared as follows int M[5][9]; /* Has 5 rows and 9 columns */ Conceptually, array M looks like this: 1 2 3 4 5 6 7 8 To access an element in row i, column j, we write M[i][j] M[i] selects ith row, M[i][j] selects jth column M[i,j] is pascal style. In C, M[i,j] is the same as M[j] Recall that comma(,) is an operator in C and evaluates to the last expression (j) in this example

Accessing 2-dimensional Arrays /* Initialization */ for (i=0; i<5; i++){ for (j=0; j<9; j++){ M[i][j] = 0; } /* Summation */ sum = 0; for (i=0; i<5; i++){ for (j=0; j<9; j++){ sum += M[i][j]; } /* Find min & max */ min = M[0][0]; Max = M[0][0]; for (i=0; i<5; i++){ for (j=0; j<9; j++){ if (M[i][j]<min) min=M[i][j]; if (M[i][j]>max) max=M[i][j]; } printf(“min: %d, max: %d\n”, min, max);

Initializing Multi-Dimensional Arrays You can create an initializer for a two-dimensional array by nesting one-dimensional initializers int M[5][9] = { {1, 1, 1, 1, 0, 1, 1, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 0, 1, 1, 1, 0, 0, 1}, {0, 0, 0, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 0, 0, 1, 1}}; If the initializer is not long enough to fill a multi-dimensional array, the remaining elements are given the value 0 int M[5][9] = { {1, 1, 2, 1, 2, 0, 0, 1, 1}, {0, 0, 0, 1, 1, 1, 1, 2, 1}}; /* Rows 2, 3 and 4 will be set to 0 */

Initializing Multi-Dimensional Arrays If an inner list is not long enough to fill a row, the remaining elements in the row are set to 0 int M[5][9] = { {1, 1, 0, 0, 1, 1, 1, 1, 1}, {0, 1, 1, 2, 1, 1}, {1, 1, 2, 2, 3}}; /* M[1][6], M[1][7], M[1][8] are set to 0 */ /* M[2][5], M[2][6], M[2][7], M[2][8] are set to 0 */ /* All elements of row 3 and 4 are set to 0 */

Higher-Dimensional Arrays An array can have any number of dimensions int Cube[8][8][8]; /* A cube of size 8 */ int Prism[4][6][10]; /* A rectangular prism of size * 4x6x10 */ /* Can have initializer */ float A[4][6][8] = {{{1, 2, 3}, {3, 4}}, {{3, 4}}}; Cube[2][3][4] = 2; Prism[3][5][8] = 6; A[0][0][4] = 3.34;

Multi-Dimensional Arrays as Function Parameters We can also have multi-dimensional arrays as function parameters /* Must specify the 2nd dimension size, i.e., 4 */ int Sum2(int A[][4], int n){ int i, j, sum = 0; for (i=0; i<n; i++) for (j=0; j<4; j++) sum += A[i][j]; return sum; } /* end-Sum2 */ This is very inconvenient though. All arguments to Sum2 must have 2nd dimension equal to 4!!