CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Data Types & Arrays.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Chapter 9: Advanced Array Manipulation
Programming Logic and Design Sixth Edition
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
String Escape Sequences
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Arrays in JavaScript.
Objectives You should be able to describe: Data Types
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Lists in Python.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
CPS120: Introduction to Computer Science
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 6: Arrays: Lists and Tables
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CS346 Javascript -3 Module 3 JavaScript Variables.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Copyright Curt Hill Variables What are they? Why do we need them?
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Basic Computation
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
IDENTIFIERS CSC 111.
Data Types, Identifiers, and Expressions
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
elementary programming
Programming Logic and Design Fifth Edition, Comprehensive
The Data Element.
Chapter 9: More About Data, Arrays, and Files
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
The Data Element.
Review of Java Fundamentals
Arrays.
Presentation transcript:

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Data Types & Arrays

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Goals By the end of this unit, you should understand …By the end of this unit, you should understand … … why we use data types.… why we use data types. … the different types of primitive data that are available to programmers.… the different types of primitive data that are available to programmers. … how to use arrays.… how to use arrays. … how we can use different algorithms to sort arrays.… how we can use different algorithms to sort arrays.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Data in Memory All programming languages include a way to reference data stored in memory.All programming languages include a way to reference data stored in memory. Typically we store such data using places in memory called variables. In a program we reference a variable's name (it's identifier) to either assign data to a variable or to read data from a variable.Typically we store such data using places in memory called variables. In a program we reference a variable's name (it's identifier) to either assign data to a variable or to read data from a variable.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Variable Identifiers Variable identifiers are the way we reference variables in a program. They are also referred to as variable names.Variable identifiers are the way we reference variables in a program. They are also referred to as variable names. In most languages, identifiers must follow the rules below:In most languages, identifiers must follow the rules below: –Identifiers must begin with a letter. –Subsequent identifier characters may be letters, numbers or the underscore character, "_". –Identifiers may not contain spaces. –Identifiers must be unique and cannot conflict with a reserved word (keyword).

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Identifier Examples VALID IDENTIFIERS userNameuserName user_nameuser_name USERNAMEUSERNAME userName2userName2 uSeRnAmEuSeRnAmE INVALID IDENTIFIERS _userName_userName user'sNameuser'sName user nameuser name user-nameuser-name 2UserName2UserName

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Identifiers are Case-Sensitive! Identifiers are case-sensitive, meaning that uppercase letters and lowercase letters are seen as different characters.Identifiers are case-sensitive, meaning that uppercase letters and lowercase letters are seen as different characters. Therefore:Therefore: –userName <> USERNAME –UserName <> username –userName <> UsErNaMe –etc …

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Identifier Conventions Although not "hard and fast" rules, programmers have developed some common conventions for creating identifiers:Although not "hard and fast" rules, programmers have developed some common conventions for creating identifiers: –Create variable identifiers using a meaningful name. –Some programmers like to use prefixes to an identifier, indicating data type (more on that later). –Use camel-casing for identifiers that are multi-word phrases.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science More on Camel-Casing Camel-Casing is a convention some programmers use to make identifiers easy to read. If an identifier has more than 1 word in it, you can use camel-casing.Camel-Casing is a convention some programmers use to make identifiers easy to read. If an identifier has more than 1 word in it, you can use camel-casing. To use camel-casing:To use camel-casing: –Begin the identifier name with a lowercase letter. –Capitalize the first letter of each subsequent word in an identifier's name.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Camel-Casing Examples userNameuserName monthlyStatementmonthlyStatement loanAmountloanAmount selectedCourseselectedCourse studentIDstudentID instructorOfRecordinstructorOfRecord yearlySalaryyearlySalary

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Data Typing When we create a variable, not only do we give a variable an identifier, we also declare its type (sometimes referred to as "dimensioning" a variable).When we create a variable, not only do we give a variable an identifier, we also declare its type (sometimes referred to as "dimensioning" a variable). Among other purposes, a variable type tells a computer how much memory to set aside for a particular variable.Among other purposes, a variable type tells a computer how much memory to set aside for a particular variable. Why do we type variables? Conservation of resources.Why do we type variables? Conservation of resources.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Primitive Types Data primitives, or primitive types, are data types a computer inherently understands.Data primitives, or primitive types, are data types a computer inherently understands. Most languages break primitive types into four categories (if not more):Most languages break primitive types into four categories (if not more): –Integers –Floating Point Number (Real Numbers) –Characters/Strings –Booleans

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Integer Numbers Programmers use integer data types to store positive and negative whole numbers and the number zero.Programmers use integer data types to store positive and negative whole numbers and the number zero. Require small amount of space in memory.Require small amount of space in memory. We would declare in pseudocode like this: Declare variableName as IntegerWe would declare in pseudocode like this: Declare variableName as Integer

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Floating Point Numbers Programmers use the “float” data type to store numbers with fractional values.Programmers use the “float” data type to store numbers with fractional values. Floats require larger amounts of space in memory.Floats require larger amounts of space in memory. You can also store integers as floats.You can also store integers as floats. Pseudocode Example: Declare variableName as FloatPseudocode Example: Declare variableName as Float

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Characters A character is a single number, letter or symbol not intended to be used in calculations.A character is a single number, letter or symbol not intended to be used in calculations. Each character is represented internally by a numeric code in a given encoding scheme, like ASCII, ANSI or Unicode (related link).Each character is represented internally by a numeric code in a given encoding scheme, like ASCII, ANSI or Unicode (related link).related linkrelated link The encoding schemes represent lower- and upper-case letters using different numbers.The encoding schemes represent lower- and upper-case letters using different numbers. Even a space or blank is represented by a number!Even a space or blank is represented by a number! We enclose character values in quotes.We enclose character values in quotes.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Strings Some programming languages using the string data type both for true strings and characters, while others recognize only characters as a data type and strings as special arrays made of characters. A string is a sequence of characters.Some programming languages using the string data type both for true strings and characters, while others recognize only characters as a data type and strings as special arrays made of characters. A string is a sequence of characters. Just like with characters, we enclose string values in quotes.Just like with characters, we enclose string values in quotes. The null string consists of two quotes : “”The null string consists of two quotes : “” Strings have a length in memory that is equal to the number of characters in the string, including blanks.Strings have a length in memory that is equal to the number of characters in the string, including blanks.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Creating a String in Pseudocode Basic form: Declare variableName as StringBasic form: Declare variableName as String Example: Declare userName as StringExample: Declare userName as String

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Comparing Strings Two strings are equal if they have the same length and the same characters, in the same order.Two strings are equal if they have the same length and the same characters, in the same order. –“Blue” and “BLUE” are not equal. –“c%$$” and “c%$$” are equal. Other comparisons are based on the ASCII/ANSI/Unicode value:Other comparisons are based on the ASCII/ANSI/Unicode value: –“B” is less than “b” because “B” has an encoding value of 66, while “b” has a value of 98.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Numbers as Strings We can “tell” the computer to treat a number as a string, if we don’t intend to use that number in a calculation, by enclosing it in quotes. The code below will not display 3, but the character string “1 + 2”.We can “tell” the computer to treat a number as a string, if we don’t intend to use that number in a calculation, by enclosing it in quotes. The code below will not display 3, but the character string “1 + 2”. Declare myString as String myString = “1 + 2” Write myString

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Booleans Programmers use the Boolean data type to store true/false values.Programmers use the Boolean data type to store true/false values. Booleans require very small amounts of space in memory.Booleans require very small amounts of space in memory. When assigning values to a Boolean, we use the words true or false, without quotes.When assigning values to a Boolean, we use the words true or false, without quotes. Pseudocode Example: Declare variableName as BooleanPseudocode Example: Declare variableName as Boolean

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Converting Data Types Sometimes, we need to convert a value to another data type in order to use it. In most languages, we can convert a smaller data type to a larger one without trouble. For instance, converting an integer to a float:Sometimes, we need to convert a value to another data type in order to use it. In most languages, we can convert a smaller data type to a larger one without trouble. For instance, converting an integer to a float: Declare firstNumber as Integer Declare secondNumber as Float firstNumber = secondNumber = firstNumber secondNumber gets the value secondNumber gets the value

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Casting When programmers need explicitly convert data types (like converting from a float to an integer), they use something called casting or parsing.When programmers need explicitly convert data types (like converting from a float to an integer), they use something called casting or parsing. We need to be careful when casting, as it can be a destructive operation. For instance, casting the value to an in integer number will result in value 5, completely different from the original value.We need to be careful when casting, as it can be a destructive operation. For instance, casting the value to an in integer number will result in value 5, completely different from the original value.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Casting Casting differs from language to language (some languages use casting operators, some use functions, etc.).Casting differs from language to language (some languages use casting operators, some use functions, etc.). Pseudocode (uses a casting operator):Pseudocode (uses a casting operator): Declare userNumber as String Delcare actualNumber as Integer Prompt “Enter an integer number: ”, userNumber actualNumber = (Integer) userNumber

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Introducing Arrays Sometimes, it is quite cumbersome to declare multiple variables. Consider having to declare 35 different variables to represent students in class (70 if you separate first and last names!).Sometimes, it is quite cumbersome to declare multiple variables. Consider having to declare 35 different variables to represent students in class (70 if you separate first and last names!). To solve this problem, programming languages include another data structure called an array.To solve this problem, programming languages include another data structure called an array.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science What is an Array? An array is a data structure that contains related items that are of the same data type and where each item in the array shares the same name.An array is a data structure that contains related items that are of the same data type and where each item in the array shares the same name. In memory, array values occupy contiguous locations.In memory, array values occupy contiguous locations. For instance, we could use an array to store the names of students in a class...For instance, we could use an array to store the names of students in a class...

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Common Array Terms An element is a value stored in an array.An element is a value stored in an array. Each element is stored in a different position in an array. We can reference a position using a subscript (a.k.a. index). When we reference an element, we indicate the array name and the subscript of the particular element we want:Each element is stored in a different position in an array. We can reference a position using a subscript (a.k.a. index). When we reference an element, we indicate the array name and the subscript of the particular element we want: Write myStringArray[5]

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Array Numbering & Length We number subscripts, or indexes, starting with the number zero (0). We increment by 1 for each subsequent index.We number subscripts, or indexes, starting with the number zero (0). We increment by 1 for each subsequent index. The array’s length refers to the total number of indexes used in an array.The array’s length refers to the total number of indexes used in an array. The length is always one more than the last index number. Conversely, the last index number is always one less than the array’s length.The length is always one more than the last index number. Conversely, the last index number is always one less than the array’s length.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science One-Dimensional Arrays A one-dimensional array is a single list of values (called “elements”) and their addresses in the array (called “indexes” or “subscripts”)A one-dimensional array is a single list of values (called “elements”) and their addresses in the array (called “indexes” or “subscripts”) Think of a seating chart in elementary school – a single row of students is an arrayThink of a seating chart in elementary school – a single row of students is an array Janie Bobby Sally Joey Mary Row0 Mary is located in Row0[4]

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Two-Dimensional Arrays Two dimensional arrays contain other arrays as elementsTwo dimensional arrays contain other arrays as elements To reference an element of a “child” array, you would need to reference both the index number of the child array and the index number of the element in the child array.To reference an element of a “child” array, you would need to reference both the index number of the child array and the index number of the element in the child array. See the next slide for details …See the next slide for details …

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Two-Dimensional Array Example Janie Bobby Sally Joey Mary Katie Jim Alex Ravi Jamal Terri Cindy Mike Jacob Kirby Class Array Mary is located in Class[0,4]

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Parallel (Corresponding) Arrays Parallel arrays give the programmer the ability to store multiple pieces of information about a single entity in an application.Parallel arrays give the programmer the ability to store multiple pieces of information about a single entity in an application. The indexes of each array should correspond to one another for individual entities:The indexes of each array should correspond to one another for individual entities: –Student ID –Student Name –Student Exam Score

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Parallel Arrays Example Array Name = SID 0John1Mary 2Elizabeth 3Omar 4Charles 5Rebecca 6Katherine 7Hannah 8Alexander 9William Array Name = n201Class Array Name = examScr

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Searching and Sorting Arrays If we have multiple, related arrays, with the elements ordered correctly, we can find values in one array based on on a search key value in another array.If we have multiple, related arrays, with the elements ordered correctly, we can find values in one array based on on a search key value in another array. A search key is a known value for one of the arrays. In the text example, a specific value of ‘Flight’ can be used to find the Origin, Time, and Gate of that flight. It can be used as a search key.A search key is a known value for one of the arrays. In the text example, a specific value of ‘Flight’ can be used to find the Origin, Time, and Gate of that flight. It can be used as a search key. This is called a ‘table lookup’ or ‘serial search’.This is called a ‘table lookup’ or ‘serial search’.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Searching and Sorting Arrays 3 steps:3 steps: –Load, or populate, the arrays. –Search the array that contains the key, to find a match. –Display the key and corresponding values in the non-key arrays with the same subscript as the one with the key value, if a match is found. –Display an ‘unsuccessful search’ message if no matches were found.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Searching and Sorting Arrays Use a flag, or switch, to decide which message to print.Use a flag, or switch, to decide which message to print. A flag is a variable that is set to zero or one, depending on the results of an operation.A flag is a variable that is set to zero or one, depending on the results of an operation. The value of the flag can be checked later in the program with an ‘If’ statement to determine which action to take.The value of the flag can be checked later in the program with an ‘If’ statement to determine which action to take.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The Bubble Sort Technique To sort data means to arrange it in a particular numerical or alphabetical order, ascending or descending.To sort data means to arrange it in a particular numerical or alphabetical order, ascending or descending. To sort small amounts of data, use the ‘bubble sort’ technique.To sort small amounts of data, use the ‘bubble sort’ technique. –Make several passes through the data. –Compare adjacent pairs of data. –If the pairs are not in order, swap them. –Continue until all data is processed.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Sorting Examples g/tmcm-java-labs/labs/xSortLabLab.htmlhttp:// g/tmcm-java-labs/labs/xSortLabLab.htmlhttp:// g/tmcm-java-labs/labs/xSortLabLab.htmlhttp:// g/tmcm-java-labs/labs/xSortLabLab.html

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Questions?

Resources Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.