Week 1: Hello World! Muhao Chen

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

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

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.

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Increment and the While. Class 15

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

Scientific Computing

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

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

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

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

This watermark does not appear in the registered version - Slide 1

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

COMP322 - Introduction to C++ Lecture 01 - Introduction

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Ch 1. Algorithms and Basic C++ Programming

Your First C++ Program. September 1, 2010

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

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

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

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Introduction to Programming using C++

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):

Programming with C++ as a Second Language

Chapter 2. C++ Basics

Computer Programming : C++

Chapter 1 Introduction to Computers and Programming

VARIABLES & ASSIGNMENTS

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Review. Relational Operators. The if Statement. CS 151 Review #4

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

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

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Why Is Repetition Needed?

Chapter 2: Overview of C++

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

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

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 2 C++ Fundamentals

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

Fundamentals of Structured Programming

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

Exercise 1.1 Hello world

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

CHAPTER 3 BASIC INSTRUCTION OF C++

Unit 7. 'while' Loops

Security Coding Module - Buffer Overflow Data Gone Wild CS1

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

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

Chapter 2: Basic Elements of C++

Lab 2.1: Fixing a C++ program

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

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

CSI33 Data Structures

Chapter 1 INTRODUCTION

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Fundamentals of Programming CS-110. Lecture 2

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

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

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

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

Review of Important Topics in CS1600. Functions Arrays C-strings

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

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

CSCE 110 PROGRAMMING FUNDAMENTALS

WARM UP LESSONS BARE BASICS

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

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

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

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

C++ Support Classes (Data and Variables)

CS101 PLEDGED SPRING 2001

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

LECTURE 02 INTRODUCTION TO C++

Week 2. Muhao Chen.

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

A First Program - Greeting.cpp

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

CS242 COMPUTER PROGRAMMING

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

C++ basics Getting started with, and Data Types.

Lecture # 6. Repetition. Review. If Else Statements Comparison Operators Boolean Expressions Nested Ifs. Switch Statements.

Assist. Prof. Dr. Caner ÖZCAN

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

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

After an "Hour of Code" now what?

MS Visual Studio.Net 2008 Tutorial

Getting started with C++ (Part 2)

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

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

Chapter 2: Introduction to C++

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Homework #3 CS2255 Fall 2012

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

Transcription:

Week 1: Hello World! Muhao Chen 1

Muhao Chen Teaching Fellow Email address: muhaochen@ucla.edu Office Hours: Thursday 11:30 ~ 2:30 PM BH2432 Personal office BH3551 Homepage (where I post slides): http://yellowstone.cs.ucla.edu/~muhao/ 2

Muhao Chen Ph.D. Candidate in CS, UCLA (w/ Prof. Carlo Zaniolo) 2014-present B.S. in CS, Fudan Univ. 2014 Research interest Neural language models, deep learning for NLP, knowledge bases This is a picture of me unsuccessfully skiing. 3

About CS31 Skills for programming using C++ (without data structures) Basic data type models Basic principles of memory allocation Basic knowledge of object-oriented programming 4

Outline Review How to compile programs Project 1 5

Review What is a program? A sequence of rules and instructions that describe the logic of specific tasks to be processed by the computer. To calculate some formulas, to train some machine learning models. Operating systems, databases, compilers, network system Websites, games 6

Review What is a programming language? A language of medium difficulty to both us and computer. We use it to represent the procedural logic of instructions. Machines map it to machine language and execute the instructions. 7

Review Example In Human language: Print out Hello World! on the screen. In a programming language: cout << Hello World! << endl; In machine language: 0101101111101010101101011010 Compiler. VC++, g++, etc 8

A punch card used in 1960s to program Fortran (the very early programming language which was extremely close to machine languages) 9

Compile a Program #include <iostream> using namespace std; int main() Variable { int a = 1, b = 1; cout<< a + b << endl; } Operand Include the <iostream> library to use cout Use namespace std (standard) Namespace is a collection of name definitions A function name can be given different definitions in two namespaces endl output a new line main() function: where the C++ program begins its logic. Note: it is case-sensitive in C++ 10

Compilers Compiling with a Visual Studio (VC++) Wysiwyg ** (Choose win32/win64 console application when you create the project!) Compiling with g++ g++ -g source_code.cpp -o target./target 11

Errors What is a compile error? Fails to compile. Syntax errors, library errors, link errors, etc. What is a logical error? Compiles successfully. Program may run well / Or may crash (e.g. infinite loop, over-allocated memory, etc). Gives incorrect results / undefined behaviors. 12

Project 1 http://web.cs.ucla.edu/classes/fall17/cs31/ int main() { int numsurveyed; int numapprove; int numdisapprove; cout << "How many people were surveyed? "; cin >> numsurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numapprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numdisapprove; } double pctapprove = 100.0 * numapprove / numsurveyed; double pctdisapprove = 100.0 * numdisapprove / numsurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctapprove << "% say they approve." << endl; cout << pctdisapprove << "% say they disapprove." << endl; if (numapprove > numdisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; 13

forhillary Project 1 One thing we should pay attention to In step 5, find input integer values that cause it to produce incorrect, unusual, or nonsensical results. Is this to cause a compile error or a logical error? Note: The variables numsurveyed, numapprove, and numdisapprove are integer types, so it is not the case to input floating type values like 12.3456. What values should we input to trigger incorrect results? Incorrect results: numsurveyed!= numapprove + numdisapprove (e.g. 3000, 2000, 2000) Unusual or nonsensical results? 14

Data types Type int (Integer) double (double precision float) float (single precision float) SIze 4Bytes (=32bits) or 8Bytes (=64bits) 8Bytes (=64bits) 4Bytes (=32bits) long int, unsigned int, char, boolean 15

int (suppose it s on a 32bits system) int: 4Bytes. Range: -2147483648~2147483647 (-2 31 ~2 31-1) 5 = 0 000 0000 0000 0000 0000 0000 0000 0101 Signed bit (S) Value bits (V) -5 = 1 111 1111 1111 1111 1111 1111 1111 1011 If S = 0: Value = V If S = 1. Value = V - 2 31 int a=2147483647; // 0 1111 1111, i.e. 2 31-1 a += 1; /// How much is a now? 16

Bit overflow A bit overflow occurs when an arithmetic operation attempts to create a numeric value that is too large to be represented within the available storage space. So just assign 999999999999999999999999999999999999 also gives logic error. 17

Project 1 The zip file you submit must follow the instructions exactly. (Pay attention to how to name each cpp file and your zip file!) Be careful about compile error and logical error. Projects submitted after the due time will receive reduced or no credit. (Something about David s projects) 18

Next week Data types and variables Operators Conditions Loops I/O 19

Thank you! 20