CMPT 201.

Slides:



Advertisements
Similar presentations
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Advertisements

Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
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.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Basic Elements of C++ Chapter 2.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to C++ Programming
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Instructor: Tina Tian. About me Office: RLC 203A Office Hours: Wednesday 1:30 - 4:30 PM or .
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Chapter 02 (Part III) Introduction to C++ Programming.
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Objective: Students will be able to: Declare and use variables Input integers.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Bill Tucker Austin Community College COSC 1315
C++ Basics Lecture 2.
Basic concepts of C++ Presented by Prof. Satyajit De
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
C++ Programming: Presentation 1
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Midterm Review.
CMPT 238 Data Structures C++ Review
Completing the Problem-Solving Process
Computing Fundamentals
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Introduction to C++.
Basic Elements of C++ Chapter 2.
Chapter 2 – Getting Started
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Programming 1 (CS112) Dr. Mohamed Mostafa Zayed 1.
Course websites CS201 page link at my website: Lecture slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Unit 3: Variables in Java
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CMPT 201

Include Directives #include <iostream> using namespace std; Preprocessor directive iostream is a library containing definitions of the input and output function Linker Appears at the start of the program, begin with # using namespace std; Tells the compiler to use names in iostream in a “standard” way

int main() { //beginning of the main function .... //statements return 0; } //end of the program

The output function cout c(C++ language)out(output) c(C++ language)in(input) cout<<whatever data you want to output; cout<<“Welcome to CMPT201!”; << represents the flow of the data.

Okay, let’s write a hello world program in C++ C++ is fun!

Note No extra space between < and iostream, or between the end of the file name and > #include<iostream > #include< iostream> #include<iostream> Old C++ compilers: iostream.h Statements end with a semi-colon.

Program Layout Compiler accepts almost any pattern of line breaks and indentation Programmers format programs so they are easy to read Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves Indent statements Use only one statement per line

.cpp file .exe file

More about cout <<: insertion operator cout<<“Hello World”; is equivalent to cout<<“Hello”<<“ World”; cout<<“Hello ”; cout<<“World”;

About cout Start a new line Escape sequence \n (need to go inside of the quotes) endl Escape sequence \n new line \t tab \” ” \\ \

Comments // This is a comment. /* This is a comment. This is another comment. */

Exercise Write a simple program to print to the screen the following. Be sure to add your name in the comment. Hello! I say “C++ is fun!” Good-bye!

Variables a place to hold a number or data of other types Variable names Start with a letter or the underscore Remaining can be letters, numbers or underscore x x1 3X _abc %change data-1 PROG.CPP Big_Bonus C++ is case-sensitive rate RATE Rate

Variable Names Variables are often spelled with all lowercase letters. Avoid keywords/reserved words int, double cin, cout Choose meaningful names! x, y, z distance, speed, time

Declare a Variable Syntax Example Where? type name; type name1, name2, ... ; Example int number; double weight, height; Where? before the variable is used at the start of the main function (preferred)

Assign Value to a Variable Syntax variable = value(or expression); Example a = 2; b = a; c = a + b; count = count + 1 is totally wrong in math, but in C++?

Combine Declaration and Assignment int number = 3; double rate = 0.07, balance = 0;

Example here cout<<number; cout<<“number”;

Input Function cin cout: output to screen cin: input from keyboard cout<<whatever you want to output; cin>>variable_name; cin>>variable1>>variable2; Or cin>>variable1; cin>>variable2;

Example here Give the user a prompt before the input.

Exercise Write a program that reads in two integers and then outputs both their sum and their product.

Debug Bug: a mistake in a program Debugging: Eliminating the mistake

Errors Syntax errors Run-time errors Logic errors The compiler may be wrong about the location/error! Start from the first error Warning message Run-time errors Logic errors hardest to diagnose

Modify the previous program so that it also outputs their quotient Modify the previous program so that it also outputs their quotient. Recompile the program.

Test It is important to test your program on several data sets!

Homework 1 Due next Friday before class Hard copy Source code Screen shot of test runs