Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Similar documents
Scientific Computing

Welcome to MCS 275. Course Content Prerequisites & Expectations. Scripting in Python from OOP to LAMP example: Factorization in Primes

COMP322 - Introduction to C++ Lecture 01 - Introduction

EP241 Computer Programming

Your First C++ Program. September 1, 2010

C++ for Python Programmers

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

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Computer Science II Lecture 1 Introduction and Background

Introduction to Programming

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Understanding main() function Input/Output Streams

Introduction to Programming using C++

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

MA400: Financial Mathematics

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

Computer Programming : C++

Tutorial 2: Compiling and Running C++ Source Code

Compiling C/C++ programs

Exercise 1.1 Hello world

Chapter 1 Introduction to Computers and C++ Programming

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Chapter 1 INTRODUCTION

CE221 Programming in C++ Part 1 Introduction

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Tutorial-2a: First steps with C++ programming

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Introduction to C ++

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

CHAPTER 3 BASIC INSTRUCTION OF C++

CS 11 C++ track: lecture 1

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Midterm Practice TA: Brian Choi Section Webpage:

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

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

ECE250: Algorithms and Data Structures C++ Language Tutorial

Basic Computer Programming for ISNE. Santi Phithakkitnukoon ผศ.ดร.ส นต พ ท กษ ก จน ก ร

Unified Modeling Language a case study

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

CS 215 Fundamentals of Programming II Spring 2011 Project 2

C++ For Science and Engineering Lecture 2

4. Structure of a C++ program

(0) introduction to the course. how to learn a programming language. (0) course structure

Getting started with C++ (Part 2)

Security Coding Module - Buffer Overflow Data Gone Wild CS1

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Chapter 2: Introduction to C++

Computer Programming. Dr. Fahad Computer Science Deptt.

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

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++

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

CS 106B, Lecture 1 Introduction to C++

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

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

Inheritance and Polymorphism

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS11 Introduction to C++ Fall Lecture 1

Templates and Vectors

Objectives. In this chapter, you will:

Topics. bool and string types input/output library functions comments memory allocation templates classes

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

The C++ Language. Arizona State University 1

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

CS11 Intro C++ Spring 2018 Lecture 1

Introduction to Scientific Computing with C++ Introduction Textbook Assignments Lab Accounts and Software HELLO ADD INTS MANDEL Conclusion

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Computer Programming

LECTURE 02 INTRODUCTION TO C++

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

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

Cours de C++ Introduction

Bruce Merry. IOI Training Dec 2013

A First Program - Greeting.cpp

Lecture 3 Tao Wang 1

Your first C and C++ programs

UEE1302 (1102) F10: Introduction to Computers and Programming

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

(the bubble footer is automatically inserted into this space)

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

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CS 376b Computer Vision

CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Data Structures and Algorithms

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

CSCE Practice Midterm. Data Types

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

CS201 Latest Solved MCQs

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

Course Information and Introduction

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Transcription:

Welcome to MCS 360 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements MCS 360 Lecture 1 Introduction to Data Structures Jan Verschelde, 28 August 2017 Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 1 / 27

Introduction to Data Structures 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 2 / 27

Catalog Description more or less up to date MCS 360 - Introduction to Data Structures Pointers and dynamic memory allocation in C/C++, recursion, stacks, queues, heaps, binary and multiway trees, graphs, hash tables. Sorting and searching algorithms. Prerequisite(s): Grade of C or better in MCS 260 and Grade of C or better in MCS 275. Changes: 1 MCS 275 no longer required, because of Python but still strongly recommended for programming experience. 2 Any Introduction to Computer Science (similar to MCS 260) will do just as well as prerequisite. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 3 / 27

Content of the Course Text Book The design of the course follows Elliot B. Koffman and Paul A.T. Wolfgang: Objects, Abstraction, Data Structures, and Design using C++, John Wiley and Sons, 2006. Reading the textbook is recommended. However, programming is a skill, acquired mainly by practice, practice, and lots of practice... A natural sequel to MCS 360 is MCS 401/CS 401: computer algorithms. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 4 / 27

Organization of the Material along the text book Five parts: 1 introduction to C++ (chapters P, 1, 2, 3) 2 vector, stack, list, queue and deque (chap 4, 5, 6) first midterm on chapters P, 1, 2, 3, 4, 5, 6 3 recursion, trees, maps, hash tables (chap 7, 8, 9) 4 sorting algorithms, balancing trees (chap 10, 11) second midterm on chapters 7, 8, 9, 10, 11 5 after the second midterm, we cover chapter 12 on graphs. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 5 / 27

Introduction to Data Structures 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 6 / 27

Purpose of the Course Goals of MCS 360 are threefold: 1 solve problems with algorithms using right data structures (programs = algorithms + data structures) 2 gain basic working knowledge of C++ 3 application of the Standard Template Library (STL) About C++ + widespread use with good performance no framework language as Java or Python Some important points: emphasis on five computer projects, active participation to the lab sessions. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 7 / 27

Introduction to Data Structures 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 8 / 27

using g++ free available open source The GNU compiler collection includes front ends for C, C++,... g++ = GNU C++, a freely redistributable C++ compiler By default installed on Unix, Linux, Mac OS X. On windows: www.mingw.org or www.cygwin.com Although object-oriented programming supports programming in the large, our programs will typically remain small. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 9 / 27

