A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Similar documents
A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Chapter 7 : Arrays (pp )

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Programming for Engineers Arrays

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Unit 7. Functions. Need of User Defined Functions

ONE DIMENSIONAL ARRAYS

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Computer Science & Engineering 150A Problem Solving Using Computers

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Chapter 7 Array. Array. C++, How to Program

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Lecture 04 FUNCTIONS AND ARRAYS

How to declare an array in C?

Unit 3 Decision making, Looping and Arrays

Lesson 7. Reading and Writing a.k.a. Input and Output

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Array. Arrays. Declaring Arrays. Using Arrays

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Single Dimension Arrays

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

Multiple-Subscripted Arrays

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

CSCI 171 Chapter Outlines

C++ PROGRAMMING SKILLS Part 4: Arrays

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Model Viva Questions for Programming in C lab

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Array Initialization

C Programming

Chapter 7 Functions. Now consider a more advanced example:

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

TOPICS TO COVER:-- Array declaration and use.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Arrays (Deitel chapter 7)

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

ARRAYS(II Unit Part II)

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

One Dimension Arrays 1

Chapter 3 Basic Data Types. Lecture 3 1

C: How to Program. Week /Apr/23

Introduction to Scientific Computing and Problem Solving

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Procedural Programming

Arrays and Pointers (part 1)

Chapter 3: Arrays and More C Functionality

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Functions. Introduction :

Input/Output Week 5:Lesson 16.1

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

Chapter 9 Introduction to Arrays. Fundamentals of Java

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

C Programming Review CSC 4320/6320

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

Engineering program development 6. Edited by Péter Vass

A First Book of ANSI C Fourth Edition. Chapter 12 Structures

