[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 12
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] <12> [13] [14] [15] [16] [17] [18] [19] [20]
Podcasts: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]
Labs: [01] [02] [03] [04] [05] [06] [07] [09] [10]
Fri Feb 15 12:03:35 PST 2008

Contents


    Chapter 5: pages 148-157 Numeric data

      Reading

        The following Introduction is also available [ 12.mp3 ] as a podcast.

        Introduction

        The beginning of Chapter 5 of Skansholm reviews the numerical data that C++ programs can work with. It explains binary notation for integers and the properties of the predefined integer types. There is a list on page 152. In CS201 we will mostly work with simple "int"s. The key property of the integer data types is that they are fast, simple, precise, and have a limited range. All computer experts know about how they work.

        The "anagram" function on page 153 shows an interesting trick.

        Section 5.2 describes the floating point types: float, double, and so on. Here you need to remember that floating point notation is slower and less accurate than integer notation. On the other hand it covers a much wider range of possibilities. And as the numbers get bigger, so do the rounding errors! Notice the word 'mantissa' and 'exponent'.

        The last section in this piece -- 5.3 describes the sizeof operator. Notice the lake of a space between 'size' and 'of'. It is not that important in CS201. It is worth noting the trick for calculating number of elements in an array on page 157. It is subtle and works.

        5.1 Integer Types

        5.1.1 How integers are stored in a computer

        Binary notation.

        5.1.2 Predefined Integer types -- unsigned short long int char

        page 153 -- anagram

        Cunning trick....

        5.2 Floating-point types

        5.2.1 Storing real numbers in computers

        5.2.2 Predefined floating-point types -- float double long double

        5.3 The sizeof operator

        How to calculate the number of elements in an array:
         		sizeof f / sizeof f[0]

      . . . . . . . . . ( end of section Reading) <<Contents | End>>

      Jargon and Glossary

      1. byte::information= bit^8, 8 bits.
      2. bit::information={0,1}.
      3. nibble::information=bit^4, 4 bits.
      4. 1 byte == 2 nibbles.

      5. short::data_type=used to store smaller integers -- whole numbers, use for counting things when you have to save a little bit of space.
      6. int::data_type=used to store integers -- whole numbers, use for counting things.
      7. long::data_type=used to store larger integers -- whole numbers, use for counting things or when multiplying many integers together.
      8. float::data_type=used for real numbers when precision is not important, good for old or small computers.
      9. double::=data_type=used for real numbers when precision is important, use for all measurements. You may be able to get even more accuracy by using "long double".
      10. char::data_type=used to store a single 7, 8 or 9 bit character, these days use ASCII.
      11. wchar::data_type=used to store a single 16 bit characters, brand new and uses UNICODE probably.
      12. string::data_type=used to store a sequence of numbered char.

      13. data_type::=a collection of objects that are stored in similar formats and have the same operations and operators.

      Syntax

      Questions

        How is numeric data important

        Counting and measuring are key human activities. Starting with money, lengths, land, volumes, weight, ... we have always needed to compute with numbers -- or get cheated. So understanding how numeric data works (and sometimes fails) is a useful piece of knowledge.

        What predefined data types are most useful in CS201

        Which predefined data types will we actually be using

        Use int to count and scan, use double for measurements, and strings+chars for non-numeric data.

        How does a computer work

        A computer has a control unit(think: dumb brain), an arithmetic-Logical Unit (ALU) (think: calculator), primary memory(think: working space), secondary memory(think: file cabinet), and input/output(think: in and out trays).

        The primary memory is divided into numbered memory cells each holding one byte of information.

        A control unit has a program counter (PC) that counts instructions and follows the Van Neuman cycle:

        1. Get the next instruction from location PC.
        2. Decode the instruction into operations and the address of data.
        3. Get the data form primary memory and move it to the arithmetic logic unit.
        4. Tell athe ALU to do the operation
        5. Send the result back to somewhere in memory.
        6. Compute a new value for PC and return to the first step.

        Note: some instructions skip steps. For example, Branch instructions often just move the address into the PC.

        What determines the size of data types on a machine

        Nearly all machines have a fixed size for int's and one for doubles. I've only know one where you can change the "word size" as it ran. It didn't survive in the market place.

        What is a float

        In C++ it is a number that has a floating decimal point but is not as large, slow, or precise as double length. The details do not matter much!

        Is there a limit on the size of floating point numbers.

        Yes ... also on the precision. But it all depends on the CPU you have and the compiler you are using. However there is a library <float.h> or <cfloat> that defines constants like FLT_MAX, DBL_MAX, and LDBL_MAX the maximum representable number in each of float, double, and long double.

        The <limits.h> or <climits> library does something similar for chars and its.

        By the way, these days all CPUs are using the IEEE Standard floating point notation (ANSI/IEEE Std 754-1985) which defines the limits and precisions.

        Notice: all computation is limitted by the ammount of memory available.

        What is the purpose of the sizeof operator

        It tells you how many bytes the compiler has given to a variable or to a data type. The lab will give you many examples... .

        How do bits store information

        A bit can be made to stand for a single true/false or 1/0 decision.

        Two bits can handle four different cases. All we need to do is to decide what each combination means. Similar 8 bits can handle 8 different meanings. Here is how we would make them encode unsigned and signed numbers and as Days of the week:
        BitsUnsigned #Signed #Day of Week
        00000Sun
        00111Mon
        01022Tue
        01133Wed
        10044Thu
        1015-3Fri
        1106-2Sat
        1117-1ERROR

        The information has to be encoded as data. In effect we give each bit a particular meaning. We usually do it character by character. But good examples include the UPC code or the International codes for Airports.

        Exercise: work out a one letter code for the days of the week.

        Exercise: work out a two letter code for the months.

        Who put bhinary code together

        I think it was Liebnitz in the 1600's.

        How are letters changed to binary

        When you tap a letter on the key board a particular set of contacts is made.

        The ASCII code specified that 'A' was 01000001 and that B would be next... and so on upto 'Z', 'a' starts with 01100001, and so on upto 'z'.

        It was a committee decision by the American Standards people.

        Does every key on the keyboard have a special number

        The main keyboard A-Z0-9....{}[];:....!@#$... are all one byte ASCII numbers. The arrow and function keys use a sequence of 2,3, or 4 one byt codes developed by ANSI...

        Does the operating system determine the word size -- Vista comes in 32-bit and 64-bit versions

        I think that this indicates the size of an address in programs running under the operating system. I don't think it changes the ammount of data the CPU handles in one cycle. I think that the sizeof operator may well give the same sizes, for example, whatever the operating system is.

        Why are there 8 different predefined data types

        This is how C evolved.

        The book doesn't mention some of them like wchar (wide character).

        What is the method used to store positive and negative numbers

        It is called twos-complement... The Wikipedia article [ Two's_Complement ] is a good description of hiw it all works and why.

        Is there a negative unsigned data type

        No. Not really needed. You just write the code with a normal unsigned number and imagine it is negative as you write the code.

        Can you give a good example that will help with the next quiz

        Who said that the next quiz depended on data types?

        When is void used

        We put void in a function definition to idicate it doe not retrun any reliable data.

        The is another use, the infamous 'void*' which may mention but leave for CS202 to explain in detail.

        Can you write a program to search a MS Excel spreadsheet for data and take you to it

        Probably. I've done searches like this in other spreadsheets.

        What does bool mean

        The keyword bool is short for Boolean, which refers to the name of George Boole. This link [ Boolean ] into the Wikipedia will lead you to more than you need to know about things Boolean.

        In what ways can pointers be used....

        Next time [ 13.html ]

      . . . . . . . . . ( end of section Questions) <<Contents | End>> .Next -- Quiz 6 Lab 7 Project 5 [ projects.html#P5 ] [ 13.html ] [ lab06/ ]

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. OOP::="Object-Oriented Programming", Current paradigm for programming.
  4. SP::="Structured Programming", Previous paradigm for programming.
  5. TBA::="To Be Announced", something I should do.
  6. TBD::="To Be Done", something you have to do.
  7. UML::="Unified Modeling Language", [ 15.html ] (class notes on the UML and OOP).

End