380 likes | 391 Views
Learn about data types, arrays, variables, identifiers, and primitive data types in programming. Understand memory storage, identifier rules, and naming conventions. Study integer, float, character, and string data types with examples and conventions.
E N D
Goals • By the end of this unit, you should understand … • … why we use data types. • … the different types of primitive data that are available to programmers. • … how to use arrays. • … how we can use different algorithms to sort arrays.
Data 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.
Variable Identifiers • 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: • 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).
VALID IDENTIFIERS userName user_name USERNAME userName2 uSeRnAmE INVALID IDENTIFIERS _userName user'sName user name user-name 2UserName Identifier Examples
Identifiers are Case-Sensitive! • Identifiers are case-sensitive, meaning that uppercase letters and lowercase letters are seen as different characters. • Therefore: • userName <> USERNAME • UserName <> username • userName <> UsErNaMe • etc …
Identifier Conventions • 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.
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. • 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.
Camel-Casing Examples • userName • monthlyStatement • loanAmount • selectedCourse • studentID • instructorOfRecord • yearlySalary
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). • 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.
Primitive Types • Data primitives, or primitive types, are data types a computer inherently understands. • Most languages break primitive types into four categories (if not more): • Integers • Floating Point Number (Real Numbers) • Characters/Strings • Booleans
Integer Numbers • Programmers use integer data types to store positive and negative whole numbers and the number zero. • Require small amount of space in memory. • We would declare in pseudocode like this:Declare variableName as Integer
Floating Point Numbers • Programmers use the “float” data type to store numbers with fractional values. • Floats require larger amounts of space in memory. • You can also store integers as floats. • Pseudocode Example:Declare variableName as Float
Characters • A characteris 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). • The encoding schemes represent lower- and upper-case letters using different numbers. • Even a space or blank is represented by a number! • We enclose character values in quotes.
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 stringis a sequence of characters. • Just like with characters, we enclose string values in 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.
Creating a String in Pseudocode • Basic form:Declare variableName as String • Example:Declare userName as String
Comparing Strings • 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: • “B” is less than “b” because “B” has an encoding value of 66, while “b” has a value of 98.
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”. Declare myString as String myString = “1 + 2” Write myString
Booleans • Programmers use the Boolean data type to store true/false values. • Booleans require very small amounts of space in memory. • When assigning values to a Boolean, we use the words true or false, without quotes. • Pseudocode Example:Declare variableName as Boolean
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: Declare firstNumber as Integer Declare secondNumber as Float firstNumber = 8675309 secondNumber = firstNumber • secondNumber gets the value 8675309.0
Casting • 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 5.745 to an in integer number will result in value 5, completely different from the original value.
Casting • Casting differs from language to language (some languages use casting operators, some use functions, etc.). • Pseudocode (uses a casting operator): Declare userNumber as String Delcare actualNumber as Integer Prompt “Enter an integer number: ”, userNumber actualNumber = (Integer) userNumber
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!). • To solve this problem, programming languages include another data structure called an array.
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. • In memory, array values occupy contiguous locations. • For instance, we could use an array to store the names of students in a class ...
Common Array Terms • 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: Write myStringArray[5]
Array Numbering & Length • 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 length is always one more than the last index number. Conversely, the last index number is always one less than the array’s length.
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 array 0 Janie Bobby 1 Sally 2 Joey 3 Mary 4 Row0 One-Dimensional Arrays Mary is located in Row0[4]
Two-Dimensional Arrays • Two 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. • See the next slide for details …
0 0 0 Terri Katie Janie Cindy Jim Bobby 1 1 1 Mike Alex Sally 2 2 2 Joey Ravi Jacob 3 3 3 Mary Jamal Kirby 4 4 4 2 0 1 Two-Dimensional Array Example Mary is located in Class[0,4] Class Array
Parallel (Corresponding) Arrays • 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: • Student ID • Student Name • Student Exam Score
Parallel Arrays Example Array Name = SID Array Name = n201Class Array Name = examScr
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. • 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’.
Searching and Sorting Arrays • 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.
Searching and Sorting Arrays • 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. • The value of the flag can be checked later in the program with an ‘If’ statement to determine which action to take.
The Bubble Sort Technique • 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. • 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.
Sorting Examples • http://www.cs.iupui.edu/~aharris/n301/alg/tmcm-java-labs/labs/xSortLabLab.html
Resources • Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.