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

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Advertisements

Chapter 11 Introduction to Programming in C
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
Programming in Visual Basic
True or false A variable of type char can hold the value 301. ( F )
Chapter 10.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Reasons to study concepts of PL
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways.
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
CSE : Programming in C Instructor: Lei Wang Office: Dreese Lab 474 Office Hour: Friday.
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
Computer Science 210 Computer Organization Introduction to C.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 1.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
Which Language is Better?
Programming With C.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Object-Oriented Programming in C++
1 Original Source : and Problem and Problem Solving.ppt.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 12 Functions – Part IV
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Programming 2 Intro to Java Machine code Assembly languages Fortran Basic Pascal Scheme CC++ Java LISP Smalltalk Smalltalk-80.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Software Engineering Algorithms, Compilers, & Lifecycle.
Memory Management in Java Mr. Gerb Computer Science 4.
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Computer Science 210 Computer Organization
Objectives Identify the built-in data types in C++
Computer Science 210 Computer Organization
Variables Title slide variables.
Scope Rules and Storage Types
Presentation transcript:

CIS 101: Computer Programming and Problem Solving Lecture 8 Usman 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

Header file includes functions for input/output

C++ basics Main function is executed when you run the program. (Later we will see 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 that these are integers.

C++ basics cout is an operator used to display output to the screen. cout syntax: cout << …;

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 return type.

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.

Compiling a C++ program No errors and no warnings. Machine code has been generated.

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.

Now we run the executable Clicking on the ! button runs the exectuable.

C++ program output

C++ basics Reading data using cin operator

C++ basics Reading data using cin operator All three integers can be defined in one line

Same function in MATLAB Using input to read data

Output of this program

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

MATLAB handles this automatically PROGRAMOUTPUT

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++ arrays IMPORTANT: 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 array Will 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 within 3 otherwise you will get an error) Similar to MATLAB except for parenthesis

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 }

C++ loops---output

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)

C++ loops and if --- output

Now let’s go to the LAB!