+ All Categories
Home > Documents > CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer...

CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer...

Date post: 19-Dec-2015
Category:
View: 223 times
Download: 3 times
Share this document with a friend
Popular Tags:
50
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT
Transcript

CIS 101: Computer Programming and Problem Solving

Lecture 8Usman Roshan

Department of Computer Science

NJIT

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

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)

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

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

C++ basics

C++ basics

Header file includes functionsfor input/output

C++ basics

Main function is executed whenyou run the program. (Later we willsee how to pass it parameters)

Same function in MATLAB

Main function definition

C++ basics

Explicitly define integers

Same function in MATLAB

No need to specify thatthese are integers.

C++ basics

cout is an operator used to display output to the screen.

cout syntax: cout << <variable> << <variable> << …;

Same function in MATLAB

Use disp for output

C++ basics

Function has return type of integer.Note that this has to be specified.

Same function in MATLAB

No need to specify returntype.

Compiling a C++ program

Click on Build and then Compile.this will now translate this sourcecode into machine code, whichis the CPU language.

Compiling a C++ program

No errors and no warnings.Machine code has been generated.

Building a C++ program

Now we build. This means linking thecompiled file (which is in machine code)with other machine code required to runthe program on the CPU.

For example when we #include other filesthen their machine code is linked with machinecode for this file. The end-result is one largefile containing machine code for the CPU.This is also called the executable file.

Now we run the executable

Clicking on the ! buttonruns the exectuable.

C++ program output

C++ basics

Reading data using cin operator

C++ basics

Reading data using cin operator

All three integers can be definedin one line

Same function in MATLAB

Using input to read data

Output of this program

C++ program output

C++ program output

What if we entered numbers with decimal points (i.e. reals)?

C++ program output

Average of 4 and 0.5 = (4+0.5)/2 = 4.5/2 = 2.25. This says 0 because we specifiedthe variable type as int and not float. So the 0.5 (the second number) gets floored to 0.

MATLAB handles this automatically

PROGRAM OUTPUT

Let’s fix the C++ program:using floats now

Now we are asking for floats(real numbers, not necessarily integers)

C++ program output

It works!

C++ arrays

Array syntax

C++ arrays

Array syntax

Array of fixed size(cannot be changed)

C++ arrays

Array syntax

Array of fixed size(cannot be changed)MATLAB: A(1)

C++: A[1]

C++ arraysIMPORTANT: C++ arrays start from 0, MATLAB start from 1

C++ array output

Same function in MATLAB

Output of MATLAB

Modifying arrays

Let us add a sixth number to the arrayWill this work?

Modifying arrays---compile

It compiled fine!

Modifying arrays---link

It also linked fine!

But fails to run!

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.

C++ if and for loops

• Very similar to MATLAB, except no end is required.

• Statements must be grouped within { and }.

C++ loops

Specify an array of size 3

C++ loops

Specify an array of size 3

Loop construct(have to stay within3 otherwise you willget an error)Similar to MATLABexcept for parenthesis

C++ loops

Specify an array of size 3

Loop construct(have to stay within3 otherwise you willget an error)

Loop statements are enclosed within { and }

C++ loops---output

C++ loops and if

If-else construct:Similar to MATLAB exceptthat (1) conditional expressionis inside paranthesisand (2) { } are used to enclosestatements to execute ifcondition is true (or false)

C++ loops and if --- output

Now let’s go to the LAB!


Recommended