Monday, August 25, 2003

Computer architecture


Computer Systems 2nd Edition, Warford, J. Stanley


Chapter 2 Notes


Read chapter 2 sections 1 - 3 - C++
  1. Variables
    1. The C++ Compiler
      1. Write the program (source) using a text editor
      2. Invoke the compiler to translate to machine language
      3. Execute the object program
      4. Some compilers will combine some of these steps.
      5. Source file is a text file
      6. Once compiled to an executable you will not need to recompile again unless you change it.
      7. C++ Compiler is software not hardware.
    2. Machine Independence
      1. At this level (ISA3) languages are machine independence
      2. 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
    3. Program 2.1
      1. C++ variable have three attributes
        1. Name - identifier - provided by programmer
        2. Type - what type of values it can have
        3. Value - the content of the variable
      2. 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

      3. the #include <iostream.h> is a directive that allows the programmer to use >> and << to send information in and out of the computer.
      4. Variables in this program are of the type int meaning INTEGER
      5. The names are exam1, exam2, and bonus, they get their value form the input statement
      6. there is also a value called bonus which has a constant value (cannot be changed) which has a value of 5
      7. These values are set outside the main program, so they are global, but they could be local (inside the main function)
      8. First line that can be executed is cin >> exam1 >> exam2;
        which tells the program to read in from the input device two variables.
      9. 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
      10. This uses the = symbol which is called the assignment operator
      11. cout << "score = " << score; uses the standard output (monitor) to output the statements to the screen
    4. Program 2.2

      1. #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
      2. char ch; creates a variable of character
      3. cout << "Original Charachter: " << ch << endl; makes that the output
      4. ch++; just adds 1 to the character value (same as character = character + 1)
      5. Last line outputs the next character
  2. Flow of Control
    1. Program 2.3
      1. Programs use statements to control the flow of the program, if and switch for selection and while, do and for are used for repetition
      2. The if statement checks to see if a statement comparing two values is true. Comparators are:
      3. == - Equal to
      4. < - Less than
      5. <= - less than or equa to
      6. > - Greater than
      7. >= - Greater than or equal to
      8. != - Not Equal to
      9. We can also combine the comparators with boolean operators
        1. && - AND
        2. || - OR
        3. ! - NOT
      10. If an if statement only has one statement after it, then you do not need { }, if it does, then you do need the { }
    2. Program 2.4 -(Switch Statement)
      1. In stead of having to use several if statements you can use a switch statement
      2. The switch is more efficient than the if statement
    3. Program 2.5 - (While loop)
      1. The while will allow a loop(all the material inside the { }) to repeat until the boolean condtions are met.
      2. Flow loops back to the top of the loop till condition is met.
    4. Program 2.6 - do
      1. do appears to not have any condition but it is at the exit of the loop
      2. The exit check is a while and checks for the condition to be met.
      3. It forces the loop to be done once
    5. Program 2.7 (for loop)
      1. Allows for a controlled count through things
      2. Has three parts to it
        1. initialize (x=0;
        2. test x < 4;
        3. increment x++)
  3. Functions
    1. Two types of functions in C++ - ones that return a value and ones that do not.
    2. Ones that do not in some languages are called procedures
    3. 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)
    4. This is refereed to as LIFO - Last In First Out
    5. Program 2.8
    6. Program 2.9
    7. Program 2.10
      1. Some functions will pass values with the function call
      2. In C++ this is done within the ( ) that are named after the function

No comments:

Post a Comment