General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009
Remaining Course Timeline Aug 3 - C/C++ Aug 5 - Matlab as a Tool Aug 10 - Final Review Aug 12 - Final, Project 2 due/demos
Lecture Overview Introducing C++ C++ vs. MATLAB Simple C++ programs Data Types Scope Loops Functions
A programming language Derived from a language called C Ubiquitous Object Oriented ◦ Easier to write large scale projects ◦ Reusability High Performance
Supports data security Helps code reuse Allows multiple functions/operators with same name
Large projects Systems applications Graphics Data Structures Want to speed up your scripts
When not to use C++ Small programs Prototyping (MATLAB, scripting languages) Web applications (javascript)
C++ versus MATLAB MATLAB uses an interpreter To run can just type and use matlab commands C++ uses a compiler Program converted to machine code with compiler
C++ versus MATLAB MATLAB uses dynamic typing Introduce new variables as needed Type of variable discovered by use C++ uses static typing Variables have to be introduced Type is given when introduced
#include using namespace std; int main() { // display output cout << “Hello World\n”; } First C++ Program (helloworld.cpp)
$ CC –o helloworld helloworld.cpp $./helloworld Hello World $
First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; }
First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } A library providing input and output functionality
First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } Can use names in the standard C++ library
First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } All C++ programs have a main function
First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } A comment
First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } Send the string Hello World\n to console
C++ Variables Must introduce variables before using “Double quotes” for strings Input and output int studentAge; cout << “How old are you?”; cin >> studentAge;
Arrows “show the way” data if flowing cout << … going out (output) cin >> … going in (input)
cout << “Student Age” << 20 << endl; int i; float f; cin >> i; cin >> f;
C++ Data Types
Integer short, int, long Floating point float, double, long double Logical bool (values: true, false) Character char Text String (“Hello World”)
C++ Data Types short quantity = 63; int carValue = 0; // always initialize variables!! carValue = 57000; quantity = carValue; carValue = quantity; The variable quantity cannot store big numbers
C++ Data Types char Letter = ‘a’; string greeting = “This is a string”; greeting = Letter; Letter = greeting; The variable Letter cannot store strings; Think: A character is a string but a string is not a character.
C++ Scopes Curly braces { } introduce a new block Creates a Scope Functions create a scope Loops and if statements create a scope
C++ Scopes int main() { int x = 19; // x is known in all of main if (x == 19) { int y = 20; cout << “x + y is “ << x + y << endl; } // y is not visible here! }
For Loops in C++ Syntax for (initial expression; condition; concluding expression) { //Code } Slightly more complex that MATLAB’s Syntax for variable = start : increment : end %Code end
For Loops – A Comparison In MATLAB ◦ You create a vector of values by specifying A start point An end point Maybe an increment In C++ ◦ You set the loop parameters by specifying An initial condition A condition Concluding condition
For Loops – An Example Accessing ten elements in an Array In MATLAB for i = 1:10 x = array(i) end In C++ for (int i = 0; i < 10; i++) { int x = array[i]; }
For Loops – An Example Accessing every other element in an Array In MATLAB for i = 1:2:10 x = array(i) end In C++ for (int i = 0; i < 10; i+=2) { int x = array[i]; }
Break
C++ Function Definition Example 1: int main() { cout << “Hello World” << endl; } Example 2: int squareArg(int x) { return x * 2; }
C++ Function Definition Example 2: 1. int squareArg(int x) { 2. return x * 2; 3. } 1. Return type, function name, argument lists 2. Function body a) 0 or more return statements 3. Close curly brace
C++ Function Definition Function can take multiple arguments Example 3: int Power(int x, int n) { int powerof = x; for (int i =2; i<=n i++) { powerof = powerof * x; } return powerof; }
C++ Function Calling Main function calls the function squareArg. Example 2: int squareArg(int x) { return x * 2; } int main() { int x = squareArg(100); cout << “square of 100 is ” << x << endl; }
Putting all together Create a function to sum number from 1 to N What do we need?
Putting all together
Declaring of C++ arrays A typical declaration for an array is: type name [elements]; For example: int grades[60]; // int array of 60 elements
Initializing C++ arrays When we declare array we can assign initial values to each element Enclose the values in braces { } For example: int quantity [5] = {10, 235, 77, 40, 12071};
Initializing C++ arrays Can leave the square brackets empty [ ] Array size matches number of elements between {} For example: int quantity[] = {10, 235, 77, 40, 12071};
C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity
C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity A=quantity [0]; After this executes A has value 10.
C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity quantity [3] = 75; After this executes quantity[3] will have value 75.
C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity quantity [3] = 75; Statement has executed.
Simple Array Example #include using namespace std; int main() { int quantity[] = {16, 2, 77, 40, 12071}; int result = 0; for (int i = 0; i<5; i++) { result = result + quantity[i]; } cout << result; }
Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … }
Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } element type
Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } Array variable name
Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } Pair of empty brackets
Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } What is missing?
Example of Passing an Array #include using namespace std; int sumElements(int anArray[], int length) {... } int main() { int quantity[] = {16, 2, 77, 40, 12071}; int length = 5; int result = sumElements(quantity, length); cout << “Sum of Elements: ” << result << endl; } // rest of program on next slide
Project 2 tips Lots of comments! Clear reports on individual contribution Modularized code ◦ Use lots of functions If it has > 6-7 lines of code it probably does more than one task ◦ Test each function that has outputs