Chapter 8. Arrays The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Size: px
Start display at page:

Download "Chapter 8. Arrays The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill"

Transcription

1 Chapter 8 Arrays McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved.

2 Chapter Objectives Establish an array and refer to individual elements in the array with subscripts Use a foreach loop to traverse the elements of an array Create a structure for multiple fields of related data Accumulate totals using arrays Distinguish between direct access and indirect access of a table Write a table lookup for matching an array element Combine the advantages of list box controls with arrays Store and look up data in multidimensional arrays McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-2

3 Single-Dimension Arrays A list or series of values similar to a list of values for list boxes and combo boxes without the box Use an array to store a series of variables for later processing May be referred to as tables or subscripted (or indexed) variables Individual variables (elements) are treated the same as any other variable and may be used in any statement McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-3

4 Subscripts Advantage of an array is using variables for subscripts in place of constants Subscripts are constants, variables, or numeric expressions Must be integers Array name and number of elements are declared No limit to the number of elements (except for available memory) McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-4

5 The Declaration Statement for Arrays General Form - 1 Declare arrays as public or private Defaults to private Location of declaration determines scope and lifetime of array variables Declare an array by placing opening and closing square brackets after the data type Declare the number of elements or the initial values of array elements (which determine the number of elements) McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-5

6 The Declaration Statement for Arrays General Form - 2 Datatype[] arrayname = new Datatype[NumberOfElements]; Datatype[] arrayname = new Datatype[] {InitialValueList}; Datatype[] arrayname = {InitialValueList}; [public private] Datatype[] arrayname = new Datatype[NumberOfElements]; McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-6

7 The Declaration Statement for Arrays Examples Array subscripts are zero based First element is always element zero All array elements must be the same data type string[] namestring = new string[25]; decimal[] balancedecimal = new decimal[10]; int[] numbersinteger = new int[] {1, 5, 12, 18, 20}; string[] departmentsstring = new string[] {"Accounting", "Marketing", "Human Relations"}; private string[] categorystring = new string[10]; public string[] IDNumbersString = new string[5]; string[] namestring = {"Sean", "Sam", "Sally", "Sara"}; McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-7

8 Valid Subscripts A subscript must reference a valid element of an array Zero through one less than the number of elements C# rounds fractional subscripts C# throws an exception for a subscript that is out of range Arrays are based on the System.Array collection McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-8

9 foreach Loops - 1 To reference each element of an array use for loops or foreach loops Advantages of a foreach loop The subscripts of the array do not have to be manipulated How many elements there are in an array does not have to be known Array elements are read-only in the body of a foreach loop C# automatically references each element of the array and assigns its value to ElementName Makes one pass through the loop per element The variable used for ElementName must be the same data type as array elements or an Object data type McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-9

10 foreach Loops - 2 Best to declare the variable for ElementName as part of the foreach statement to create a blocklevel variable A foreach loop will execute if the array has at least one element Loop continues to execute until all elements are processed Execution of code continues with the line following the closing braces when the loop finishes You can use a break statement to exit the loop early McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-10

