V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

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

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Array Session 9

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.

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

How to declare an array in C?

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Introduction to Computer Science Midterm 3 Fall, Points

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

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

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

Programming for Engineers Arrays

Chapter 9 Introduction to Arrays. Fundamentals of Java

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

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

Multiple-Subscripted Arrays

(8-1) Arrays I H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 8, 2018) Washington State University

Array Initialization

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

UNIT-I Fundamental Notations

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

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

Computer Programming: C++

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Arrays in Java Multi-dimensional Arrays

Computer Science & Engineering 150A Problem Solving Using Computers

Pointers and Arrays 1

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

Arrays and Strings. Arash Rafiey. September 12, 2017

C++ PROGRAMMING SKILLS Part 4: Arrays

Chapter 6: Using Arrays

Arrays. Theoretical Part. Contents. Keywords. Programming with Java module 3

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

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

Session 8.2. Finding Winners Using Arrays

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

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

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

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

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

BITG 1113: Array (Part 1) LECTURE 8

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

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

C How to Program, 6/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!

Lexical Considerations

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Programming for Electrical and Computer Engineers. Pointers and Arrays

Functions, Arrays & Structs

Programming Logic and Design Sixth Edition

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

TOPICS TO COVER:-- Array declaration and use.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

6. Control Statements II

Arrays and Collections. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

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

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

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

Lexical Considerations

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Computer Programming

Array. Prepared By - Rifat Shahriyar

Chapter 7 : Arrays (pp )

Chapter 6. Data Types

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

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

Syntax Syntax for file I/O error detection system function (not in Annex A)

Procedures, Parameters, Values and Variables. Steven R. Bagley

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

1 Lexical Considerations

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Arrays. What if you have a 1000 line file? Arrays

Procedural Programming

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

INTRODUCTION 1 AND REVIEW

Review of Important Topics in CS1600. Functions Arrays C-strings

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

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program

UNIT 1. Introduction to Microsoft.NET framework and Basics of VB.Net

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

The Warhol Language Reference Manual

Lecture 04 FUNCTIONS AND ARRAYS

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Chapter 8 :: Composite Types

Arrays. Arizona State University 1

Boolean Algebra Boolean Algebra

Computers Programming Course 10. Iulian Năstac

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

Transcription:

Programming in C 1 Introduction to Arrays A collection of variable data Same name Same type Contiguous block of memory Can manipulate or use Individual variables or List as one entity 2 Celsius temperatures: I ll name it c. Type is int. Introduction to Arrays Used for lists of like items Scores, speeds, weights, etc. Avoids declaring multiple simple variables Used when we need to keep lots of values in memory Sorting Determining the number of scores above/below the mean Printing values in the reverse order of reading Etc. 3 111 Ch 07 A 1

Declaring Arrays General Format for declaring arrays <data type> <variable> [<size>]; Declaration Declaring the array allocates memory Static entity - same size throughout program Examples Type is int. Name is c. 4 Defined Constant as Array Size Use defined/named constant for array size Improves readability Improves versatility Improves maintainability Examples: 5 Powerful Storage Mechanism Can perform subtasks like: "Do this to i-th indexed variable" where i is computed by program "Fill elements of array scores from user input" "Display all elements of array scores Sort array scores in order Determine the sum or average score "Find highest value in array scores" "Find lowest value in array scores" 6 111 Ch 07 A 2

Accessing Array Elements Individual parts called many things: Elements of the array Indexed or subscripted variables To refer to an element: Array name and subscript or index Format: arrayname[subscript] Zero based c[0] refers to c 0, c sub zero, the first element of array c 7 Accessing Array Elements Example Note two uses of brackets: In declaration, specifies SIZE of array Anywhere else, specifies a subscript/index 8 Accessing Array Elements Example Given the declaration We reference elements of scores by scores [0] scores [1] subscript/index scores [11] 9 111 Ch 07 A 3

Accessing Array Elements Size, subscript need not be literal constant Can be named constant or expression 10 Major Array Pitfall Array indexes go from 0 through size-1! C will 'let' you go out of the array s bounds Unpredictable results may get segmentation fault Compiler will not detect these errors! Up to programmer to 'stay in bounds?? 11 for-loops with Arrays Natural counting loop Naturally works well 'counting thru' elements of an array General form for forward direction for (subscript = 0; subscript < size; subscript++) General form for reverse direction for (subscript = size-1; subscript >= 0; subscript--) 12 111 Ch 07 A 4

for-loops with Arrays Examples 13 Score 1 is 56 Score 2 is 52 Score 3 is 80 Score 4 is 74... Score 12 is 87 Score 12 is 87 Score 11 is 97 Score 10 is 86 Score 9 is 80... Score 1 is 56 Uses of Defined Constant Use everywhere size of array is needed In for-loop for traversal: In calculations involving size: When passing array a function: 14 Array as Function Parameter Include type and brackets [] Size inside brackets is optional and is ignored Passes pointer/reference to array Function can modify array elements Common to also pass size Example: 15 111 Ch 07 A 5

Initializing Arrays Arrays can be initialized at declaration Size cannot be variable or named constant Equivalent to 16 Auto-Initializing Arrays If fewer values than size supplied: Fills from beginning Fills 'rest' with zero of array base type Declaration Performs initialization 17 Auto-Initializing Arrays If array size is left out Declares array with size required based on number of initialization values Example: Allocates array scores with size of 3 18 111 Ch 07 A 6

Multidimensional Arrays Arrays with more than one dimension Declaration: Additional sizes each enclosed in brackets Two dimensions Table or array of arrays Requires two subscripts row and column 19 Initializing Multidimensional Nested lists Unspecified values set to zero 2D Example: 20 Three-dimensional Visualization 21 111 Ch 07 A 7

Multidimensional Array Parameters Must specify size after first dimension 22 Programming in C T H E E N D 23 111 Ch 07 A 8