CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Similar documents
Chapter 1: Introduction to Computers and Programming

Chapter 1 INTRODUCTION

A First Program - Greeting.cpp

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Introduction. Arizona State University 1

CSc Introduction to Computing

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Exercise 1.1 Hello world

Chapter 1: Why Program? Computers and Programming. Why Program?

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

Fundamentals of Programming CS-110. Lecture 2

CS2141 Software Development using C/C++ C++ Basics

Introduction to Programming EC-105. Lecture 2

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Computer System and programming in C

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

REVIEW. The C++ Programming Language. CS 151 Review #2

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Integer Data Types. Data Type. Data Types. int, short int, long int

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CS242 COMPUTER PROGRAMMING

6.096 Introduction to C++ January (IAP) 2009

VARIABLES & ASSIGNMENTS

Lecture 2 Tao Wang 1

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Objectives. In this chapter, you will:

Week 0: Intro to Computers and Programming. 1.1 Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

The C++ Language. Arizona State University 1

Exercise: Using Numbers

1. In C++, reserved words are the same as predefined identifiers. a. True

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Computational Physics Operating systems

LECTURE 02 INTRODUCTION TO C++

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Creating a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Lab # 02. Basic Elements of C++ _ Part1

BASIC ELEMENTS OF A COMPUTER PROGRAM

BITG 1233: Introduction to C++

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

UNIT- 3 Introduction to C++

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Unit 3. Constants and Expressions

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Chapter 1 Introduction to Computers and C++ Programming

Getting started with C++ (Part 2)

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Computer Programming : C++

Chapter 1 Introduction to Computers and Programming

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CSc 10200! Introduction to Computing. Lecture 1 Edgardo Molina Fall 2013 City College of New York

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

