Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www.human-memory.net/types_sensory.html
Input
Output
Devices Devices Computer
Input Buffer Processor
Devices
Permanent
Long term
Storage
Memory
Output devices allow us to get information out of the computer Input and output devices are known as peripherals
1
7/4/2020
keyboards
Input/output devices
What input/output devices can you think of?
Mice speakers
headphones Printers
2
7/4/2020
monitors scanners
COMPUTATION
3
7/4/2020
Introduction
• C programming language
– Structured and disciplined approach to program design
– Story Paragraph Sentence
– Program Function Statement
• #include <stdio.h>
– Preprocessor directive - tells computer to load contents of a certain file
– <stdio.h> allows standard input/output operations
4
7/4/2020
What How
New line printf( "\n" );
Type Example Size Range Format Code
Tab printf( "\t" );
char A B C..Z 8 bits, -128 to +127 “%c”
Alarm printf( "\a" ); a b c..z 1 byte
@ $ % *
Double quote printf( "\"" );
int 32, 3, 4 ,5 32 bits, -2,14,74,83,648 to “%d”
Slash(\) printf( "\\" ); -4, -5, -6 4 bytes +2,14,74,83,647 “%i”
float 3.4, 5.25, 32 bits, 3.4E-38 to 3.4E+38 “%f”
Percent (%) printf( “%%" ); -2.3, -6.7 4 bytes
double 3.4, 5.25, 64 bits, 1.7E-308 to “%lf”
-2.3, -6.7 8 bytes 1.7E+308
.692 0.1011
458.692 111001010.1011
5
7/4/2020
Representing characters
Memory Concepts
Represent characters as integral numbers. This is called Coding.
ASCII American Standard Code For Information Interchange • Variables
– Variable names correspond to locations in the
computer's memory.
– Every variable has a name, a type, a size and a value.
– Whenever a new value is placed into a variable
(through scanf, for example), it replaces (and
destroys) previous value
– Reading variables from memory does not change them
• A visual representation
a 45
b = 8; b = 8;
11 The value of c: 11
c = a + b; c = a + b;
Type Format Code
int a, b, c; int a, b, c;
a = 3; Output:
a = 3; Output:
b = 8; b = 8;
The value of c: 11 The value of c: 11
c = a + b; c = a + b;
printf("The value of c:"); printf("The value of c: %d",c);
printf("The value of c: %d",c);
printf("%d" , c);
6
7/4/2020
Key words in C
Ke yw o rd s
7
7/4/2020
• interest • First_tag
• balance • char
• year • Price$
• month • group one
• average_number
• int_
• 8boys
• Arithmetic operators:
Write down a program that will take two integers
as input and will print the results of their addition,
subtraction, multiplication and division.
C o p era tion Arithm etic Alg eb ra ic C exp ressio n
op era to r exp ressio n
#include <stdio.h>
Addition + f+7 f + 7
int main(){
Subtraction - p–c p - c
int a,b;
Multiplication * bm b * m
scanf(“%d%d”,&a,&b);
Division / x/y x / y
printf(“Addition: %d\n”,a+b);
Modulus % r mod s r % s printf(“Subtraction: %d\n”,a-b);
printf(“Multiplication: %d\n”,a*b);
printf(“Division: %d\n”,a/b);
A Rahman
Working with int…examples Interchanging content using
function?
• Interchange two numbers:
main(){
– Given integers as input, can you interchange their content? x y
int x, y;
scanf(“%d%d”,&x,&y);
int t;
t = x;
x = y;
y = t;
printf(“%d %d”,x,y);
Pointers 48
8
7/4/2020
9
7/4/2020
• Lower to higher auto-conversion (called auto-casting) In mathematics the floor, ceil and round of a fractional
int x = 9;
number x is defined as follows:
float y = x; //OK no warning no error floor(x) = the largest integer not exceeding x
ceil(x) = the smallest integer not below x
• Higher to lower still auto-casting but generates warning round(x) = the nearest integer to x
float x = 9.5;
According to the above definition when x = 2.3,
int y = x; //OK but generates warning but no error
floor(x) = 2, ceil(x) = 3 and round(x) = 2
int y = (int) x; // warning is gone
(a) If x = 2.7 what are its floor, ceil and round values?
• Work out the followings: (b) Write down a program that will take a positive
float x = 5/3; float x = 5.0/3; fractional number x as input and will print its floor and
int y = 5/3; int y = 5.0/3; rounded values (ceil will be shown later when we learn
x = 1.0 y = 1 x = 1.6667 y = 1 55
if-else).
• Arithmetic calculations are used in most programs • Find the values of the followings:
– Use * for multiplication and / for division
– Integer division truncates remainder
7 / 5 evaluates to 1 5+6*7-8/7 = 46
– Modulus operator returns the remainder
7 % 5 evaluates to 2
5*4/3 = 6
• Operator precedence
– Some arithmetic operators act before others (i.e., multiplication
before addition) 5.0*4/3 = 6.667
• Use parenthesis when needed
– Example: Find the average of three variables a, b and c
• Do not use: a + b + c / 3
• Use: (a + b + c ) / 3 5/(6+7) – 8%8 = 0
10
7/4/2020
Assignment Operators
(shorthand notations) Problem Solving Methodology (must
• Assignment operators abbreviate assignment expressions read)
c = c + 3;
can be abbreviated as c += 3; using the addition assignment
operator
1. State the problem clearly
• Statements of the form
variable = variable operator expression; 2. Describe the input/output information
can be rewritten as
variable operator= expression; 3. Work the problem by hand, give example
4. Develop a solution (Algorithm Development)
• Examples of other assignment operators:
d -= 4 (d = d - 4) and Convert it to a program (C program)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
5. Test the solution with a variety of data
(x1,y1)
11
7/4/2020
67 68
How will you find the distance between two other points (2,5) and (10,8)?
5. Testing
• After compiling your program, run it and see if it
gives the correct result.
• Your program should print out
The distance between two points is 3.61 x1=2, y1=5, x2=10, y2=8,
69 70
12