Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.

Similar presentations


Presentation on theme: "Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data."— Presentation transcript:

1 Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data Types  Formatting of numbers (printf)

2 Commenting  Commenting your source code is very important:  Comments help user identify sections of a large program.  Comments help others understand structure of program and how program operates.  A some point, you will be required to read someone else’s C programming code!

3 Commenting  All comments within C source code must begin with /* symbol and end with */ symbol.  Examples:  /* This is a comment */  /* This is a comment over 2 lines */

4 Inputting Data from Keyboard  So far, we have only stored data in variables and displayed data, but a computer program should allow for data to be inputted (for example: the keyboard).  The scanf command is used in C to input data from the keyboard to be stored as a variable in the computer’s internal memory

5 Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } You need to initialize a variable in order to store inputted data

6 Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } A printf command is used to prompt user to enter data (note no newline!)

7 Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } Since printf statement does NOT move down one line, provide a space for appearance

8 Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } A format specifier is used to indicate the numeric data type. In this case “%d” represents an integer.

9 Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } &age indicates the location in the computer’s internal memory that will hold inputted data

10 Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } The format specifier is used to represent the value of the variable age in the display.

11 In Class Exercise  Write a C program to read your height (in centimeters - no decimals) and store in a variable called “height” and display the message: My height is x centimeters! (where x represents your height)

12 Arithmetic Operators  Now that numerical data can be stored in variables, we can perform mathematical calculations to convert data into information.  Arithmetic Operators include:  Multiplication (*), Division (/), Modulus (%)  Addition (+) and Subtraction (-)

13 Arithmetic Operators  Modulus (%) is used only with integers to determine the remainder of a number (e.g.. Examples:  7 % 3 = 4  20 % 3 = 2 (How the ##?@@!! did you get that?!?)

14 Arithmetic Order of Operations  To avoid arithmetical errors, it is important to understand in what order the computer perform math calculations:  First:calculations in brackets ( )  Second:multiplication, division, modulus (in order of appearance)  Third:addition & subtraction (in order of appearance)

15 In Class Exercise  Calculate the results of the following expressions as if you were the computer: 3 * 4 / 2 =  5 + 2 * 10 / 2 =  (7 - 3) / 2 =  2 + ((15 / 3) * 7) =  4 % 2 =  100 % 33 =  7 + 23 % 16 =

16 Mixing Different Numeric Data Types  The type of numeric data type may have significant results on the outcome of a calculation!  You should choose the right numerical data type for the variable to prevent mathematical errors which would cause errors in your program!

17 Mixing Different Numeric Data Types  The following chart displays what data type to initialize for storing math result:  Double: When performing math on just doubles, both integers & doubles, or dividing two integers (otherwise, decimal values get “cut-off” or truncated)  Integer: Multiplying, adding, subtracting, or modulus of integers.  Note: Due to advancement of PC’s you are encouraged to use doubles instead of floats

18 In Class Exercise  What would be the best numerical data type for the following variables that store these calculations? (note: variables x & y are integers and variables a & b are doubles)  x + y + 1  x / (y + 3)  x + a - b  a * x + y / b

19 Formatting Numbers with printf  How do we make display of numbers look nice (“line –up”)?  To display the variable cars (which is a double) as currency ($ xx.xx): printf (“The sales amount is: $ %.2lf\n”, cars); %.2 lf tells printf to display only 2 decimal place for the variable (which is a double)

20 In Class Exercise –Write a program called minute_convert to prompt for and store the total number of minutes (assume the total number of minutes is greater than 60). Convert the number of minutes into hours and minutes. E.g. –120 minutes is the same as 2 hour(s) and 0 minutes. –75 minutes is the same as 1 hour(s) at 15 minutes.


Download ppt "Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data."

Similar presentations


Ads by Google