High Level Survey
-
Upload
muhammad-rana-farhan -
Category
Documents
-
view
216 -
download
0
Transcript of High Level Survey
-
8/14/2019 High Level Survey
1/32
High-level survey Problems, algorithms, and programs
Problem solving and program design
Compiling and running a C program
Errors and debugging
Focus on the big ideas Many details to cover in future lectures
Reading Sec. 1.3-1.5; start reading Ch. 2.
B-1
-
8/14/2019 High Level Survey
2/32
ProblemDefinition of task to be performed (often by a computer)
Algorithm
A particular sequence of steps that will solve a problemSteps must be precise and mechanicalThe notion of an algorithm is a (the?) fundamentalintellectual concept associated with computing
ProgramAn algorithm expressed in a specific computer
programming language (C, C++, Java, Perl, )
B-2
-
8/14/2019 High Level Survey
3/32
Programming Cooking
Problem Make fudge brownies
Algorithm Recipe
Program Recipe written in a specificlanguage (English, Russian,
Chinese, Latvian, etc.)
B-3
-
8/14/2019 High Level Survey
4/32
Clearly specify the problemAnalyze the problem
Design an algorithm to solve theproblem
Implement the algorithm (write theprogram)
Test and verify the completed program
B-4
-
8/14/2019 High Level Survey
5/32
Is a given number even or odd?
B-5
-
8/14/2019 High Level Survey
6/32
What numbers are allowed?Where does the number comefrom?
What do even and odd mean?
How is the answer to be reported?
B-6
-
8/14/2019 High Level Survey
7/32
Given an integer number typed in fromthe keyboard, If it is even, write even on the screen If it is odd, write odd on the screen
B-7
-
8/14/2019 High Level Survey
8/32
Read in the number Divide the number by 2 If the remainder is 0, write even Otherwise, write odd
B-8
-
8/14/2019 High Level Survey
9/32
Read in the number Divide the number by 2 If the remainder is 0, write even
Otherwise, write odd
B-9
Test: 234784832792543
-
8/14/2019 High Level Survey
10/32
Read in the number Divide the number by 2 If the remainder is 0, write even Otherwise, write odd
B-10
Test: 234784832792543
An alternate algorithm:If the rightmost digit is 0, 2, 4, 6, or 8, write even
Otherwise, write odd
-
8/14/2019 High Level Survey
11/32
Now that have an algorithm, we wouldlike to write a C program to carry it out.
But first, what is a program? In fact,what is a computer?
B-11
-
8/14/2019 High Level Survey
12/32
CPU or processor: executes simple instructions manipulatingvalues in memory
B-12
CentralProcessingUnit (CPU)
MainMemory
Monitor
Network
Disk
Keyboardmouse
-
8/14/2019 High Level Survey
13/32
The CPU executes instructions one after theother.
Such a sequence of instructions is called aprogram
Without a program, the computer is just
useless hardware
Complex programs may contain millions ofinstructions
B-13
-
8/14/2019 High Level Survey
14/32
Memory is a collection of locations calledvariables
Each variable has
Aname(anidentifier)Atype(the kind of information it can contain)
Basic types includeint (integers whole numbers: 17, -42)
double(floating-point numbers with optional fraction
and/or exponent: 3.14159, 6.02e23)char(character data: a, ?, N, , 9)
B-14
-
8/14/2019 High Level Survey
15/32
Ask the user to enter a number Read the number and call it num Divide num by 2 and call the remainder rem
Ifrem is 0 write even otherwise write odd
The actual program has LOTS ofdetails IGNORE THEM FOR NOW Pay attention to the main ideas
B-15
-
8/14/2019 High Level Survey
16/32
/* read a number and report whether it is even or odd */ #include
intmain (void) { intnum; /* input number */ intrem; /* remainder after division by 2 */
/* get number from user */ printf(Please enter a number: ); scanf(%d, &num);
B-16
-
8/14/2019 High Level Survey
17/32
/* calculate remainder and report even or odd */ rem = num % 2; if(rem == 0) { printf(even\n); }else{ printf(odd\n); } /* terminate program */
return0; }
Remember: Dont sweat the details!!! (for now)
B-17
-
8/14/2019 High Level Survey
18/32
B-18
-
8/14/2019 High Level Survey
19/32
Text surrounded by/*and*/arecommentsUsed to help the reader understandthe programIgnored during program execution
Programs change over time. Itsimportant that programmers beable to understand old code - good
comments are essential.
/* read a number */#includeintmain (void) { intnum; /* input number */ intrem; /* 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 */ return0;}
B-19
-
8/14/2019 High Level Survey
20/32
Variabledeclarations createnew variables and specify theirnames and types.
/* read a number */#includeint 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 */ return0;}
B-20
-
8/14/2019 High Level Survey
21/32
Following the declarations arestatementsthat specify the operations the program isto carry outLots of different kindsSome (if, else, return) are part of the Clanguage properOthers (scanf, printf) are contained inlibrariesof routines that are available for use in ourprogramsFor now, dont worry too much about the
distinction
/* read a number */#includeintmain (void) { intnum; /* input number */ intrem; /* 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;}
B-21
-
8/14/2019 High Level Survey
22/32
Functions are sequences ofstatements defined elsewhere. Somefunctions (such as printf and scanfhere) are provided with the system.We will also learn how to write anduse our own functions.
/* read a number */#includeintmain (void) { intnum; /* input number */ intrem; /* 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;}
B-22
-
8/14/2019 High Level Survey
23/32
Some parts of the program arestandard utterances that need tobe included at the beginning andend.
Well explain all of thiseventuallyJust copy it for now in each ofyour programs
/* read a number */#include int main (void) { intnum; /* input number */ intrem; /* 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;}
B-23
-
8/14/2019 High Level Survey
24/32
The computers processor only understandsprograms written in its ownmachinelanguage
Sequences of 1s and 0s
Different for each processor family (x86, PowerPC,SPARC, ARM, )
How is it that it can obey instructionswritten in C?
B-24
-
8/14/2019 High Level Survey
25/32
There are two steps in creating an executableprogram starting from C source code
A program called the Ccompilercompilertranslates the C code intoan equivalent program in the processors machinelanguage (1s and 0s)
A program called thelinkerlinkercombines this translatedprogram with any library files it references (printf, scanf,etc.) to produce an executable machine language program(.exe file)
Environments like Visual Studio do both steps whenyou build the program
B-25
-
8/14/2019 High Level Survey
26/32
B-26
library(ANSI)header
(stdio.h)
executableprogram
com
piler
l
i
nk
e
r
sourcecode
objectcode
.c file 011010001101
-
8/14/2019 High Level Survey
27/32
Lots!Things are rarely perfect on the first attempt
Both the compiler and linker could detect errors
Even if no errors are are detected, logic errors(bugs) could be lurking in the code
Getting the bugs out is a challenge even forprofessional software developers
B-27
-
8/14/2019 High Level Survey
28/32
Syntax:Syntax:the required form of the programpunctuation, keywords (int, if, return, ), wordorder, etc.
The C compiler always catches these syntaxerrors or compiler errors
Semantics (logic):Semantics (logic):what the program meanswhat 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
B-28
-
8/14/2019 High Level Survey
29/32
Type in the even/odd program and seewhat happens when you:
Leave off a few semicolons or misspellsomething (syntax)
In the last printf statements, change odd toeven. Run the program. What happens if youenter 17? (semantics)
Experiment and see what happens
B-29
-
8/14/2019 High Level Survey
30/32
Weve covered a lotof new ideasAlgorithms and programsComputer organization and memoryThe basic components of C programsComments, declarations, statementsCompilers, linkers, libraries, and programexecutionErrors
Lots of terminology, too
B-30
-
8/14/2019 High Level Survey
31/32
Upcoming lectures: review what weveseen today and fill in details
Meanwhile, get started reading andtrying things on the computer!
B-31
-
8/14/2019 High Level Survey
32/32