C++ is a compiled language The compiler translates C++ code in hello_world.cpp into code the computer can execute. At the command prompt $ we type $ g++ -o /tmp/hello_world hello_world.cpp The output of g++ is at /tmp/hello_world. To run the program, type at the command prompt $ $ /tmp/hello_world Hello World! $ Any editor can be used to write.cpp files. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 10 / 27

our first program Our first program writes Hello World! to screen. A C++ program typically consists of 1 preprocessor directives 2 function definitions 3 type declaration of variables 4 control statements Recommended way of working: 1 write basic version of code 2 compile and test, if done then stop 3 extend adding extra statements 4 go to step 2 Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 11 / 27

Introduction to Data Structures 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 12 / 27

code for our first program #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } 1 preprocessor directives start with # 2 including iostream is needed for the insertion operator <<. 3 the program is the definition of a function main 4 one statement sends Hello World! to screen, followed by the end of line (endl) symbol 5 the function returns zero if all went well Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 13 / 27

Comments and Blocks The compiler ignores lines starting with //, e.g.: // L-1 MCS 360 Mon 28 Aug 2017 : hello_world.cpp Multiple lines of documentation are between /* and */. Code between curly braces defines a block. int main() { // omitted code return 0; } Every statement is terminated by semicolon ; indentation is optional, but strongly recommended. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 14 / 27

our first interactive program Our first interactive program 1 prompts the user to enter a name, 2 stores the entered name in a string, 3 writes a personalized greeting. If we save the program in /tmp/hello_there we run it at the prompt $ as $ /tmp/hello_there Who is there? me Hello me! $ Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 15 / 27

Introduction to Data Structures 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 16 / 27

the namespace std Instead of std::cout << "Hello World!" << std::endl; we can write cout << "Hello World!" << endl; if we insert using namespace std; after the preprocessor directives. A namespace is a collection of names or identifiers defined together (like a Python module). The C++ standard library is defined in namespace std. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 17 / 27

code for the entire program #include <iostream> #include <string> using namespace std; int main() { string name; cout << "Who is there? "; // insertion operator cin >> name; // >> is the extraction operator cout << "Hello " << name << "!" << endl; } return 0; Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 18 / 27

Introduction to Data Structures 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 19 / 27

Euclid s Algorithm flowchart Input: a, b; a b > 0. Output: gcd of a and b. r := remainder of a/b r = 0? Yes print b No a := b; b := r Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 20 / 27

computing a GCD $ /tmp/gcd_dowhile1 Welcome to our Greatest Common Divisor give a positive integer x : 468 give a positive integer y : 864 computing the gcd of 468 and 864... x = 468 y = 864 r = 468 x = 864 y = 468 r = 396 x = 468 y = 396 r = 72 x = 396 y = 72 r = 36 x = 72 y = 36 r = 0 gcd(468,864) = 36 $ confirm input before and after computations intermediate output to monitor progress Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 21 / 27

Introduction to Data Structures 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while statements Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 22 / 27

while and do-while Either first test the condition in while before executing the statements: < initialization > while( < condition > ) { < statements > } Or test the condition in while after executing the statements: < initialization > do { < statements > } while( < condition > ); Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 23 / 27

reading numbers #include <iostream> #include <sstream> using namespace std; int main() { int x,y; cout << "Welcome to our Greatest Common Divisor\n"; cout << " give a positive integer x : "; cin >> x; cout << " give a positive integer y : "; cin >> y; cout << "computing the gcd of " << x << " and " << y << "..." << endl; ostringstream s; // write to a string s << "gcd(" << x << "," << y << ") = "; Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 24 / 27

computing the GCD int r; do { r = x % y; // remainder calculation cout << " x = " << x << " y = " << y << " r = " << r << endl; x = y; y = r; } while (r!= 0); // stop test cout << s.str() << x << endl; } return 0; Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 25 / 27

Summary + Exercises In this lecture we covered first three sections of Chapter P. On Tuesday go to lab SEL 2263 make sure your netid is working, there will be a quiz at the end. Exercises: 1 Install g++ on your laptop or home computer. 2 Do g++ -E hello_world.cpp (our first C++ program). What happens? What is this option -E? 3 Extend gcd_dowhile1.cpp with a check that the greatest divisor reported after the loop really divides the two given numbers. 4 Instead of do-while,useasimplewhile statement to encode Euclid s algorithm. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 26 / 27

more exercises 5 One kilogram (kg) is 2.20462 pounds (lb). Write a C++ program which prompts the user for a weight in kilograms. The program computes the weight measured in pounds and writes the corresponding pounds to the standard output. A session with the program could go as follows: Give a weight in kg : 50 The weight 50 in pounds is 110.231. Note: the type for a floating-point number is double. 6 Write a C++ program which prompts the user for a positive integer number. The program writes the input number and prints the bits in the binary decomposition of the number, in reverse order, printing the least significant bit first. A session with the program could go as follows: Give a number : 360 The bits in 360 : 0 0 0 1 0 1 1 0 1. Introduction to Data Structures (MCS 360) Welcome to MCS 360 L-1 28 August 2017 27 / 27