Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009

2 Remaining Course Timeline Aug 3 - C/C++ Aug 5 - Matlab as a Tool Aug 10 - Final Review Aug 12 - Final, Project 2 due/demos

3 Lecture Overview Introducing C++ C++ vs. MATLAB Simple C++ programs Data Types Scope Loops Functions

4 A programming language Derived from a language called C Ubiquitous Object Oriented ◦ Easier to write large scale projects ◦ Reusability High Performance

5 Supports data security Helps code reuse Allows multiple functions/operators with same name

6 Large projects Systems applications Graphics Data Structures Want to speed up your scripts

7 When not to use C++ Small programs Prototyping (MATLAB, scripting languages) Web applications (javascript)

8 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

9 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

10 #include using namespace std; int main() { // display output cout << “Hello World\n”; } First C++ Program (helloworld.cpp)

11 $ CC –o helloworld helloworld.cpp $./helloworld Hello World $

12 First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; }

13 First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } A library providing input and output functionality

14 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

15 First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } All C++ programs have a main function

16 First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } A comment

17 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

18 C++ Variables Must introduce variables before using “Double quotes” for strings Input and output int studentAge; cout << “How old are you?”; cin >> studentAge;

19 Arrows “show the way” data if flowing cout << … going out (output) cin >> … going in (input)

20 cout << “Student Age” << 20 << endl; int i; float f; cin >> i; cin >> f;

21 C++ Data Types

22 Integer short, int, long Floating point float, double, long double Logical bool (values: true, false) Character char Text String (“Hello World”)

23 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

24 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.

25

26 C++ Scopes Curly braces { } introduce a new block Creates a Scope Functions create a scope Loops and if statements create a scope

27 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! }

28 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

29 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

30 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]; }

31 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]; }

32 Break

33 C++ Function Definition Example 1: int main() { cout << “Hello World” << endl; } Example 2: int squareArg(int x) { return x * 2; }

34 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

35 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; }

36 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; }

37 Putting all together Create a function to sum number from 1 to N What do we need?

38 Putting all together

39

40 Declaring of C++ arrays A typical declaration for an array is: type name [elements]; For example: int grades[60]; // int array of 60 elements

41 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};

42 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};

43 C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] 10 235 77 40 1207 quantity

44 C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] 10 235 77 40 1207 quantity A=quantity [0]; After this executes A has value 10.

45 C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] 10 235 77 40 1207 quantity quantity [3] = 75; After this executes quantity[3] will have value 75.

46 C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] 10 235 77 75 1207 quantity quantity [3] = 75; Statement has executed.

47 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; }

48 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[]) { … }

49 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

50 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

51 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

52 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?

53 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

54 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


Download ppt "General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009."

Similar presentations


Ads by Google