H61ICP - 2012/13
Key Definitions/Concepts
Problem
Definition
Algorithm
A
Program
An
H61ICP - 2012/13
2012/13
H61ICP Lecture 1
Cooking
Problem
Algorithm
Recipe
Program
H61ICP - 2012/13
2012/13
H61ICP Lecture 1
A Sample Problem
H61ICP - 2012/13
Analysis
What numbers are allowed?
Where does the number come
from?
What do even and odd mean?
How is the answer to be reported?
H61ICP - 2012/13
2012/13
H61ICP Lecture 1
H61ICP - 2012/13
An Algorithm
Test: 234784832792543
An alternate algorithm:
If the rightmost digit is 0, 2, 4, 6, or 8, write even
Otherwise, write odd
H61ICP - 2012/13
2012/13
H61ICP Lecture 1
Next, a C Program
Now
H61ICP - 2012/13
Program Sketch
H61ICP - 2012/13
2012/13
10
H61ICP Lecture 1
11
2012/13
12
H61ICP Lecture 1
Sample Execution
H61ICP - 2012/13
13
Text surrounded by /*
and */ are comments
essential.
2012/13
H61ICP Lecture 1
Variables
Variable declarations
create new variables
and specify their
names and types.
/* read a number */
#include <stdio.h>
int main (void) {
int num; /* input number */
int rem;
/* remainder */
/* get number from user */
printf(Please enter a number: );
scanf(%d, &num);
/* calculate remainder */
rem = num % 2;
if (rem == 0) {
printf(even\n);
} else {
printf(odd\n);
}
/* terminate program */
return 0;
}
H61ICP - 2012/13
Statements
of different kinds
Some (if, else, return) are part
of the C language proper
Others (scanf, printf) are
contained in libraries of routines
that are available for use in our
programs
For now, dont worry too much
about the distinction
/* read a number */
#include <stdio.h>
int main (void) {
int num; /* input number */
int rem;
/* remainder */
/* get number from user */
printf(Please enter a number: );
scanf(%d, &num);
/* calculate remainder */
rem = num % 2;
if (rem == 0) {
printf(even\n);
} else {
printf(odd\n);
}
/* terminate program */
return 0;
}
H61ICP - 2012/13
2012/13
15
16
H61ICP Lecture 1
Functions
/* read a number */
#include <stdio.h>
int main (void) {
int num; /* input number */
int rem;
/* remainder */
/* get number from user */
printf(Please enter a number: );
scanf(%d, &num);
/* calculate remainder */
rem = num % 2;
if (rem == 0) {
printf(even\n);
} else {
printf(odd\n);
}
/* terminate program */
return 0;
}
Functions are
sequences of
statements defined
elsewhere. Some
functions (such as printf
and scanf here) are
provided with the
system. We will also
learn how to write and
use our own functions.
H61ICP - 2012/13
Boilerplate
/* read a number */
#include <stdio.h>
int main (void) {
int num; /* input number */
int rem;
/* remainder */
/* get number from user */
printf(Please enter a number: );
scanf(%d, &num);
/* calculate remainder */
rem = num % 2;
if (rem == 0) {
printf(even\n);
} else {
printf(odd\n);
}
/* terminate program */
return 0;
}
H61ICP - 2012/13
2012/13
17
18
H61ICP Lecture 1
of 1s and 0s
for each processor family (x86, PowerPC, SPARC,
ARM, )
How
C?
H61ICP - 2012/13
19
A
Environments
2012/13
20
10
H61ICP Lecture 1
c
o
m
executable
program
p
i
library
(ANSI)
e
r
debugger
r
source
code
l
n
header
(stdio.h)
010
110
object
code
H61ICP - 2012/13
21
Even
Getting
2012/13
22
11
H61ICP Lecture 1
you want it to do
The C compiler cannot catch these kinds of errors!
They can be extremely difficult to find
They may not show up right away
H61ICP - 2012/13
23
Try It Yourself!
Type
2012/13
24
12
H61ICP Lecture 1
Wow!!
Weve covered a lot of new ideas
Algorithms and programs
Computer organization and memory
The basic components of C programs
Comments, declarations, statements
Compilers, linkers, libraries, and program
execution
Errors
Lots
of terminology, too
H61ICP - 2012/13
2012/13
25
13