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);
}
Enter length: 10 [Entered]
Enter width: 8 [Entered]
The area equals 80
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;
cout << "This will output to the screen";
cin >> variable; //enter a value and that value will be stored in "variable".
return(0);
}
right brace.
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***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 10In 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
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
int number1, number2, number3;
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
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).
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)
#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);
}
int myInteger = 3.5; // myInteger = 3
cout << myInteger ; // 3
bool boolValue;
boolValue = true; // true and false are keywords in C++
cout << boolValue << endl; // 1
boolValue = false;
cout << boolValue << endl; // 0
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