CS 16 Chapter 1 Computer Program A set of instructions fro a computer to follow Computer software The collection of programs used by a computer Includes Editors Translators System Managers Three main classes of computers PC personal use Workstation larger and more powerful than PC Mainframe 5 main components Input devices allows communication to the computer output devices allows communication to the use CPU processor Main memory Secondary memory Main memory Longlist of memory locations o each contains zeros and ones o can change during program execution Binary Digit or Bit o a digit that can only be zero or one Byte o each memory location has eight bits Address o Number that indentifies a memory location Compiler the input program is usually called the source program or source code and the translated version produced by the compiler is called the object program or object code Algorithm A sequence of precise instructions that leads to a solution Program An algorithm expressed in a language the computer can understand C cout This will be printed out to the terminal endl outputs endl ends the line and starts a new line CS 16 n starts new line simple program begins this way include iostream Using namepsace std Int main ends with closing bracket sample program include iostream using namespace std int main int number of pods peas per pod total peas cout Press return after entering a number n cout Enter the number of pods n cin number of pods cout Enter the number of peas in a pod n cin peas per pod total peas number of pods peas per pod cout If you have cout number of pods cout pea pods n cout and cout peas per pod cout peas in each pod then n cout you have cout total peas cout peas in all the pods n return 0 Press return after entering a number Enter the number of pods 10 Enter the number of peas in a pod 9 If you have 10 pea pods and 9 peas in each pod then you have 90 peas in all the pods int number of pods peas per pod total peas This line is called a variable declaration This variable declaration tells the computer that number of pods peas per pod and total peas will be used as names for three variables CS 16 The arrows written or tell you the direction that data is moving The arrows and are called insert and extract or put to and get from respectively For example consider the line cout Press return after entering a number n This line may be read put Press number n to cout or simply output Press number n cin number of pods This line may be read get number of pods from cin or simply input number of pods total peas number of pods peas per pod The asterisk symbol is used for multiplication in C so this statement says to multiply number of pods and peas per pod In this case 10 is multiplied by 9 to give a result of 90 cout If you have cout number of pods cout pea pods n These are just three more output statements that work basically the same as the previous statements that begin with cout The only thing that is new is the second of these three statements which says to output the variable number of pods When a variable is output it is the value of the variable that is output so this statement causes a 10 to be output Variables int is an abbreviation for integer Could store 3 102 3211 456 etc Number of bars is of type integer Double represents numbers with a fractional component Could store 1 34 4 0 345 6 etc 2 and 2 0 are not the same number n A whole number such as 2 is of type int n A real number such as 2 0 is of type double Chapter 4 Procedural Abstraction and Functions that return a value Top Down Design Break the algorithm in to subtasks Break each subtask in to smaller subtasks Eventually the smaller subtasks are trivial to implement in the programming language Predefined Functions Sqrt function the root sqrt 9 0 the root will contain 3 A function call can be used like any expression CS 16 bonus sqrt sales 10 Cout The side of a square with area area is sqrt area Function name Argument List Argument List is a comma separated list Argument 1 Argument 2 Argument Last Example side sqrt area cout 2 5 to the power 3 0 is pow 2 5 3 0 Random Number Generator 1 Seed the random number generator only once include cstdlib include ctime srand time 0 2 The rand function returns a random integer that is greater than or equal to 0 and less than RAND MAX rand Use and to scale to the number range you want For example to get a random number from 1 6 to simulate rolling a six sided die int die rand 6 1 Programmer Defined Functions Two components of a function definition Function declaration or function prototype o Shows how the function is called o Must appear in the code before the function can be called o Syntax Type returned Function Name Parameter List Comment describing what function does Function definition o Describes how the function does its task o Can appear before or after the function is called o Syntax Type returned Function Name Parameter List code to make the function work Example CS 16 double total cost int number par double price par const double TAX RATE 0 05 5 tax double subtotal subtotal price par number par return subtotal subtotal TAX RATE The return statement ends the function call Boolean Functions A function can return a bool value o Such a function can be used where a boolean expression is expected o Makes programs easier to read o if rate 10 rate 20 rate 0 is easier to read as if appropriate rate n If function appropriate returns a bool value based on the the expression above
View Full Document
Unlocking...