Data Types and Data Structures

Slides:



Advertisements
Similar presentations
Data types and variables
Advertisements

Chapter 2: Introduction to C++.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
CIS Computer Programming Logic
Lists in Python.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Term 2, 2011 Week 1. CONTENTS Problem-solving methodology Programming and scripting languages – Programming languages Programming languages – Scripting.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
7 - Programming 7J, K, L, M, N, O – Handling Data.
Chapter # 2 Part 2 Programs And data
Unit 2 Technology Systems
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
ITM 352 Data types, Variables
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Chapter 2: Introduction to C++
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
BASIC ELEMENTS OF A COMPUTER PROGRAM
© 2016 Pearson Education, Ltd. All rights reserved.
Documentation Need to have documentation in all programs
Basic Elements of C++.
The Selection Structure
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
Modified from Sharon Guffy
JavaScript: Functions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Basic Elements of C++ Chapter 2.
Arrays An Array is an ordered collection of variables
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables ICS2O.
Chapter 2 Variables.
Coding Concepts (Data Structures)
PHP.
ARRAYS 1 GCSE COMPUTER SCIENCE.
Coding Concepts (Data- Types)
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Chapter 2: Introduction to C++.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Topics Designing a Program Input, Processing, and Output
Programming Logic and Design Fifth Edition, Comprehensive
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Chapter 2 Variables.
DATA TYPES There are four basic data types associated with variables:
Variables and Constants
Introduction to Computer Programming IT-104
Presentation transcript:

Data Types and Data Structures

Data Types Keywords Data types: The forms in which data can be stored. String: Usually written as a sequence of characters enclosed in single or double quotation marks, i.e. 'hello' or "hello". Integer: Used for whole numbers. Float: Used for numbers that contain decimal points, or for fractions. Boolean: Used where data is restricted to True/False or yes/no options. Variable: A method of storing other data.

Integer This data type can only store whole number values. The values can be +ve and -ve The typical range for such numbers is –32768 to 32767 E.g. –34, 0, 3456

Real (Single in Visual Basic) This data type stores numbers which can contain decimal parts The values can be +ve and -ve The range of numbers that be stored is much larger as the computer uses the scientific method of storage E.g. –5.4, 6.0, 0.0, 5.67 Note, when integers are stored as real values they can begin to lose their accuracy

String This data type can store any character, usually up to a maximum length of 256 characters Hence numbers, letters and special characters (anything other than a number or letter) can all be stored If a number is stored in this data type it no longer has any numerical quality E.g. “Fred”, “01515263814”, “L31 6DE”

Char This data type can store any single character Hence a number, letter or special character (anything other than a number or letter) can all be stored If a number is stored in this data type it no longer has any numerical quality E.g. “M”, “y”, “1”

Boolean This data type can store one of two values, TRUE or FALSE This data type is usually employed to show whether an event has taken place or not

Storage Data of different types uses different amounts of storage space in memory or on the disk

Arithmetic Operations Having seen the basic 5 data types, we need to ascertain what arithmetic operations each can support From the next table simply put a Y(es) where the operation is valid and N(o) when the operation isn’t valid

Arithmetic Operations

Arithmetic Operations

Arithmetic Operations Note that the Yes? For the equals sign when using real data types. This means the operation is possible but not desirable This is because to test real numbers they would have to accurate to many decimal places The loss of accuracy associated with real values would mean that there is always a chance the match wouldn’t be generated Usual therefore to use >= or <= instead

TASK: Data types Example Data Type “Donkey” 1066 name = “Bob” X>Y score = 20 246810 2.4681 Z “Computing!”

Variables The name given to each variable is up to the programmer, but ideally a variable name should reflect the value being heldThere are some rules about variable names: Consistency: ‘name’ is not the same as ‘Name’ or ‘NAME’. Spacing: variable names should not have a space in them. Use underscores or camelCase instead, eg total_money; totalMoney). Digits: variable names should not start with a digit.

Consider these example variable names, all of which could be variable names to store the length of a side of a square Variable name Comment l length side_length sideLength side length A poor choice – it has no meaning Okay but a bit vague Good Wrong – don’t use spaces

TASK: Name the Variables Consider the variable naming rules. Choose an appropriate variable name for each of the following situations… Situation Chosen Variable Name Variable to store the number of sheep counted Variable to store the name of player Variable to store the sum of money you have in your pocket Variable to store the measurement the length a ball is thrown Variable to store the number of coins Mario has collected

Boolean Programs use simple comparisons to help make decisions. Boolean logic is a form of algebra where all values are either True or False. These values of true and false are used to test various programming conditions. Expression Boolean equivalent Equals = Greater than > Less than < Greater than or equal to >= Less than or equal to <= Does not equal <> And AND Or OR Not NOT

Review of data types Variable name Assigned value Data Type index 3 In some programming languages, the data type of a variable does not have to be explicitly declared. They assume what the data type of a variable should be from the data that is assigned to it. TASK: What data type would be assigned to the following values? Variable name Assigned value Data Type index 3 3.33 Catherine

Data Structures A data structure is a specialized format for organizing and storing data. General data structure types include the array, the file, the record, the table, the tree, and so on. Any data structure is designed to organize data to suit a specific purpose so that it can be accessed and worked with in appropriate ways.

