Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Data Types & Arrays."— Presentation transcript:

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

2 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.

3 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.

4 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).

5 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

6 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 …

7 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.

8 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.

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

10 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.

11 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

12 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

13 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

14 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.

15 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.

16 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

17 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.

18 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

19 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

20 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

21 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.

22 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

23 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.

24 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...

25 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]

26 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.

27 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]

28 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 …

29 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]

30 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

31 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

32 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’.

33 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.

34 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.

35 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.

36 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Sorting Examples g/tmcm-java-labs/labs/xSortLabLab.htmlhttp://www.cs.iupui.edu/~aharris/n301/al g/tmcm-java-labs/labs/xSortLabLab.htmlhttp://www.cs.iupui.edu/~aharris/n301/al g/tmcm-java-labs/labs/xSortLabLab.htmlhttp://www.cs.iupui.edu/~aharris/n301/al g/tmcm-java-labs/labs/xSortLabLab.html

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

38 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.


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

Similar presentations


Ads by Google