Thursday, May 8, 2014

Wk2-Mtg2 - II-6 Format Strings*, II-7 Numbers*, II-8 Loops (pt1)

Review: 

  We began this meeting by going over questions found while reading this chapter.  We answered the question "What is an argument". In doing so we referred to the Wikipedia page on "Parameter". We notice on this page that parameters are also called arguments to the function, subroutine, or method.

II-6 Format Strings*

This section looks at our printf() in a little more detail and what we are doing with it.  (Formatting directives and escape sequences as intro to formatting numbers).

*Challenge (squarer)

In this exercise we are asked to compute the simple square of a number and display the number we squared and the result with specific formatting (put the numbers in quotes).   This allows us to demonstrate how to display multiple integers along with using escape sequences so that we can print characters which are normally directives to the compiler.

II-7 Numbers*

Integer Numbers

This section looks at Integer numeric data-types, their sizes, their  signed'ness, how to display them and what operations can be done with them.

NOTE: a key takeaway is reinforced! :
A data-type specifies, in a sense, two things:
  1. How much storage the variable will occupy
  2. how to interpret the stored value/content.
For example (looking at just the char datatype): 

  • char:
    occupies: 8-bits/1 Byte,
    interpret as: most-significant bit is the sign, remaining seven bits are the value
    - range of possible values:[-128 to +127]
  • unsigned char:
    occupies: 8-bits/1 Byte,
    interpret as: all 8 bits are the value
    - range of possible values:[0 to 255]
NOTE: We can use the general whole number types: char, short, int, long, etc. but we must be aware that their allocated size in bytes will be architecture/platform specific
These sizes are NOT consistent for all types, across all platforms!

-or-

However, in C, if  we #include <machine/types.h>, we can then use size-specific types (e.g., for 8- thru 64-bit-widths, we have intN_t and u_intN_t
   where N is replaced with one of [8,16,32,64] as in u_int8_t, int16_t and u_int64_t.) 
These sizes are exactly the size we specify independent of architecture/platform!

NOTE: NSInteger and NSUInteger are the same size as long and unsigned long (meaning long and NSInteger along with their unsigned counterparts are 32-bits-wide on 32-bit architectures and 64-bits-wide on 64-bit architectures!)

Next, we looked at printf() displaying of numbers appropriate to their datatype sizes and how to display values in various number bases. (using %d, %u, %o, and %x - NOTE(1): %o and %x ignore the signed-ness as we intend for all bits to be shown in the output. NOTE(2) %x displays the a-f values in lower case, while %X displays them in upper case)

Then we looked at integer math operations and operator precedence.  We note that divide in the integer case is simply truncating the remainder.  Then we note that we have a modulus (%) operator with which we can obtain the remainder part of the division instead of the quotient.

lastly we reviewed the shorthand operators: increment (++) and decrement(--) along with operators +=, -=, *=, /= and even %=.

Floating point Numbers

This section reviews the sizes (float, double and long double) of floating-point numeric data-types and explains the fundamentals of how floating point numbers are stored (as two signed integers: mantissa and exponent).  it reviews the print formatting directives for displaying floating point numbers with decimal notation using %f and as scientific notation using %e.   Lastly the math library which is commonly needed is made available by adding #include <math.h> at the top of our C source file.  We also noted in this chapter that all of the trigonometry related functions found in this header use values in Radians, not Degrees!

*Challenge ({noname})

In this exercise we are asked to use a routine from the math library to compute the sin of 1 radian. We are also asked to control the formatting of the floating point result to having 3 places to the right of the decimal.

II-8 Loops (part 1)

This section introduces the first flow-control statements; the while-loop,  how it can be represented in many cases by a for statement.  it speaks to leaving the loop early using the break statement and we can skip processing values within the loop by using the continue statement.  lastly the variant form the do-while loop was introduced.

 [NOTE: we wanted better flowcharts for our two loops so I've linked to the Wikipedia pages for these loops (above)!  Notice their pre-test-loop and post-test-loop nomenclature.  Very "reminding" of the difference between the two structures! -ed]

Note: we have not completed this Loops Section with this session. We will do so in our next meeting.

Homework: For the next meeting, in addition to the reading assignment identified in the syllabus, also review this post and all pages which are directly linked from this post.

No comments:

Post a Comment