Scanf / Arithmetic Operators / Formatting Display with numerical

Download Report

Transcript Scanf / Arithmetic Operators / Formatting Display with numerical

Agenda
 Commenting
 Inputting Data from Keyboard (scanf)
 Arithmetic Operators
()*/+-%
 Order of Operations
 Mixing Different Numeric Data Types
 Formatting of numbers (printf)
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!
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
*/
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
Example of Program with scanf
#include <stdio.h> You need to initialize
main ()
a variable in order to
store inputted data
{
int age;
printf (“Please enter your age: ”);
scanf (“%d”, &age);
printf (“you are %d years old.\n”, age);
}
Example of Program with scanf
#include <stdio.h> A printf command is used
main ()
to prompt user to enter
data (note no newline!)
{
int age;
printf (“Please enter your age: ”);
scanf (“%d”, &age);
printf (“you are %d years old.\n”, age);
}
Example of Program with scanf
#include <stdio.h> Since printf statement does
NOT move down one line,
main ()
provide a space for
{
appearance
int age;
printf (“Please enter your age: ”);
scanf (“%d”, &age);
printf (“you are %d years old.\n”, age);
}
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.
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 location
in the computer’s internal memory
that will hold inputted data
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.
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)
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 (-)
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?!?)
Arithmetic Order of Operations
To avoid arithmetical errors, it is important
to understand in what order the computer
perform math calculations:
 First:
 Second:
 Third:
calculations in brackets ( )
multiplication, division, modulus
(in order of appearance)
addition & subtraction
(in order of appearance)
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 =
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!
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
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
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)
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.