Computer architecture
Computer Systems 2nd Edition, Warford, J. Stanley
Chapter 2 Notes
Read chapter 2 sections 1 - 3 - C++
- Variables
- The C++ Compiler
- Write the program (source) using a text editor
- Invoke the compiler to translate to machine language
- Execute the object program
- Some compilers will combine some of these steps.
- Source file is a text file
- Once compiled to an executable you will not need to recompile again unless you change it.
- C++ Compiler is software not hardware.
- Machine Independence
- At this level (ISA3) languages are machine independence
- In order for a program to run on both types of computers though you will need a compiler for each type/os that you want to run it on
- Program 2.1
- C++ variable have three attributes
- Name - identifier - provided by programmer
- Type - what type of values it can have
- Value - the content of the variable
- Sample program for review
#include <iostream.h>
const int bonus = 5;
int exam1;
int exam2;
int score;
main ( )
{
cin >> exam1 >> exam2;
score = (exam1 + exam2) /2 + bonus;
cout << "score = " << score;
}
INPUT
86 72
OUTPUT
score = 84 - the #include <iostream.h> is a directive that allows the programmer to use >> and << to send information in and out of the computer.
- Variables in this program are of the type int meaning INTEGER
- The names are exam1, exam2, and bonus, they get their value form the input statement
- there is also a value called bonus which has a constant value (cannot be changed) which has a value of 5 These values are set outside the main program, so they are global, but they could be local (inside the main function)
- C++ variable have three attributes
- First line that can be executed is cin >> exam1 >> exam2;
which tells the program to read in from the input device two variables. - Next is score << (exam1 + exam2) / 2 + bonus;
this takes the values, adds them together, averages them and then adds a bonus and places it in the variable score - This uses the = symbol which is called the assignment operator
- cout << "score = " << score; uses the standard output (monitor) to output the statements to the screen
- The C++ Compiler
- Program 2.2
#include <iostream.h>
char ch;
main ( )
{
cin >> ch;
cout << "Original Charachter: " << ch << endl;
ch++;
cout << "Following charachter: " << ch << endl;
}
INPUT
d
OUTPUT
Original charachter: d
Following Charachter: e- char ch; creates a variable of character
- cout << "Original Charachter: " << ch << endl; makes that the output
- ch++; just adds 1 to the character value (same as character = character + 1)
- Last line outputs the next character
- Program 2.3
- Programs use statements to control the flow of the program, if and switch for selection and while, do and for are used for repetition
- The if statement checks to see if a statement comparing two values is true. Comparators are:
- == - Equal to
- < - Less than
- <= - less than or equa to
- > - Greater than
- >= - Greater than or equal to
- != - Not Equal to
- We can also combine the comparators with boolean operators
- && - AND
- || - OR
- ! - NOT
- If an if statement only has one statement after it, then you do not need { }, if it does, then you do need the { }
- In stead of having to use several if statements you can use a switch statement
- The switch is more efficient than the if statement
- The while will allow a loop(all the material inside the { }) to repeat until the boolean condtions are met.
- Flow loops back to the top of the loop till condition is met.
- do appears to not have any condition but it is at the exit of the loop
- The exit check is a while and checks for the condition to be met.
- It forces the loop to be done once
- Allows for a controlled count through things
- Has three parts to it
- initialize (x=0;
- test x < 4;
- increment x++)
- Two types of functions in C++ - ones that return a value and ones that do not.
- Ones that do not in some languages are called procedures
- When the program is run and we have to go to a function information is placed on the memory stack (push) and when it returns it takes it off the memory stack(pop)
- This is refereed to as LIFO - Last In First Out
- Program 2.8
- Program 2.9
- Program 2.10
- Some functions will pass values with the function call
- In C++ this is done within the ( ) that are named after the function
No comments:
Post a Comment