11 General Form foreach Loops - 3 foreach (DataType ElementName in ArrayName) { // Statement(s) in the loop. } Examples foreach (string onenamestring in namestring) { Console.WriteLine(oneNameString); // Write one element of the array. } foreach (decimal oneitemdecimal in allitemsdecimal) // Display all elements in the array. { totalsrichtextbox.text += indexinteger++ + "\t" + totaldecimal.tostring() + "\n"; } McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-11

12 Structures Combine multiple fields of data to create a new structure using the struct statement Similar to defining a new data type struct declaration Cannot go inside a method Generally placed at the top of a file with the classlevel declarations Can be placed outside of a class Public by default; can declare as private The number of elements of an array inside a structure cannot be specified McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-12

13 The struct Statement - Examples struct Employee { public string lastnamestring; public string firstnamestring; public string SSNString; public string streetstring; public string statestring; public string ZIPString; public DateTime hiredatetime; public int paycodeinteger; } public struct Product { public string descriptionstring; public string IDString; public int quantityinteger; public decimal pricedecimal; } struct SalesDetail { public decimal[] saledecimal; } McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-13

14 Declaring Variables Based on a Structure After you declare a structure, declare variables of the structure, just as if it were a data type The location of the variable declaration determines the scope and lifetime of the variable Employee officeemployee; Employee warehouseemployee; Product widgetproduct; Product inventoryproduct[] = new Product[100]; SalesDetail housewaressalesdetail; SalesDetail homefurnishingssalesdetail; McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-14

15 Accessing the Elements in a Structure Variable - 1 Each field of data in a structure is referred to as an element To access elements, use the dot notation similar to that used for objects A variable that is not an array does not need an index A variable that is an array needs the item and the element within the array of structures specified McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-15

16 Accessing the Elements in a Structure Variable - 2 Examples officeemployee.lastnamestring officeemployee.hiredatetime warehouseemployee.lastnamestring widgetproduct.descriptionstring widgetproduct.quantityinteger widgetproduct.pricedecimal inventoryproduct[indexinteger].descriptionstring inventoryproduct[indexinteger].quantityinteger inventoryproduct[indexinteger].pricedecimal McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-16

17 Including an Array in a Structure An array can be included in a structure, but you cannot specify the number of elements in the struct declaration Public struct SalesDetail //Class-level declarations. { public decimal[] saledecimal; } At the class level or inside a method, declare the name of the variable SalesDetail housewaressalesdetail; Inside a method establish the number of elements in the array housewaressalesdetail.saledecimal = new decimal[7]; In processing, use a subscript to refer to each individual element within the structure housewaressalesdetail.saledecimal[dayindexinteger] = todayssalesdecimal; McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-17

18 Using Array Elements for Accumulators Array elements are regular variables Perform the same way as all variables used so far Can be used for counters or total accumulators Eight totals will be accumulated to demonstrate the use of array elements as total accumulators McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-18

19 Adding to the Correct Total Use the totaldecimal array to hold the sales totals for eight scout troops User enters the group number and sales Subtract one from the group number and use it as a subscript for the correct total Add the sales amount to the corresponding total This technique is called direct reference of the array // Convert input group number to subscript. groupnumberinteger = int.parse(grouptextbox.text) 1; // Add sale to the correct total. saledecimal = decimal.parse(saletextbox.text); totaldecimal[groupnumberinteger] += saledecimal; McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-19

20 Debugging Array Programs - 1 View array elements in debugging time by setting a breakpoint in code and viewing the Autos or Locals window Click the plus sign to the left of the array name to view the individual array elements McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-20

21 Debugging Array Programs - 2 Visual Studio 2008 introduces visualizers Pop up the value of an array in the Code Editor window Pause the mouse pointer over a variable or array Array name pops up, point to plus sign to display current contents of each element McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-21

22 Table Lookup - 1 Values used to identify a series of elements may not be sequential Establish a structure and declare an array of the structure In the Form_Load event handler, initialize the array elements Use a table lookup to find the correct array element, the subscript Compare the input group number to each element in the table, one by one When a match is found, add to the corresponding total McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-22

23 Table Lookup - 2 public struct GroupInfo { public string groupnumberstring; public decimal totaldecimal; } public GroupInfo[] arraygroup = new GroupInfo[8]; indexinteger 2 groupnumber -String [0] [1] [2] [3] [4] [5] [6] [7] totaldecimal McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-23

24 Table Lookup - 3 UML action diagram of the logic of a lookup operation McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-24

25 Coding a Table Lookup - 1 do loop works better than a foreach loop Logic of a lookup operation // Convert input group number to a subscript. do { if (grouptextbox.text == arraygroup[groupnumberinteger].groupnumberstring) { // A match is found. decimal saledecimal = decimal.parse(salestextbox.text); arraygroup[groupnumberinteger].totaldecimal += saledecimal; foundboolean = true; // Clear the controls. grouptextbox.clear(); salestextbox.clear(); grouptextbox.focus(); } groupnumberinteger++; } while (groupnumberinteger < 8 &&!foundboolean); McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-25

26 Coding a Table Lookup - 2 Validate group number entry If wrong, display a message box Check foundboolean after loop completes to determine why loop terminated Did it terminate because of a match or without a match Table lookup works for any table, numeric or string Searched fields do not have to be in any sequence McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-26

27 Using List Boxes with Arrays Use a list box or combo box rather than a text box for user input Use the list's SelectedIndex property to determine the array subscript SelectedIndex property holds the position of the selected list item groupnumberinteger = groupcombobox.selectedindex; McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-27

28 Multidimensional Arrays - 1 To define a two-dimensional array or table: The declaration statement specifies the number of rows and columns The row is horizontal and the column is vertical Specify the number of elements within parentheses and/or specify initial values Specify the row with the first subscript and the column with the second subscript Use a comma between the subscripts Always use two subscripts when referring to individual elements of the table McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-28

29 Multidimensional Arrays - 2 Elements of an array are used the same way as other variables Accumulators, counts, reference fields for lookup In statements As conditions Invalid references would include a value greater than the subscript for the row or column McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-29

30 Multidimensional Arrays - 3 General form DataType[,] ArrayName = new DataType[NumberOfElements, NumberOfColumns]; DataType[,] ArrayName = new DataType[,] = {ListOfValues}; Examples string[,] namestring = new string[3, 4]; string[,] namestring = new string[,] = { {"James", "Mary", "Sammie", "Sean"}, {"Tom", "Lee", "Leon", "Larry"}, {"Maria", "Margaret", "Jill", "John"} }; Both of these statements create arrays with 3 rows and 4 columns Note the comma between square brackets Specifies that the array has two dimensions McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-30

31 Initializing Two-Dimensional Arrays Numeric array elements are initially set to zero String elements are initially set to an empty string You may need to initialize (or reinitialize) arrays to some other value Nested for loops can set each array element to an initial value Cannot use a foreach loop to initialize an array McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-31

32 Nested for Loop Example for (int rowinteger = 0; rowinteger < 3; rowinteger ++ ) { for (int columninteger = 0; columninteger < 4; columninteger ++) { namestring[rowinteger, columninteger] = ""; //Initialize each element. } } McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-32

33 Printing a Two-Dimensional Table Use a foreach loop to print one array element per line // Print one name per line. foreach (string elementstring in namestring) { // Set up a line. e.graphics.drawstring(elementstring, printfont, Brushes.Black, printxfloat, printyfloat); // Increment the Y position for the next line. printyfloat += lineheightfloat; } To print an entire row in one line, use a for loop and set the X and Y coordinates to print multiple elements per line McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-33

34 Summing a Two-Dimensional Table Include a total field for each row and each column Sum the figures in both directions (double check the totals) Each column and row needs one total variable Two onedimensional arrays work well for totals McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-34

35 Lookup Operation for Two-Dimensional Tables - 1 Use same techniques as for single dimension arrays Direct reference If meaningful row and column subscripts are available Table lookup Many 2D tables used for lookup will require additional one-dimensional arrays or lists to aid in the lookup process McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-35

36 Lookup Operation for Two-Dimensional Tables - 2 Example - User selects the weight from one list and the zone from the second list The SelectedIndex from the Weight list is used to find the correct row The SelectedIndex from the Zone list is used to find the correct column McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 8-36

Chapter 8. Arrays and Collections. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

Chapter 8. Arrays and Collections. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. Chapter 8 Arrays and Collections McGraw-Hill Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. Objectives Establish an array and refer to individual elements in the array with subscripts.

More information

Chapter 12 Two-Dimensional Arrays

Chapter 12 Two-Dimensional Arrays Chapter 12 Two-Dimensional Arrays Objectives Declare and initialize a two-dimensional array Enter data into a two-dimensional array Display the contents of a two-dimensional array Sum the values in a two-dimensional

More information

CIS 3260 Intro to Programming with C#

CIS 3260 Intro to Programming with C# Variables, Constants and Calculations McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. Define a variable Distinguish between variables, constants, and control objects Differentiate

More information

Chapter 7. Lists, Loops, and Printing The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Chapter 7. Lists, Loops, and Printing The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Chapter 7 Lists, Loops, and Printing McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter Objectives - 1 Create and use list boxes and combo boxes Differentiate among the available

More information

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

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Array An array is a group of variables (called elements or components) containing

More information

Chapter 6: Using Arrays

Chapter 6: Using Arrays Chapter 6: Using Arrays Declaring an Array and Assigning Values to Array Array Elements A list of data items that all have the same data type and the same name Each item is distinguished from the others

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Chapter 9 Introduction to Arrays. Fundamentals of Java

Chapter 9 Introduction to Arrays. Fundamentals of Java Chapter 9 Introduction to Arrays Objectives Write programs that handle collections of similar items. Declare array variables and instantiate array objects. Manipulate arrays with loops, including the enhanced

More information

INTRODUCTION 1 AND REVIEW

INTRODUCTION 1 AND REVIEW INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.

More information

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

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body Week 3 Pointers, References, Arrays & Structures Gaddis: Chapters 6, 7, 9, 11 CS 5301 Fall 2013 Jill Seaman 1 Arguments passed by value! Pass by value: when an argument is passed to a function, its value

More information

UNIT-I Fundamental Notations

UNIT-I Fundamental Notations UNIT-I Fundamental Notations Introduction to Data Structure We know that data are simply values or set of values and information is the processed data. And actually the concept of data structure is much

More information

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

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f; Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f; Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter

More information

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

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays A Beginner s Guide to Programming Logic, Introductory Chapter 6 Arrays Objectives In this chapter, you will learn about: Arrays and how they occupy computer memory Manipulating an array to replace nested

More information

Problem Solving with C++

Problem Solving with C++ GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter

More information

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

Programming Logic and Design Sixth Edition

Programming Logic and Design Sixth Edition Objectives Programming Logic and Design Sixth Edition Chapter 6 Arrays In this chapter, you will learn about: Arrays and how they occupy computer memory Manipulating an array to replace nested decisions

More information

Chapter Nine Arrays. Sixth Edition

Chapter Nine Arrays. Sixth Edition Microsoft Visual Basic 2015: Reloaded Sixth Edition Chapter Nine Arrays Objectives After studying this chapter, you should be able to: Declare and initialize one-dimensional and twodimensional arrays Store

More information

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers Microcontroller Systems ELET 3232 Topic 8: Structures, Arrays, & Pointers 1 Agenda Become familiar with and apply: Arrays Structures Pointers 2 Array Arrays A data set of a particular data type All elements

More information

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

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

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

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections C++ Programming Chapter 6 Arrays and Vectors Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

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

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers C++ for Engineers and Scientists Third Edition Chapter 12 Pointers CSc 10200! Introduction to Computing Lecture 24 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this chapter you will

More information

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

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I Lecture (07) Arrays By: Dr Ahmed ElShafee ١ introduction An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type Instead

More information

Programming for Electrical and Computer Engineers. Pointers and Arrays

Programming for Electrical and Computer Engineers. Pointers and Arrays Programming for Electrical and Computer Engineers Pointers and Arrays Dr. D. J. Jackson Lecture 12-1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements.

More information

Chapter 8 Arrays. Programming In Visual Basic.NET

Chapter 8 Arrays. Programming In Visual Basic.NET Chapter 8 Arrays Programming In Visual Basic.NET 1 Case Structure Best alternative for testing a single variable or expression for multiple values Any decisions coded with nested If statements can also

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

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

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays 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:

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

More information

Spring 2010 Java Programming

Spring 2010 Java Programming Java Programming: Guided Learning with Early Objects Chapter 7 - Objectives Learn about arrays Explore how to declare and manipulate data in arrays Learn about the instance variable length Understand the

More information

Microsoft Visual Basic 2005 CHAPTER 6. Loop Structures

Microsoft Visual Basic 2005 CHAPTER 6. Loop Structures Microsoft Visual Basic 2005 CHAPTER 6 Loop Structures Objectives Add a MenuStrip object Use the InputBox function Display data using the ListBox object Understand the use of counters and accumulators Understand

More information

Introduction to C Final Review Chapters 1-6 & 13

Introduction to C Final Review Chapters 1-6 & 13 Introduction to C Final Review Chapters 1-6 & 13 Variables (Lecture Notes 2) Identifiers You must always define an identifier for a variable Declare and define variables before they are called in an expression

More information

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #43 Multidimensional Arrays In this video will look at multi-dimensional arrays. (Refer Slide Time: 00:03) In

More information

JME Language Reference Manual

JME Language Reference Manual JME Language Reference Manual 1 Introduction JME (pronounced jay+me) is a lightweight language that allows programmers to easily perform statistic computations on tabular data as part of data analysis.

More information

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays CS0007: Introduction to Computer Programming Review If the == operator has two array variable operands, what is being compared? The reference variables held in the variables.

More information

Data Types and Variables in C language

Data Types and Variables in C language Data Types and Variables in C language Basic structure of C programming To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are

More information

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof. GridLang: Grid Based Game Development Language Language Reference Manual Programming Language and Translators - Spring 2017 Prof. Stephen Edwards Akshay Nagpal Dhruv Shekhawat Parth Panchmatia Sagar Damani

More information

Technical Questions. Q 1) What are the key features in C programming language?

Technical Questions. Q 1) What are the key features in C programming language? Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility

More information

Microsoft Visual Basic 2005: Reloaded

Microsoft Visual Basic 2005: Reloaded Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations Objectives After studying this chapter, you should be able to: Declare variables and named

More information

PieNum Language Reference Manual

PieNum Language Reference Manual PieNum Language Reference Manual October 2017 Hadiah Venner (hkv2001) Hana Fusman (hbf2113) Ogochukwu Nwodoh( ocn2000) Index Introduction 1. Lexical Convention 1.1. Comments 1.2. Identifiers 1.3. Keywords

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

Programming Language 2 (PL2)

Programming Language 2 (PL2) Programming Language 2 (PL2) 337.1.1 - Explain rules for constructing various variable types of language 337.1.2 Identify the use of arithmetical and logical operators 337.1.3 Explain the rules of language

More information

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

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA. Arrays Defining arrays, declaration and initialization of arrays Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical

More information

UNIT 3

UNIT 3 UNIT 3 Presentation Outline Sequence control with expressions Conditional Statements, Loops Exception Handling Subprogram definition and activation Simple and Recursive Subprogram Subprogram Environment

More information

Flag Quiz Application

Flag Quiz Application T U T O R I A L 17 Objectives In this tutorial, you will learn to: Create and initialize arrays. Store information in an array. Refer to individual elements of an array. Sort arrays. Use ComboBoxes to

More information

Functions, Arrays & Structs

Functions, Arrays & Structs Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions l Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

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

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays

More information

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

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 CMPE 013/L and Strings Gabriel Hugh Elkaim Spring 2013 4 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are

More information

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Data Types, Variables and Arrays OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani Identifiers in Java Identifiers are the names of variables, methods, classes, packages and interfaces. Identifiers must

More information

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

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo 1. (a)what is an algorithm? Draw a flowchart to print N terms of Fibonacci

More information

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc.

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc. Chapter 1 INTRODUCTION SYS-ED/ Computer Education Techniques, Inc. Objectives You will learn: Facilities and features of PL/1. Structure of programs written in PL/1. Data types. Storage classes, control,

More information

Module 6: Array in C

Module 6: Array in C 1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of

More information

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator. Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley

More information

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University Module 5 Arrays Adapted from Absolute Java, Rose Williams, Binghamton University Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type An

More information

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

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 Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

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

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will: Chapter 8 Arrays and Strings Objectives In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array

More information

Multiple-Subscripted Arrays

Multiple-Subscripted Arrays Arrays in C can have multiple subscripts. A common use of multiple-subscripted arrays (also called multidimensional arrays) is to represent tables of values consisting of information arranged in rows and

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

Multiple Choice Questions ( 1 mark)

Multiple Choice Questions ( 1 mark) Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s

More information

Chapter 8. Arrays and More Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

Chapter 8. Arrays and More Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8 Arrays and More Addison Wesley is an imprint of 2011 Pearson Addison-Wesley. All rights reserved. Introduction Arrays are like groups of variables that allow you to store sets of similar data

More information

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

More information

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 5 Arrays ALQUDS University Department of Computer Engineering Objective: After completing this session, the students should be able to:

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

CS 230 Programming Languages

CS 230 Programming Languages CS 230 Programming Languages 11 / 20 / 2015 Instructor: Michael Eckmann Questions/comments? Chapter 6 Arrays Pointers Today s Topics We all know what arrays are. Design issues Legal types for subscripts

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 ARRAYS ~ VECTORS KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 What is an array? Arrays

More information

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation. Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write an do-while loop Be able to write a for loop

More information

Lab 8 - Vectors, and Debugging. Directions

Lab 8 - Vectors, and Debugging. Directions Lab 8 - Vectors, and Debugging. Directions The labs are marked based on attendance and effort. It is your responsibility to ensure the TA records your progress by the end of the lab. While completing these

More information

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012 Data Abstractions National Chiao Tung University Chun-Jen Tsai 05/23/2012 Concept of Data Structures How do we store some conceptual structure in a linear memory? For example, an organization chart: 2/32

More information

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

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays A First Book of ANSI C Fourth Edition Chapter 8 Arrays One-Dimensional Arrays Array Initialization Objectives Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional

More information

Control Statements. Musa M. Ameen Computer Engineering Dept.

Control Statements. Musa M. Ameen Computer Engineering Dept. 2 Control Statements Musa M. Ameen Computer Engineering Dept. 1 OBJECTIVES In this chapter you will learn: To use basic problem-solving techniques. To develop algorithms through the process of topdown,

More information

6. User Defined Functions I

6. User Defined Functions I 6 User Defined Functions I Functions are like building blocks They are often called modules They can be put togetherhe to form a larger program Predefined Functions To use a predefined function in your

More information

Computer Programming: C++

Computer Programming: C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II Passing Array to a Function

More information

YOLOP Language Reference Manual

YOLOP Language Reference Manual YOLOP Language Reference Manual Sasha McIntosh, Jonathan Liu & Lisa Li sam2270, jl3516 and ll2768 1. Introduction YOLOP (Your Octothorpean Language for Optical Processing) is an image manipulation language

More information

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

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved. Chapter 12 Pointers and Arrays 1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements. This leads to an alternative way of processing arrays in which pointers

More information

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while

More information

Unit 7. Functions. Need of User Defined Functions

Unit 7. Functions. Need of User Defined Functions Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have

More information

Chapter 2: Functions and Control Structures

Chapter 2: Functions and Control Structures Chapter 2: Functions and Control Structures TRUE/FALSE 1. A function definition contains the lines of code that make up a function. T PTS: 1 REF: 75 2. Functions are placed within parentheses that follow

More information

Functions, Arrays & Structs

Functions, Arrays & Structs Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier

More information

Computer Programming C++ (wg) CCOs

Computer Programming C++ (wg) CCOs Computer Programming C++ (wg) CCOs I. The student will analyze the different systems, and languages of the computer. (SM 1.4, 3.1, 3.4, 3.6) II. The student will write, compile, link and run a simple C++

More information

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321 Sketchpad Graphics Language Reference Manual Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321 October 20, 2013 1. Introduction This manual provides reference information for using the SKL (Sketchpad

More information

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS Add your company slogan SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS Computer Programming (YPG) LOGO 306.1 Review Selected Programming Environment 306.1.1 Explain the concept of reserve words,

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos. UNIT 2 ARRAYS Arrays Structure Page Nos. 2.0 Introduction 23 2.1 Objectives 24 2.2 Arrays and Pointers 24 2.3 Sparse Matrices 25 2.4 Polynomials 28 2.5 Representation of Arrays 30 2.5.1 Row Major Representation

More information

Ar r ays and Pointer s

Ar r ays and Pointer s Ar r ays and Pointer s Using Bloodshed Dev-C++ Heejin Park Hanyang University 2 Introduction Arrays Multidimensional Arrays Pointers and Arrays Functions, Arrays, and Pointers Pointer Operations Protecting

More information

Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 15: Files and Preprocessor Directives/Pointers 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained

More information

X Language Definition

X Language Definition X Language Definition David May: November 1, 2016 The X Language X is a simple sequential programming language. It is easy to compile and an X compiler written in X is available to simplify porting between

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

User Defined Functions

User Defined Functions User Defined Functions CS 141 Lecture 4 Chapter 5 By Ziad Kobti 27/01/2003 (c) 2003 by Ziad Kobti 1 Outline Functions in C: Definition Function Prototype (signature) Function Definition (body/implementation)

More information

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers Chapter 03: Computer Arithmetic Lesson 09: Arithmetic using floating point numbers Objective To understand arithmetic operations in case of floating point numbers 2 Multiplication of Floating Point Numbers

More information

Chapter 7 : Arrays (pp )

Chapter 7 : Arrays (pp ) Page 1 of 45 Printer Friendly Version User Name: Stephen Castleberry email Id: scastleberry@rivercityscience.org Book: A First Book of C++ 2007 Cengage Learning Inc. All rights reserved. No part of this

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

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

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

Syntax errors are produced when verifying an EasyLanguage statement that is not

Syntax errors are produced when verifying an EasyLanguage statement that is not Building Winning Trading Systems with Trade Station, Second Edition By George Pruitt and John R. Hill Copyright 2012 by George Pruitt and John R EasyLanguage Syntax Errors Syntax errors are produced when

More information

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

UNIT-II. Part-2: CENTRAL PROCESSING UNIT Page1 UNIT-II Part-2: CENTRAL PROCESSING UNIT Stack Organization Instruction Formats Addressing Modes Data Transfer And Manipulation Program Control Reduced Instruction Set Computer (RISC) Introduction:

More information

Model Viva Questions for Programming in C lab

Model Viva Questions for Programming in C lab Model Viva Questions for Programming in C lab Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart is a diagram that shows a continuous

More information