A First Book of ANSI C Fourth Edition

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 10 Introduction to Arrays
An Introduction to Programming with C++ Fifth Edition
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.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
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.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
A First Book of ANSI C Fourth Edition Chapter 8 Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Lesson 12 Arrays Objectives: About the Fibonacci sequence One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study:
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
ICS103 Programming in C Lecture 11: Arrays I
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Computer Programming for Engineers
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
7 Arrays.
CNG 140 C Programming (Lecture set 8)
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
Presentation transcript:

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

Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in data type Also called a scalar variable Data structure (aggregate data type): data type with two main characteristics Its values can be decomposed into individual data elements, each of which is either atomic or another data structure It provides an access scheme for locating individual data elements within the data structure A First Book of ANSI C, Fourth Edition

Introduction (continued) One of the simplest data structures, called an array, is used to store and process a set of values, all of the same data type, that forms a logical group A First Book of ANSI C, Fourth Edition

Introduction (continued) A First Book of ANSI C, Fourth Edition

One-Dimensional Arrays A one-dimensional array, also called a single-dimensional array and a single-subscript array, is a list of values of the same data type that is stored using a single group name A First Book of ANSI C, Fourth Edition

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

One-Dimensional Arrays (continued) To create a one-dimensional array: #define NUMELS 5 int grades[NUMELS]; In C, the starting index value for all arrays is 0 Each item in an array is called an element or component of the array Any element can be accessed by giving the name of the array and the element’s position The position is the element’s index or subscript Each element is called an indexed variable or a subscripted variable A First Book of ANSI C, Fourth Edition

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

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

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

One-Dimensional Arrays (continued) Subscripted variables can be used anywhere scalar variables are valid grades[0] = 98; grades[1] = grades[0] - 11; Any expression that evaluates an integer may be used as a subscript #define NUMELS 5 total = 0; /* initialize total to zero */ for (i = 0; i < NUMELS; i++) total = total + grades[i]; /* add a grade */ A First Book of ANSI C, Fourth Edition

Input and Output of Array Values Individual array elements can be assigned values using individual assignment statements or, interactively, using the scanf() function #define NUMELS 5 for(i = 0; i < NUMELS; i++) { printf("Enter a grade: "); scanf("%d", &grades[i]); } Be careful: C does not check the value of the index being used (called a bounds check) A First Book of ANSI C, Fourth Edition

Input and Output of Array Values (continued) 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

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

Array Initialization The individual elements of all global and static arrays (local or global) are, by default, set to 0 at compilation time The values within auto local arrays are undefined Examples of initializations: int grades[5] = {98, 87, 92, 79, 85}; double length[7] = {8.8, 6.4, 4.9, 11.2}; char codes[6] = {'s', 'a', 'm', 'p', 'l', e'}; char codes[] = {'s', 'a', 'm', 'p', 'l', 'e'}; char codes[] = "sample"; /* size is 7 */ A First Book of ANSI C, Fourth Edition

Array Initialization (continued) 1 #define SIZE1 20 2 #define SIZE2 25 3 #define SIZE3 15 4 5 int gallons[SIZE1]; /* a global array */ 6 static int dist[SIZE2]; /* a static global array */ 7 8 int main() 9 { 10 int miles[SIZE3]; /* an auto local array */ 11 static int course[SIZE3]; /* static local array */ 12 . 13 . 14 return 0; 15 } A First Book of ANSI C, Fourth Edition

Array Initialization (continued) The NULL character, which is the escape sequence \0, is automatically appended to all strings by the C compiler A First Book of ANSI C, Fourth Edition

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

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

Arrays as Function Arguments Individual array elements are passed to a function by including them as subscripted variables in the function call argument list findMin(grades[2], grades[6]); Pass by value When passing a complete array to a function, the called function receives access to the actual array, rather than a copy of the values in the array findMax(grades); Pass by reference A First Book of ANSI C, Fourth Edition

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

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

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

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

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

Requirements Specification Need two functions Determine the average… Determine the standard deviation… …of a list of integer numbers Each function must accept the numbers as an array and return the calculated values to the calling function We now apply the top-down development procedure to developing the required functions A First Book of ANSI C, Fourth Edition

Analyze the Problem Determine the input items: list of integer numbers Determine the desired outputs: (1) average, and (2) standard deviation List the algorithms relating the inputs and outputs: Average Function: Calculate the average by adding the grades and dividing by the # of added grades Standard Deviation Function: Subtract the average from each individual grade. Each number in the new set is called a deviation. Square each deviation found in Step 1. Add the squared deviations and divide the sum by the number of deviations. The square root of the number found in Step 3 is the standard deviation. A First Book of ANSI C, Fourth Edition

Select an Overall Solution Problem-Solver Algorithm is adapted: Initialize an array of integers Call the average function Call the standard deviation function Display the returned value of the average function Display the returned value of the standard deviation function A First Book of ANSI C, Fourth Edition

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

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

Test and Debug the Functions Write a main() program unit to call the function and display the returned results (see Program 8.6) A test run using Program 8.6 produced the following display: The average of the numbers is 76.40 The standard deviation of the numbers is 13.15 Testing is not complete without verifying the calculation at the boundary points Checking the calculation with all of the same values, such as all 0s and all 100s Use five 0s and five 100s A First Book of ANSI C, Fourth Edition

Two-Dimensional Arrays A two-dimensional array, or table, consists of both rows and columns of elements int val[3][4]; A First Book of ANSI C, Fourth Edition

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

Two-Dimensional Arrays (continued) Initialization: #define NUMROWS 3 #define NUMCOLS 4 int val[NUMROWS][NUMCOLS] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} }; The inner braces can be omitted: int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27, 6,14,25,2,10}; Initialization is done in row order A First Book of ANSI C, Fourth Edition

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

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

Two-Dimensional Arrays (continued) The display produced by Program 8.7 is Display of val array by explicit element 8 16 9 52 3 15 27 6 14 25 2 10 Display of val array using a nested for loop A First Book of ANSI C, Fourth Edition

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

Two-Dimensional Arrays (continued) Row size can be omitted A First Book of ANSI C, Fourth Edition

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

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

Internal Array Element Location Algorithm Each element in an array is reached by adding an offset to the starting address of the array Address element i = starting array address + offset For single-dimensional arrays: Offset = i * the size of an individual element For two-dimensional arrays: Offset = column index value * the size of an individual element + row index value * # of bytes in a complete row # of bytes in a complete row = maximum column specification * the size of an individual element A First Book of ANSI C, Fourth Edition

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

Larger Dimensional Arrays A three-dimensional array can be viewed as a book of data tables (the third subscript is called the rank) int response[4][10][6]; A four-dimensional array can be represented as a shelf of books where the fourth dimension is used to declare a desired book on the shelf A five-dimensional array can be viewed as a bookcase filled with books where the fifth dimension refers to a selected shelf in the bookcase Arrays of three, four, five, six, or more dimensions can be viewed as mathematical n-tuples A First Book of ANSI C, Fourth Edition

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

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

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

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 A First Book of ANSI C, Fourth Edition