Arrays, Slices, and more. Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Arrays.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Structures Spring 2013Programming and Data Structure1.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Arrays.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
ISBN Chapter 6 Data Types: Structured types.
©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.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Java Unit 9: Arrays Declaring and Processing Arrays.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Built-in Data Structures in Python An Introduction.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Computer Programming BCT 1113
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Lecture 18 Arrays and Pointer Arithmetic
Java Programming Language
C++ Array 1.
Presentation transcript:

Arrays, Slices, and more

Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex struct { X int Y int }

 To access a field within a struct, use a dot  Syntax: func main() { v := Vertex{9,5} //declare and initialize v v.X = 4 //set X equal to 4 fmt.Println (v.X) //print out X }

Struct Literals  You can initialize the values of the fields within the struct when you initialize the struct red := Vertex{12,12} // X and Y equal 12  You can use the variableName: to specify a particular value yellow := Vertex{Y: 66, X: 12} //Y equals 66, X equals 12, order does not matter  Or you can implicitly assign values blue := Vertex{} //X and Y are both implicitly assigned 0

Maps  A map matches keys to values  Maps are created with make before they are used  Syntax: var myMap map [string] Vertex func main (){ myMap = make(map [string] Vertex) }

 Assign a key to a value like this: myMap [“Red”] = Vertex{12, 12}  Map literals are like struct literals, but the keys are required var myMap = map [string] Vertex{ “Red”: Vertex {12, 12}, “Yellow”: Vertex {12, 66}, }  Alternatively, if the top-level type is just a type name, you can omit it from the elements of the literal var myMap = map [string] Vertex{ “Red”: {12,12} “Yellow”: {12,66} }

Arrays  Arrays are structures that store one or more values linearly  Its values are accessed by integer indices from 0 to len(arrayName) – 1  The len(arrayName) function is built in and returns the length of the passed in array  Syntax: myArray := [5] int {1,2,3,4,5};  The size of an array is included as part of its type. So [2] int is a different type than [5]int.

 Arrays in Go are different from arrays in C Arrays are values. Assigning one array to another copies all its elements. Passing an array to a function sends a copy of the array, not a pointer to it (unless otherwise specified). The size of an array is part of its type. Go does not support the pointer arithmetic tricks that you might use in C. Instead, when you make a pointer to an array, you get a pointer to the actual array, not just a pointer to something that happens to be the first thing in the array.

 Arrays are one dimensional but can be composed to make multiple dimensions (an array of arrays) [5] [15] int is equivalent to [5] ([15] int)  Syntax: array2D := [2] ([3] int) { {1,2,3}, {4,5,6} } //an array with two elements, each element //being an array with 3 elements

Slices  A slice points to an array of values  It also has a built-in length  Syntax: aSlice := []int {1,2,3}  A slice can be cut from an array arrayName := [5] int {1,2,3,4,5} arraySlice := arrayName[a:b]  The size of a slice is b-a  arrayName[a:a] includes no elements  arrayName[a:a+1] includes one element

 Slices are abstractions of arrays  Slices are more flexible than arrays and as a result are more common in Go code  Slices have no specified lengths like arrays do  Slices can be grown with the built-in copy or append function  The keyword range allows you to loop through an entire slice or array

 Slices are also created with make  Syntax: arraySlice := make([]int, 5)  This allocates an array of length 5 and returns a slice that refers to the array  All the values are initialized to 0  The make function can also be passed a third parameter to specify the capacity of the slice arraySlice := make([]int, 5, 10) Here, the length is 5, but the slice can grow up to 10

Tying it all together  An application: creating a simple address book  addressBook.go maps values in a slice of strings to values in a slice of ints  addressBook2.go uses structs to achieve something similar