FORM 2 (Please put your name and form # on the scantron!!!!)

Programming. C++ Basics

2 nd Week Lecture Notes

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Types, Variables, and Constants

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Input And Output of C++

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

BITG 1113: Introduction To Computers And Programming Language LECTURE 1 LECTURE 1 1

printf( Please enter another number: ); scanf( %d, &num2);

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

C++ For Science and Engineering Lecture 15

C++ Programming: From Problem Analysis to Program Design, Third Edition

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

Exercise: Inventing Language

BTE2313. Chapter 2: Introduction to C++ Programming

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Chapter 2: Overview of C++

Scientific Computing

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

Computer Basics 1/24/13. Computer Organization. Computer systems consist of hardware and software.

Homework #3 CS2255 Fall 2012

The type of all data used in a C++ program must be specified

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Week 3: Pointers (Part 2)

The type of all data used in a C (or C++) program must be specified

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

CHAPTER 3 BASIC INSTRUCTION OF C++

COMP 202 Java in one week

Transcription:

CS1500 Algorithms and Data Structures for Engineering, FALL 2012 Virgil Pavlu, vip@ccs.neu.edu Jose Annunziato, jannunzi@gmail.com Rohan Garg Morteza Dilgir Huadong Li cs1500hw@gmail.com http://www.ccs.neu.edu/home/vip/teach/cpp_eng/ index.html Intro to Programming C++ basics Some slides from Chinmoy Dutta

computer systems computers are programmable - that s why they are so useful hardware components - physical components a computer is made of. software components - programs that run on a computer. CPU - Central Processing Unit CU - Control Unit ALU - Arithmetic and Logic Unit main memory - RAM secondary storage - hard drive, optical drive... hardware input devices - keyboard, mouse, microphone, webcam... output devices - monitor, printer...

software operating systems - manages the hardware devices and control their processes applications - solve user specific problems program a set of step-by-step instructions that tells a computer how to perform a given task

machine language a computer executes machine language instructions fetch/decode/execute cycle a machine instruction is a long sequence of 0 s and 1 s :-( we need something more sane!!! programming language a high-level language of giving instruction to computers BASIC, FORTRAN, COBOL, PASCAL, C, C++, Java, Javascript, Python...

Pascal (***************************************************************************** * A simple bubble sort program. Reads integers, one per line, and prints * * them out in sorted order. Blows up if there are more than 49. * *****************************************************************************) PROGRAM Sort(input, output); CONST (* Max array size. *) MaxElts = 50; TYPE (* Type of the element array. *) IntArrType = ARRAY [1..MaxElts] OF Integer; VAR (* Indexes, exchange temp, array size. *) i, j, tmp, size: integer; (* Array of ints *) arr: IntArrType; (* Read in the integers. *) PROCEDURE ReadArr(VAR size: Integer; VAR a: IntArrType); BEGIN size := 1; WHILE NOT eof DO BEGIN readln(a[size]); IF NOT eof THEN size := size + 1 END END; BEGIN (* Read *) ReadArr(size, arr); Ruby # Simple for loop using a range. for i in (1..4) print i," " end print "\n" for i in (1...4) print i," " end print "\n" # Running through a list (which is what they do). items = [ 'Mark', 12, 'goobers', 18.45 ] for it in items print it, " " end print "\n" # Go through the legal subscript values of an array. for i in (0...items.length)

Matlab % script demonstrates how to print a line containing both: % - literal text % - text converted from a numeric variable r = input('enter radius: '); a = pi*r^2; disp(['radius = ' num2str(r) ' area = ' num2str(a)]); Our first C++ program // this program expresses our feelings # include <iostream> using namespace std; int main(){ cout << I hate learning C++ at 8AM in the morning << endl; return 0;

compilation Algorithm = Solution How do we conceptually design programs? pseudocode = conceptual program, human language initialize j=0,g=1; for i = 1 to 10 j=j+i g = g*i print sum is j, product is g

C++ basics variables locations in memory they have identifier (name, case sensitive) restrictions on names address data type (this also determines size) value (this can vary as program runs)

literals also known as constants (contrast with variables) a fixed value of a given data type which does not change they can be assigned to variables x=1500 data types A way of categorizing variables and literals this determines how much size is needed to store a value of a given data type this also determines which operations are permitted on a value of a given data type

range of a data type the set of values a data type can represent more the space allocated for a data type, larger is the range (obvious) integer data types short (2 bytes) unsigned short (2 bytes) int (4 bytes) unsigned int (or simply unsigned) (4 bytes) long (4 bytes) unsigned long (4 bytes)

floating -point data types float (4 bytes) double (8 bytes) long double (8 bytes) notes on size Note that signed and unsigned variants are allocated equal amount of space, and hence their ranges are of the same size the given sizes are typical; amount of space allocated may vary with system; only guarantee is that int is at least as large as short, long at least as large as int, double at least as large as float, and long double at least as large as double

character data type char ( 1 byte) represented as 1 byte integers, hence we can treat them as numbers! conversion between the numeric value and the character done using ASCII table boolean data type bool range consists of only two values true - represented as 1 false - represented as 0

variable declaration <data type> <variable identifier>; int num; double score=0; char letter= A ; char letter2 =66; bool flag; assignment and initialization assignment num = 10; letter = A ; (note the single quotes) initialization (assignment while declaring) int num = 10; char letter = A ;

named constants just like variables (with identifier, address, data type and value), but the value is initialized at the time of declaration and cannot be changed declared with keyword - const const PI = 3.1415; note the initialization typically names are chosen uppercase why use named constants do not need to modify code at several places when the value changes (like the interest rate), just need to modify the initialization no need to write values like that of PI repeatedly; less chance of error makes code readable and easy to understand

binary representation (8 bits) positives: sign bit=0 negatives: sign bit=1, stands for -128 two complement 0 = 00000000 1 = 00000001 2 = 00000010 3 = 00000011 4 = 00000100... 127 = 01111111-128 = 10000000-127 = -128+1 =10000001-126 = -128+2 =10000010-125 = -128+3 =10000011...... range : -128 to 127