1 / 109

Programs and Data

Programs and Data. Topics. Data. Simple data types. Variables and constants. Declarations. Assignment. Input and output. Style. Objectives. At the completion of this topic, students should be able to:. Describe the way that data is stored in the computer.

gigi
Télécharger la présentation

Programs and Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programs and Data

  2. Topics Data Simple data types Variables and constants Declarations Assignment Input and output Style

  3. Objectives At the completion of this topic, students should be able to: Describe the way that data is stored in the computer Describe the object model of programming Create proper identifiers in a C# program Describe the difference between an object and simple data Describe the simple data types in the C# language Write C# programs that correctly • use declarations • use assignment statements • use literal data • use the Console class • format simple floating point data

  4. Data Central to the idea of how a program works is the concept of data. Every program you will ever write will deal with some kind of data.

  5. That data might be … • An image • Some music • A person’s name • A person’s age • A table of temperatures by week • A list of scores for a gymnastic event • A model of a molecule • A model of a character in a game • etc

  6. Data is stored in the computer’s memory. We can imagine computer memory to be something like a set of post office boxes.

  7. Some boxes hold small things (like integers) Some boxes hold larger things (like real numbers) and each box has an address Some boxes hold really big things (like objects)

  8. When we write a program, we need to reserve space in memory for any data that the program will use. We do this by giving the data a name and telling the computer what kind of data it is. The computer translates this name into an address.

  9. X000054EA– hex address 12 In order to be able to refer to the data stored at some address in the computer, we give the data at that address a name. These names are called identifiers.

  10. Identifiers The name that you use to refer to a piece of data or a method in C# is called an identifier. A C# identifier must begin with either a letter or an underscore character i.e. [a-z A-Z _] The remaining characters may be letters, digits, or the underscore character[a-z A-Z 0-9 _]. All other characters are invalid. Identifiers can be of any length. Identifiers are case sensitive.

  11. Some Valid Identifiers x x1 _abc sum data2 oldValue It is common in C# to run words together like this. Just capitalize all words after the first.

  12. Some Invalid Identifiers 123 &change 1_dollar my-data

  13. Good programmers select names that are meaningful and somehow describe the data that they name.

  14. For example, we might have a piece of data that represents the area of a circle. x would be a bad choice to name that variable areaOfCircle would be a good choice

  15. Keywords C# has a set of built in keywords that are considered to be part of the language. You cannot use any of these as identifiers in your program. In Visual C# Express Edition, By default these will show up in blue. Examples include bool break char int double class const do …

  16. Programs deal with all kinds of data. This data can be put into two broad categories. Simple Data the most basic forms of data - numbers and characters Objects more complex data – made up of many pieces of simple data. For example, a person’s address is usually made up of a House number and a street name – 123 Main Street.

  17. Every piece of data has a given size and shape and is stored at an address. The values that we store in a memory location must fit the size and shape that we specified for that address.

  18. A Memory Chip

  19. A Memory Chip Address: Where the data resides in memory Size: How many bits make up the data Shape: How the data is coded . . .00110001000110 . . . binary digits (bits)

  20. Number Systems

  21. Decimal: Base 10 In all number systems, each position represents a power of the base, where the rightmost digit is the multiplied by the base0, the next digit to the left is multiplied by base1, and so on. So 352 can be expressed in powers of 10 as (3 * 102) + (5 * 101) + (2 * 100) = 300 + 50 + 2

  22. Binary: Base 2 In binary we only have two symbols with which to count, 0 and 1. We call these binary symbols “bits”, short for “binary digits”. If we group 8 of them together we have a byte, for example: 01001001. What does this represent? Who knows? Maybe an integer, maybe a character, maybe an instruction or a pixel on your monitor or a tiny bit of music.

  23. Binary: Base 2 Since the base is 2, the place value represents a power of 2. So a number like 1100 means… (1 * 23) + (1 * 22) + (0 * 21) + (0 * 20) = 8 + 4 + 0 + 0 …which would translate to 12 in decimal

  24. Hexadecimal: Base 16 Here we need 16 unique symbols to count with, so 0 – 9 aren’t enough. To fill in the remaining equivalents of 10 – 15 in decimal, we use the letters A – F. So to count in hex, we use 0, 1, 2…8, 9, A, B, C, D, E, F. That gives us 16 unique symbols, where A16 = 1010, F16 = 1510 etc. Note that in documentation, we often express the base with a subscript, as in 3D2F16 (hex) or 10112 (binary).

  25. Hexadecimal: Base 16 In hex, an number like C416 means… (C * 161) + (4 * 160) + …which could be converted to decimal with (C * 16) + (4 * 1) = (1210 * 16) + 4 = 19610 (Note that the symbols 0 -9 have the same value in decimal and hex)

  26. Hexadecimal: Base 16 The convenient thing about hex (which is not true of decimal) is that the base (16) is a power of 2, specifically 24 As a result, hex is a concise shorthand for binary, because each hex digit represents four binary digits.

  27. Equivalent Values

  28. Hexadecimal: Base 16 For example, we would write the binary equivalent of decimal 12 as 11002, or C16. For large binary numbers (like memory addresses), it is more convenient to write the address in hex. So 0100 1011 0010 01012 can be shortened to 4B2516, which programmers often write as 0x4B25 or just x4B25. That said, keep in mind that in memory, on your hard drive and on the Internet, binary is the only format that computers use! Decimal and hex are for humans!

  29. Simple Data Simple data elements all have a data type that defines its size and shape and an identifier that is an alias for its address in memory. The data type defines the possible set of values that a simple data element can have, and the operations that can be performed on the data.

  30. Simple Numeric Data Types The C# language defines a number of different kinds of data. In this course we will mainly use the following numeric data types: Type Storage Max Value int 32 bits - 2,147,483,647 to 2,147,483,648 double 64 bits over 10308

  31. Integers are whole numbers they have no fractional part. Integer operations include addition subtraction multiplication division remainder assignment Integer Numbers

  32. Examples of Integers 10 -5 327 2,905,301

  33. Real numbers have fractional parts Real numbers are often written in scientific format The most common real data type is double Operations on real numbers include addition subtraction division multiplication assignment Real Numbers

  34. Examples of Real Numbers 10.5 -5.02 327.981 2,905,301.004 0.0000239897 -1.56 X 10-4

  35. When interpreted as a character, certain bit patterns represent printable characters and control characters. Character Data Different standards exist for encoding characters The ASCII standard, finalized in 1968, uses 7 bits for each character. In the ASCII standard, 1000001 is interpreted as the character ‘A’. The 8 bit ASCII standard was added later to increase the number of possible characters that could be encoded. 7 bits only allows for the definition of 128 unique characters. Subsequent standards (ISO8859 and ISO10646) define much larger, multi-national character sets. However, both are supersets of ASCII. Character data is defined by the keyword char

  36. The ASCII Code Table 1st hex digit x41 = ‘A’ 2nd hex digit

  37. Character Representation Characters are stored in the computer’s memory in ASCII format. For example, using the standard ASCII code, the character ‘A’ would be stored as 0100 0001. C# actually uses a superset of ASCII called Unicode, that supports a multiple byte character code and The character ‘A’ is stored as 0000 0000 0100 0001.

  38. Characters * Characters are written in C# programs as 'A', 'B', etc. * 'A' is stored as 0100 0001 in memory or hex 0X41 or dec 65 * 'B' is stored as 0100 0010 in memory or hex 0X42 or dec 66 * 'a' is stored as 0110 0001 in memory or hex 0X61 or dec 97 * 'b' is stored as 0110 0010 in memory or hex 0X62 or dec 98

  39. Control Characters Control characters are characters that do not print, but cause some action, such as moving to a new line, to occur. In C# we write control characters as a backslash, followed by a character that denotes the action to be taken. '\b' backspace '\t' tab '\n' new-line '\r' carriage return

  40. A piece of Boolean data can only have one of two values: true false Boolean data is defined by the keyword bool Boolean Data

  41. A variable is a name for a memory location (address) that holds some piece of data. The value stored in that location may change during execution of the program; however, the type may not. A constant is a name for a memory location (address) that holds some piece of data, where the value of the data cannot change during execution of the program. Variables and Constants

  42. Declarations In C#, all variables and constants must be declared before they are used in a program. C# is what is known as a strongly typed language. This means that we must tell the compiler what the data type is for every variable. The compiler then checks all operations to make sure that they are valid for the given type of data.

  43. Question . . . Assume that you are able to peek into the memory of your computer, and you see the bit pattern 0000 0000 0000 0000 0000 0000 0110 0010 What does this bit pattern mean?

  44. The correct answer is that you don’t know. Unless you know what type of data you are looking at, it is impossible to interpret the bits stored in memory.

  45. Integer Representation In most modern digital computers, integer numbers are stored internally in binary. The number of bits used to store an integer in C# is 32 bits. Example: the integer 5 is 0000 0000 0000 0000 0000 0000 0000 0101 This is the sign bit, and when this bit is zero, the number is positive

  46. Floating Point Representation Numbers that contain decimal points are stored internally in a very different format. The exact format depends upon the processor used in the computer, but in general it looks like: sign exponent Mantissa or Coefficient for example, the number 6,045.03 (0.604503 x 104) would have sign of 0 an exponent of 4 and a mantissa of .604503 The actual binary representation is beyond the scope of this course.

  47. Computer Instructions Locations in memory can hold both data and instructions. A special register, called the program counter points in memory to the next instruction to be executed. The computer fetches the next instruction from memory. The program counter moves to the next instruction. The computer then decodes and executes the instruction it just fetched.

  48. Machine Language We call the instructions stored in computer memory machine language instructions. They are defined by the chip manufacturer. For example, the machine instruction 0011 0011 0001 1010 might mean something like take the byte stored in memory location 0024 and put it into the A register.

  49. Summary Integers straight binary representation Real Numbers split into sign, exponent and coefficient Characters coded bytes – Unicode an ASCII superset Instructions coded bytes – machine language

  50. Declaring a Variable this statement reserves space in computer memory for an integer. We can then refer to the data in this location using the name “someNumber” which is an alias for the the address in memory where the value is stored. int someNumber; char firstLetter; bool theAnswer; double density = 12.45; int hoursWorked = 14; char key = ‘g’; this statement reserves space in computer memory for a character. The bit pattern for ‘g’ is then stored in that location. We can now refer to the data in this location using the name “key” .

More Related