Beginning C Lecture 5 Lecturer: Dr. Zhao Qinpei

Slides:



Advertisements
Similar presentations
Arrays and Strings.
Advertisements

Introduction to arrays
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
PLANNING THE TIC TAC TOE GAME BY NEEL DAVE. TIC TAC TOE INSTRUCTIONS Tic Tac Toe Instructions The basic concept of Tic Tac Toe 1.This is a game for two.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 8 Arrays and Strings
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
Chapter 8 Arrays and Strings
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 CSC103: Introduction to Computer and Programming Lecture No 24.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
1.2 Primitive Data Types and Variables
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing 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.
ARRAYS.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
INC 161 , CPE 100 Computer Programming
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Computer Programming BCT 1113
multi-dimensional arrays
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Arrays exist in almost every computer language.
EKT120 : Computer Programming
A simple way to organize data
2-D Lists Taken from notes by Dr. Neil Moore
Chapter 8 Arrays Objectives
JavaScript: Functions.
Arrays Arrays are data structures that allow us to store data of the same type in contiguous memory locations. That was a pretty dense statement!
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
5. Arrays, Pointers and Strings
Visual Basic .NET BASICS
CMPE 152: Compiler Design September 18 Class Meeting
One-Dimensional Array Introduction Lesson xx
Arrays Skill Area 315 Part A
7 Arrays.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Chapter 8 Arrays Objectives
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Introduction To Programming Information Technology , 1’st Semester
MSIS 655 Advanced Business Applications Programming
Managing Collections of Data
Chapter 8 Arrays Objectives
Suggested self-checks: Section 7.11 #1-11
Continue with Life, Magic and Catch -up
C Programming Pointers
CS150 Introduction to Computer Science 1
2-D Lists Taken from notes by Dr. Neil Moore
Tic-Tac-Toe Game Engine
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

Beginning C Lecture 5 Lecturer: Dr. Zhao Qinpei Email: qinpeizhao@tongji.edu.cn http://sse.tongji.edu.cn/zhaoqinpei/Courses/

Array What arrays are? How to use arrays in your program? How memory is used by an array? What a multidimensional array is? How to write a game of tic-tac-toe? Beiginning C / Qinpei Zhao 2019/4/27

Recursive Rabbit problem A rabbit gives birth of a baby rabbit every month since the third month of its own birth. And the baby rabbit gives birth of baby rabbit every month since the third month of its birth. Suppose the rabbits never die, how many rabbits are there in every month? Rabbit problem

Programming without arrays the average of ten numbers Not storing each grade (lec5_1_noarray.c) Beginning C / Qinpei Zhao 2019/4/27

Storing numbers in hard way Beginning C / Qinpei Zhao 2019/4/27

What is an array? An array is a fixed number of data items that are all of the same type. The data items in an array are referred to as elements. How many elements you want to store in your array is called the array dimension. long numbers[10]; Each individual value in the array is identified by an “index value”. numbers[0], numbers[1]… Beginning C / Qinpei Zhao 2019/4/27

What is an array? place an expression to access the value of an element, the expression would have to result in an integer value that corresponds to one of the possible index values numbers[i+2] note the legal range of an array “out of bounds” number[10] Beginning C / Qinpei Zhao 2019/4/27

Using arrays Declare an array Store numbers in the array Beginning C / Qinpei Zhao 2019/4/27

About memory again Each variable that you use in a program takes up a certain amount of memory, measured by bytes (8 bits). The label for a byte is called its address in hexadecimal . Address of operator & loaded memory differs each time a new format specifier %p Beginning C / Qinpei Zhao 2019/4/27

Memory (cont.) Stack Segment Low High Beginning C / Qinpei Zhao 2019/4/27

Arrays and Addresses The organization of an array in memory Value of the ith element address Beginning C / Qinpei Zhao 2019/4/27

Initializing an array Only for safety’s sake, assign initial values for the elements of the array If there are fewer initializing values than elements, the elements without initializing values will be 0. The compiler can deduce the number of elements from the list of values. 10 Beginning C / Qinpei Zhao 2019/4/27

An example (lec5_3_1array.c) Beginning C / Qinpei Zhao 2019/4/27

An example on initialization (lec5_4_nodata/somedata.c) Beginning C / Qinpei Zhao 2019/4/27

Multidimensional arrays 2-dimensional array float numbers[3][5]; Beginning C / Qinpei Zhao 2019/4/27

Initializing multidimensional arrays Beginning C / Qinpei Zhao 2019/4/27

An example Get a max value in a 2-dimensional array Beginning C / Qinpei Zhao 2019/4/27

Designing a program tic – tac –toe (noughts and crosses) Tic-tac-toe is played on a 3*3 grid of squares. Two players take turns entering either an X or an O in the grid. The player that first manages to get three of his or her symbols in a line horizontally, vertically, or diagonally is the winner. tic – tac –toe (noughts and crosses) lec5_problem.c Beginning C / Qinpei Zhao 2019/4/27

Summary About array A fixed number of elements of the same type Access any element within the array using the array name and one or more index values Index values are integers Combining arrays with loops provides a very powerful programming capability. Organize the data using multidimensional arrays By applying nested loops to multidimensional arrays, you can process all the array elements with a very small amount of code. To be continued on array with strings of characters. Beginning C / Qinpei Zhao 2019/4/27