agenda commenting inputting data from keyboard (scanf) arithmetic operators ( ) * / + - % order...
TRANSCRIPT
![Page 1: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/1.jpg)
Agenda
Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators
( ) * / + - % Order of Operations Mixing Different Numeric Data Types
Formatting of numbers (printf)
![Page 2: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/2.jpg)
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!
![Page 3: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/3.jpg)
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 */
![Page 4: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/4.jpg)
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
![Page 5: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/5.jpg)
Example of Program with scanf
#include <stdio.h> main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); }
You need to initializea variable in order tostore inputted data
![Page 6: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/6.jpg)
Example of Program with scanf
#include <stdio.h> 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!)
![Page 7: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/7.jpg)
Example of Program with scanf
#include <stdio.h> main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); }
Since printf statement doesNOT move down one line,provide a space for appearance
![Page 8: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/8.jpg)
Example of Program with scanf
#include <stdio.h> 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.
![Page 9: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/9.jpg)
Example of Program with scanf
#include <stdio.h> main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); }
&age indicates the locationin the computer’s internal memory that will hold inputted data
![Page 10: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/10.jpg)
Example of Program with scanf
#include <stdio.h> 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.
![Page 11: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/11.jpg)
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)
![Page 12: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/12.jpg)
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 (-)
![Page 13: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/13.jpg)
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?!?)
![Page 14: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/14.jpg)
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)
![Page 15: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/15.jpg)
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 =
![Page 16: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/16.jpg)
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!
![Page 17: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/17.jpg)
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
![Page 18: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/18.jpg)
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
![Page 19: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/19.jpg)
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)
![Page 20: Agenda Commenting Inputting Data from Keyboard (scanf) Arithmetic Operators ( ) * / + - % Order of Operations Mixing Different Numeric Data](https://reader035.vdocuments.us/reader035/viewer/2022081813/56649e9e5503460f94ba01fa/html5/thumbnails/20.jpg)
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.