Download presentation
Presentation is loading. Please wait.
1
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT
2
C++ I recommend two textbooks for C/C++ –The C Programming Language by Kernighan and Ritchie (Creators of C) –The C++ Programming Language by Stroustrup (Creator of C++) We will learn C++ syntax and solve problems on quizzes and mid-term using C++. We will also learn how to use C++ classes
3
MATLAB vs C++ Variables –MATLAB: everything is an array –C++: integers, floats, char, array, classes Memory allocation –MATLAB: EASY! nothing to do –C++: More work; must define variable before using it (we will see examples in the lab) Also have to free up allocated space (MATLAB automatically does it for you) Pointers –MATLAB: no pointers –C++: gives programmer more flexibility to optimize programs, but can introduce dangerous bugs! (if there is time we will see pointers) Loops and if constructs –Similar: that is good news! Running programs –MATLAB: easy, just click on run button! –C++: must compile first, and then execute (we will see later what this means)
4
So why C++? With MATLAB and C++ there is a safe and easy programming environment vs efficiency (time and memory) tradeoff MATLAB is easy but can be slow (it can also produce nice plots and graphics) C++ is more flexible and can be much faster NOTE: MATLAB is reasonably fast (as some benchmarks show) and continues to improve
5
Interpreter vs compiler Interpreter –Used in MATLAB –Read-check-execute loop –Slow but good for learning –Other examples are Perl and Java (Java compilers are also available now) Compiler –C++, C, Pascal, Fortran –Checks program syntax and translates it into machine code for the processor –Machine code can then run on the processor –Stand-alone (doesn’t require interpreter) and usually much faster
6
C++ basics
7
Header file includes functions for input/output
8
C++ basics Main function is executed when you run the program. (Later we will see how to pass it parameters)
9
Same function in MATLAB Main function definition
10
C++ basics Explicitly define integers
11
Same function in MATLAB No need to specify that these are integers.
12
C++ basics cout is an operator used to display output to the screen. cout syntax: cout << …;
13
Same function in MATLAB Use disp for output
14
C++ basics Function has return type of integer. Note that this has to be specified.
15
Same function in MATLAB No need to specify return type.
16
Compiling a C++ program Click on Build and then Compile. this will now translate this source code into machine code, which is the CPU language.
17
Compiling a C++ program No errors and no warnings. Machine code has been generated.
18
Building a C++ program Now we build. This means linking the compiled file (which is in machine code) with other machine code required to run the program on the CPU. For example when we #include other files then their machine code is linked with machine code for this file. The end-result is one large file containing machine code for the CPU. This is also called the executable file.
19
Now we run the executable Clicking on the ! button runs the exectuable.
20
C++ program output
21
C++ basics Reading data using cin operator
22
C++ basics Reading data using cin operator All three integers can be defined in one line
23
Same function in MATLAB Using input to read data
24
Output of this program
25
C++ program output
26
What if we entered numbers with decimal points (i.e. reals)?
27
C++ program output Average of 4 and 0.5 = (4+0.5)/2 = 4.5/2 = 2.25. This says 0 because we specified the variable type as int and not float. So the 0.5 (the second number) gets floored to 0.
28
MATLAB handles this automatically PROGRAMOUTPUT
29
Let’s fix the C++ program: using floats now Now we are asking for floats (real numbers, not necessarily integers)
30
C++ program output It works!
31
C++ arrays Array syntax
32
C++ arrays Array syntax Array of fixed size (cannot be changed)
33
C++ arrays Array syntax Array of fixed size (cannot be changed) MATLAB: A(1) C++: A[1]
34
C++ arrays IMPORTANT: C++ arrays start from 0, MATLAB start from 1
35
C++ array output
36
Same function in MATLAB
37
Output of MATLAB
38
Modifying arrays Let us add a sixth number to the array Will this work?
39
Modifying arrays---compile It compiled fine!
40
Modifying arrays---link It also linked fine!
41
But fails to run!
42
C++ arrays Size cannot be modified unless dynamic variables are defined (we will look at that later). These are static variables. This means their size is fixed. In MATLAB you can add to the array and it will automatically allocate (and free up) space in memory.
43
C++ if and for loops Very similar to MATLAB, except no end is required. Statements must be grouped within { and }.
44
C++ loops Specify an array of size 3
45
C++ loops Specify an array of size 3 Loop construct (have to stay within 3 otherwise you will get an error) Similar to MATLAB except for parenthesis
46
C++ loops Specify an array of size 3 Loop construct (have to stay within 3 otherwise you will get an error) Loop statements are enclosed within { and }
47
C++ loops---output
48
C++ loops and if If-else construct: Similar to MATLAB except that (1) conditional expression is inside paranthesis and (2) { } are used to enclose statements to execute if condition is true (or false)
49
C++ loops and if --- output
50
Now let’s go to the LAB!
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.