Week 2

Ch.1 Introduction to Computers and Programming

  • Hardware - physical components of a computer (anything you can phyiscally touch. Some examples are CPU, main memory, secondary storage devices, input/output devices.
  • Sofware - refers to actual programs and their functions. (Anything you cannot touch). Some examples would be the operating system, utility programs, applications, games, developer tools, etc.

In this class we will first focus on developing programs in a computer language known as C/C++ (mainly C++). Computers follow instructions and the CPU can only read instructions in machine language with binary numbers (00000001001010101000100000100000) {mips 32bit instruction -> add $s1 $t1 $t2 assembly lang} and to us, we cannot comprehend this. This is called "Low Level Language". So we use a "High Level Language" in order to more easily comprehend and develop code. Here is an example of some code written in C++. Although you may not understand entirely, from context clues you can understand that we are calculating the Area.



    #include <iostream>
    using namespace std; 
    
    int main() { 
      int length; 
      int width; 
      //Get length and width.  
      cout << "Enter length: ";
      cin >> length;
      cout << "Enter width: ";
      cin >> width;

      //Calculate Area
      cout << "The area equals " << length * width << endl;
      return(0);
    }


***Executed Output***

Enter length: 10 [Entered]

Enter width: 8 [Entered]

The area equals 80


Components of a High Level Language's Source Code
  • Key Words - certain words are reserved for the programming language. For example, in C/C++ the words "int" and "return" cannot be used as a user defined variable.
  • User defined variables - as a programmer, you may define your own variable names. For example, the names length and width are programmer-identifiers.
  • Operators - operators such as * / + - = are commonly used for arithmetic.
  • Punctuation and Syntax - good code is something that is readable and easy for humans to comprehend. Spacing and line entering are vital for good code. The use of comments is good practice for yourself whenever you become lost and for others to understand, something that we will practice all semester.

Ch. 2 Introduction to C/C++

In the coded example above we have the line #include <iostream>
it starts with a # and is a preproccessor directive. This allows the preproccessor to set up certain source code or header files. iostream is a header file for cin/cout uses.

using namespace std;
This is the standard namespace used by iostream. The uses of namespaces wont be discussed in this class, but know that for every program you create, you must include these two lines at the top/head of your program.

Next we have int main() which is the name of the C++ function. Although some C++ programs have many functions, in every C++ there will always be a main function. The next line is the left brace { which is a character used to contain the code pertaining to that function.

The use of comments are vital to programmers by having short notes or an explanation of certain parts of code. Comments are ignored by the compiler by the use of two // characters. Anything after the "//" is a comment and the compiler will ignore the rest of the line. An alternative is /* this is a multi-lined comment */.

Next is the declaration of integer variables called "length, width". These variables hold an integer numeric value entered in in by the user's keyboard.

 int length;
 int width; 
  . . .
 cout << "Enter width:  "; 
 cin >> width; 

syntax example
cout << "This will output to the screen";
cin >> variable; //enter a value and that value will be stored in "variable".

Notice that the >> and << are different for cin and cout.
Also notice that there is a semi-colon " ; " at the end of each line of code. This semi colon is required and marks the end of a statement.

return(0);
This sends an integer of value 0 back to the OS, ending the program, thusly, the program executed and exited successfully. Immediately after return(0); close the function with the closing } right brace.

cout object

cout allows text to be displayed to the screen. We can manipulate the look, feel, and behavior of text displayed to our liking.

 
    #include <iostream>
    using namespace std; 
    int main() { 
      //example1  - one line
      cout << "My name is John"; 
      cout << endl; 

      //example2  - multi lines 
      cout << "My name "; 
      cout << "is John"; 
      cout << endl; 

      //example3 - chaining output
      cout << "My name" << " is John" << endl; 
      
      //example4 - using newLine escape sequence "\n"
      cout << "My name \nis John \n"; 

      return(0); 
    } 
  
**Executed***
My name is John
My name is John
My name is John
My name
is John
***end***
Two ways to create a new line with cout
  • stream manipulator - endl (endl line)
  • escape sequence - "\n" must be inside of the "quotes" (less typing than endl)


Variables

A variable is a place in memory where the programmer may create a data type and the name or variable definition. We will discuss more about Data Types later, bur for now, we'll use the data type "int". The declaration "int number;" means the programmer has declared a variable named "number" that may only hold integer values (...,-2, -1, 0, 1, 2, ...).

 int number;
 number = 10; 
 cout << "Number equals: " << number << endl;  // Number equals 10 
In this example, we use the "=" (assignment operator) to give the variable an numeric integer value of 10. The usefulness of a variable is that variables can be changed.
 int number;
 number = 10; 
 cout << "Number equals: " << number << endl;  // Number equals 10 
 number = 4;   // we overwrite the pre-existing value that number contains 
 cout << "Number equals: " << number << endl;  // Number equals 4

The name of a variable can be anything except for Key Words. C++ has a list of key words (all lowercase), and if you use a key word for a variable identifier, there will be errors.
int x; vs. int numPeople;
When declaring variables, it is more understandable and readable" if you named them semantically (with meaning).
The first character of the name may be and only be [a..z, A..Z, _]. After the first character, the rest of the name can only be [a..z, A..Z, 0..9, _].
Day, l33t, Dog44, _numOfpeople, Num_of_people : all legal variable names.
3number, Cat#5, Number Of People : all illegal variable names.

Data Types, Sizes, and Ranges

Name        Description      Size*   Range*
char        Character        1byte   signed: -128 to 127 unsigned: 0 to 255
short int   Short Integer    2bytes  signed: -32768 to 32767 unsigned: 0 to 65535
int         Integer          4bytes  signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
long int    Long Integer     4bytes  signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
bool        Boolean value    1byte   true or false
float  Floating Point Number 4bytes  +/- 3.4e +/- 38 (~7 digits)
double Floating Point Number 8bytes  +/- 1.7e +/- 308 (~15 digits)
long double   F_P_Number    16bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t     Wide character   2 or 4 bytes    1 wide character

You can also declare multiple variables in one line.
For example: int number1, number2, number3;


char Data Type

The char data type goes for character data type (a..z, symbols). The size of a char is 1-byte.
char letter = 'A';
cout << letter << endl; // displays the letter A
letter = 'B'; // overwrite letter to = 'B'
cout << letter << endl; // displays the letter B

What would happen if you gave a char an int value? (refer to ASCII chart)
char letter = 65;
cout << letter << endl; // displays the letter A
letter = 66; // overwrite letter to = 'B'
cout << letter << endl; // displays the letter B

C++ string class

A char can only store one char, but a string may hold up to multiple char. In order to use a string, we must include the "string" header file.
#include <iostream>
#include <string> // needed for string
using namespace std;

int main(){
string myName;
myName = "John Doe"; // notice the use of "double-quotes"
cout << "My name is " << myName << endl; // My name is John Doe

return(0);
}

We will discuss data type later in the semester (so for now, don't worry about it, just 'know' about it).


float and double Data Types

A float/double uses whole numbers (a fractional value between integers) What if you were asked to write a program that delt with money and the use of cents. In this case you use a floating point to represent the cent ammount -- $1.75. (Just think: Decimal --> double)

  • float 4 bytes [+- 3.4E-38 and +- 3.4E38]
  • double 8 bytes [+-1.7E-308 and +- 1.7E308]
#include <iostream>
using namespace std;

int main() {
float amountHave = 11.48;
float amountOwe = 10.12;
float total = amountHave - amountOwe; // 11.48 - 10.12 = 1.36
cout << "My total is $" << total << endl; // My total is $1.36

return(0);
}
Assigning float to int
int myInteger = 3.5; // myInteger = 3
cout << myInteger ; // 3

bool Data Type

A boolean variable is either true or false. They hold the logic value by assigning the boolean variable an integer value of 1 or 0. 1 for true, 0 for false. We'll practice more with boolean variables in Ch4.

bool boolValue;
boolValue = true; // true and false are keywords in C++
cout << boolValue << endl; // 1
boolValue = false;
cout << boolValue << endl; // 0

Declaring, Initializing, and Assigning values to variables

Stated earlier, you may declare more than one variable at a time. You may also initialize a value at the same time.

int number1 = 1, number2, number3 = 3; // Declared variables number1 number2 number3
// number1 and number3 were initialized, but number2
// remains uninitialized (not given a value)
number2 = 2; // assigns 2 to number2
cout << number1 <<" " << number2 << " " << number3 << endl; // 1 2 3


More details on variables here

cplusplus variables <-- Link

Operators - a brief overview

  • Assignment Operator: <var> = <constant expression>;
  • Arithmetic Operators: + - * / %
  • Arithmetic Assignment: += -= *= /= %=
  • Comparison Operators: < <= > >= == !=
  • Logic Operators: && || !
  • Bitwise Operators: << >> & | ^ ~ (we will not be using these operators for this class)


Top