FORM 2 (Please put your name and form # on the scantron!!!!)

EC312 Chapter 4: Arrays and Strings

INTRODUCTION 1 AND REVIEW

Principles of Programming. Chapter 6: Arrays

Why arrays? To group distinct variables of the same type under a single name.

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Arrays. Systems Programming Concepts

tablica: array: dane_liczbowe

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

EM108 Software Development for Engineers

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Binghamton University. CS-220 Spring Arrays in C

Write a C program using arrays and structure

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

PROGRAMMAZIONE I A.A. 2017/2018

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

Advanced C Programming Topics

Transcription:

A First Book of ANSI C Fourth Edition Chapter 8 Arrays

Objectives One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional Arrays Common Programming and Compiler Errors A First Book of ANSI C, Fourth Edition 2

Chapter 8 Arrays The variables used so far, scalar variables, each variable can only be used to store a single value at a time or atomic type. This means that the value cannot be further subdivided or separated into a legitimate data type Frequently we may have a set of values, all of the same data type, or called a singledimensional array, that form a logical group. A First Book of ANSI C, Fourth Edition 3

8.1 One Dimensional Arrays A one-dimensional array is a list of values of the same data type. int grades[5] ; More Examples char code[4] ; // an array of four character codes double prices[6] ; // an array of six double precision prices float amount [100] ; //an array of 100 floating point amounts A First Book of ANSI C, Fourth Edition 4

Examples using the elements of the grades array are grades [0] = 98 ; grades [1] = grades [0] 11 grades [2] = 2 * ( grades [0] 6 ); grades [3] = 79 ; grades [4] = (grades [2] + grades [3] 3 ) / 2 ; total = grades [0] + grades [1] + grades [2] + grades [3] + grades [4] ; How to use for loop for adding five grade values? One extremely important advantage of using integer expressions as subscripts is that it allows sequencing through an array using a for loop. A First Book of ANSI C, Fourth Edition 5

Find maximum value When an element with a higher value is located, that element becomes the new maximum. maximum = price[0]; for (i = 1; i <= 999; i++) if (price[i] > maximum) maximum = price[i]; A First Book of ANSI C, Fourth Edition 6

Input and Output of Array Values Individual array elements can be assigned values using individual assignment statements or, interactively, using the scanf( ) function. Examples of individual data entry statements are price[5] = 10.69 ; scanf ( %d %lf, &grades[0], &price[2] ); scanf ( %c, &code[0] ); scanf ( %d %d %d, &grades[0], &grades[1], &grades[2]); A First Book of ANSI C, Fourth Edition 7

Alternatively, a for statement can be used to cycle through the array for interactive data input. For example, the code for (i = 0 ; i <= 4 ; ++i ) { printf ( Enter a grade : ); scanf ( %d, &grade[i] ); } prompts the user for five grades. The first grade entered is stored in grades[0], the second in grades[1], and so on until all five grades are entered. A First Book of ANSI C, Fourth Edition 8

One caution should be mentioned about storing data in an array. C does not check the value of the index being used. If an array has been declared as consisting of 10 elements, for example, and you use an index of 12, which is outside the bounds of the array, C will not notify you of the error when the program is compiled. A First Book of ANSI C, Fourth Edition 9

During output, individual array elements can be displayed using the printf( ) function or complete sections of the array can be displayed by including a printf( ) function call within a for loop. Examples of this are printf ( %lf, price[6] ); printf ( The value of element %d is %d, i, grades[i] ); for ( n = 5 ; n <= 20 ; ++n ) printf ( %d %lf, n, price[n] ); A First Book of ANSI C, Fourth Edition 10

Example of Input and Output of Array Values Sample output: Enter a grade: 85 Enter a grade: 90 Enter a grade: 78 Enter a grade: 75 Enter a grade: 92 grades 0 is 85 grades 1 is 90 grades 2 is 78 grades 3 is 75 grades 4 is 92 A First Book of ANSI C, Fourth Edition 11

Example of Input and Output of Array Values (continued) Statement is outside of the second for loop; total is displayed only once, after all values have been added A First Book of ANSI C, Fourth Edition 12

8.2 Array Initialization Arrays, like scalar variables, can be declared either inside or outside a function. Arrays declared inside a function are local arrays, and arrays declared outside a function are global arrays. A First Book of ANSI C, Fourth Edition 13

Array Initialization (continued) #define SIZE1 20 #define SIZE2 25 #define SIZE3 15 int gallons[size1]; /* a global array */ static int dist[size2]; /* a static global array */ int main() { int miles[size3]; /* an auto local array */ static int course[size3]; /* static local array */ } return 0; A First Book of ANSI C, Fourth Edition 14

Array elements can be initialized within their declaration statements in the same manner as scalar variables, except that the initializing elements must be included in braces {}. Examples of such initializations for automatic, static, and global arrays are int grades[5] = { 98, 87, 92, 79, 85 }; char codes[6] = { s, a, m, p, l, e }; double width[7] = { 10.96, 6.43, 2.58,.86, 5.89, 7.56, 8.22} ; static int temp[4] = {10, 20, 30, 40 } ; static float temp[4] = { 98.6, 97.2, 99.0, 101.5} ; A First Book of ANSI C, Fourth Edition 15

If the number of initializers is less than the declared number of elements listed in square brackets the initializers are applied starting with array element 0. Thus, in the declaration float length[7] = { 7.8, 6.4, 4.9, 11.2 }; only length[0], length[1], length[2] and length[3] are initialized with the listed values. The other array elements will be initialized to zero. A First Book of ANSI C, Fourth Edition 16

A unique feature of initializers is that the size of an array may be omitted when initializing values are included in the declaration statement. For example, the declaration int gallons[ ] = { 16, 12, 10, 14, 11 } ; reserves enough storage room for five elements. Similarly, the following two declarations are equivalent : char codes[6] = { s, a, m, p, l, e }; char codes[ ] = { s, a, m, p, l, e }; A First Book of ANSI C, Fourth Edition 17

An interesting and useful simplification can also be used when initializing character arrays. For example, the declaration char codes[ ] = sample ; // no braces or commas uses the string sample to initialize the codes array. \0, is called the null character The null character is automatically appended to all strings by the C compiler. A First Book of ANSI C, Fourth Edition 18

Array Initialization (continued) A First Book of ANSI C, Fourth Edition 19

8.3 Arrays as Function Arguments Individual array elements are passed to a function by simply including them as subscripted variables in the function call argument list. For example, the function call findmin(grades[2], grades[6] ) ; passes the values of the elements grades[2] and grades[6] to the function findmin ( ). Pass by Value / Pass by Reference? A First Book of ANSI C, Fourth Edition 20

Arrays as Function Arguments (continued) Size can be omitted A First Book of ANSI C, Fourth Edition 21

Arrays as Function Arguments (continued) A First Book of ANSI C, Fourth Edition 22

Case Study: Computing Averages and Standard Deviations Two statistical functions are created to determine the average and standard deviation, respectively, of an array of numbers A First Book of ANSI C, Fourth Edition 23

Write the Functions A First Book of ANSI C, Fourth Edition 24

Write the Functions (continued) A First Book of ANSI C, Fourth Edition 25

8.5 Two-Dimensional Arrays A two-dimensional array, which is sometimes referred to as a table, consists of both rows and columns of elements. For example, the array of numbers 8 16 9 52 3 15 27 6 int val[3][4]; 14 25 2 10 is a two-dimensional array of integers. A First Book of ANSI C, Fourth Edition 26

Two-Dimensional Arrays (continued) A First Book of ANSI C, Fourth Edition 27

The declaration of 2D Array int val[3][4] = { { 8, 16, 9, 52 }, { 3, 15, 27, 6 }, { 14, 25, 2, 10 } } ; declares val to be an array of integers with three rows and four columns, with the initial values given in the declaration. The first set of internal braces contains the values for row 0 of the array, the second set of internal braces contains the values for row 1, and the third set of braces the values for row 2. A First Book of ANSI C, Fourth Edition 28

Two-Dimensional Arrays (continued) is similar int val[3][4] = { { 8, 16, 9, 52 }, { 3, 15, 27, 6 }, { 14, 25, 2, 10 } } ; A First Book of ANSI C, Fourth Edition 29

Two-Dimensional Arrays (continued) A First Book of ANSI C, Fourth Edition 30

Two-Dimensional Arrays (continued) Row size can be omitted How it is different? void display (int nums[][cols] A First Book of ANSI C, Fourth Edition 31

Internal Array Element Location Algorithm (continued) A First Book of ANSI C, Fourth Edition 32

Larger Dimensional Arrays A three-dimensional array can be viewed as a book of data tables. 3-D array of size [2][3][4] Leftmost subscript i = 0 k =0 k =1 Rightmost subscript j = 0 j = 1 j = 2 j = 3 i = 1 i = 2 Middle subscript by Reddy & Ziegler 33

Memory Location a[0][0][0] a[0][0][1] a[0][0][2] a[0][0][3] a[0][1][0] a[0][1][1] a[0][1][2] a[0][1][3] a[0][2][0] a[0][2][1] a[0][2][2] a[0][2][3] a[1][0][0] a[1][0][1] a[1][0][2] a[1][0][3] a[1][1][0] a[1][1][1] a[1][1][2] a[1][1][3] a[1][2][0] a[1][2][1] a[1][2][2] a[1][2][3] by Reddy & Ziegler 34

Declaration and Storage Allocation Declaration of three-dimensional arrays requires three indices: plane, row, and column. On each plane, the row and column indices are the same and the plane index varies from one plane to the next. The plane index varies from 0 to one less than the number of planes, the row index varies from 0 to one less than the number of rows, and the column index varies from 0 to one less than the number of columns. by Reddy & Ziegler 35

by Reddy & Ziegler 36

Input of Three-Dimensional Arrays by Reddy & Ziegler 37

Output of Three-Dimensional Arrays by Reddy & Ziegler 38

Manipulation of Three-Dimensional Arrays by Reddy & Ziegler 39

by Reddy & Ziegler 40

by Reddy & Ziegler 41

Common Programming Errors Forgetting to declare the array Using a subscript that references a nonexistent array element Not using a large enough conditional value in a for loop counter to cycle through all the array elements Forgetting to initialize the array A First Book of ANSI C, Fourth Edition 42

Common Compiler Errors A First Book of ANSI C, Fourth Edition 43

Summary A single-dimensional array is a data structure that can store a list of values of the same data type Elements are stored in contiguous locations Referenced using the array name and a subscript Single-dimensional arrays may be initialized when they are declared Single-dimensional arrays are passed to a function by passing the name of the array as an argument A First Book of ANSI C, Fourth Edition 44

Summary (continued) A two-dimensional array is declared by listing both a row and a column size with the data type and name of the array Two-dimensional arrays may be initialized when they are declared Two-dimensional arrays are passed to a function by passing the name of the array as an argument Three-dimensional arrays (by Reddy & Ziegler) A First Book of ANSI C, Fourth Edition 45