Array An array is a series of memory locations –each of which holds a single item of data All data in an array must be of the same data type. It is a list or table of data that has a variable name Each item is called an element They can have many dimensions

Part 1: One-dimensional arrays

Example array For example, imagine that a score table in a game needs to record ten scores. One way to do this is to have a variable for each score: score_1 score_2 score_3 score_4 score_5 score_6 score_7 score_8 score_9 score_10 It is much simpler to keep all the related data under one name. We do this by using an array. Instead of having ten variables, each holding a score, there could be one array that holds all the related data: score(10)

How an array works 1 2 3 4 5 6 7 8 9 Score: 1 2 3 4 5 6 7 8 9 Score: Each box in the table can contain an integer. Each box has a numerical reference called an index that is used to refer to the individual data item. Note that the first element of the array shown here has an index of zero. The individual boxes in the array can be used just like variables. Assign values to them: score[4] <- 20 Input values into them: score[9] <- USERINPUT Output the value stored in a box: OUTPUT “The fourth value is ” , score[3] Did you know? Lists are data structures similar to arrays that allow data of more than one data type. Python, for example, only allow lists.

Understanding an array total <- 0 FOR game <- 0 TO 11 score [game] <- USERINPUT total <- total + score [game] ENDFOR OUTPUT “The total is “, total Annotate the following pseudocode to show explain what is happening;

Once all index spaces are filled, the loop ends. A FOR loop is created for the game array which includes 12 index placements (0-11). As each score is added, it is accumulated. Within the loop, the user inputs a number within each array index under the identifier score [0-11]. Once the loop ends, a message is output which includes the total of each number in the array. The variable total is set a starting value of zero.

Python list challenges Print screen the outputs for the following instructions; 1. 2. Add the following code; 3. What do the following commands do? len : append() : pop() :

Part 2: Two-dimensional arrays

2D Array A one-dimensional array can be seen as data elements organised in a row. A two-dimensional array can be visualised as a grid (or table) with rows and columns. For example, a nine-by-nine grid could be referenced with numbers for each row and letters for each column. A nine-by-nine, two-dimensional array could be declared with a statement such as: game [9][9] Positions in a two dimensional array are referenced like a map using horizontal and vertical reference numbers.

When an array would be used When there are multiple distinct data sets which have to be stored separately but in related structures This structure allows sets of data to be accessed by a single identifier

Sales (4,3)

Task – Complete the array Complete the table below based on the following input; game [0] [1] [2] [3]

Arrays and Images

row-major and column-major order. Row-major order and column-major order describe methods for arranging multidimensional arrays in linear storage such as random access memory.

row-major and column-major order.

row-major and column-major order.

Record A record is a data structure that groups together related items of data. These are slightly more complex than arrays as you can store more than one type of data together. Creating records will vary in different languages. Python uses a data structure called 'dictionaries' that has some features of the record structure.

Array Questions 1. What is a data structure? a) A list of variables b) A list of variables and strings c) A facility that holds more than one value 2. What is an array? a) A list of variables b) A list of strings c) A series of memory locations, each with the same name, that hold related data 3. Why are arrays used? a) Arrays store multiple data values under one name, reducing the complexity of our program b) Arrays store more data than if we were using variables c) Arrays store more data than if we were using strings 4. In an array, what is an element? a) The smallest number an array can hold b) The biggest number an array can hold c) An element is one value in an array

Array Exam Questions 5. How many elements would the array 'student_marks(10)' hold? a) Nine elements b) Ten elements c) Eleven elements 6. In the array 'numbers(6, 10, 7, 12, 9, 2)', what is the value of element numbers(4)? a) Nine b) Twelve c) Two 7. When creating the array 'scores(9)', what does the '9' in brackets represent? a) The number of elements the array can hold b) The largest number of digits an element can hold c) The largest number of characters an element can hold 8. What is the difference between an array and a list? a) An array holds numbers, whereas a list holds strings b) An array can only hold data of the same data type, whereas lists can hold values of different data types c) An array holds both numbers and strings, whereas a list only holds numbers

Answers 1. What is a data structure? a) A list of variables b) A list of variables and strings c) A facility that holds more than one value 2. What is an array? a) A list of variables b) A list of strings c) A series of memory locations, each with the same name, that hold related data 3. Why are arrays used? a) Arrays store multiple data values under one name, reducing the complexity of our program b) Arrays store more data than if we were using variables c) Arrays store more data than if we were using strings 4. In an array, what is an element? a) The smallest number an array can hold b) The biggest number an array can hold c) An element is one value in an array

Remember: Computers start counting from 0! Answers 5. How many elements would the array 'scores(9)' hold? a) Nine elements b) Ten elements c) Eleven elements 6. In the array 'numbers(6, 10, 7, 12, 9, 2)', what is the value of element numbers(4)? a) Nine b) Twelve c) Two 7. When creating the array 'scores(9)', what does the '9' in brackets represent? a) The number of elements the array can hold b) The largest number of digits an element can hold c) The largest number of characters an element can hold 8. What is the difference between an array and a list? a) An array holds numbers, whereas a list holds strings b) An array can only hold data of the same data type, whereas lists can hold values of different data types c) An array holds both numbers and strings, whereas a list only holds numbers