C++ Support Classes (Data and Variables)

Similar documents
Scientific Computing

A SHORT COURSE ON C++

Chapter 1 INTRODUCTION

Chapter 2: Basic Elements of 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++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

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

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

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

Objectives. In this chapter, you will:

Introduction to Programming using C++

Understanding main() function Input/Output Streams

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

CSI33 Data Structures

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Computer Programming : C++

Program Organization and Comments

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

These are notes for the third lecture; if statements and loops.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Getting started with C++ (Part 2)

2 nd Week Lecture Notes

Chapter 2 Basic Elements of C++

BITG 1233: Introduction to C++

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Your First C++ Program. September 1, 2010

Lab: Supplying Inputs to Programs

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

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

CSCE 121 ENGR 112 List of Topics for Exam 1

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

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

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

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

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

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

Chapter 2. Outline. Simple C++ Programs

CSI33 Data Structures

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

Your first C++ program

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

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

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

Chapter 2: Introduction to C++

Cpt S 122 Data Structures. Introduction to C++ Part II

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

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

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

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Chapter 1 Introduction to Computers and C++ Programming

Add Subtract Multiply Divide

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

1007 Imperative Programming Part II

Computing and Statistical Data Analysis Lecture 3

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

A First Program - Greeting.cpp

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

WARM UP LESSONS BARE BASICS

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

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

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Programming Language. Functions. Eng. Anis Nazer First Semester

4. Structure of a C++ program

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Technical Questions. Q 1) What are the key features in C programming language?

LECTURE 02 INTRODUCTION TO C++

CS50 Supersection (for those less comfortable)

Fast Introduction to Object Oriented Programming and C++

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

C++ for Python Programmers

Computer Science II Lecture 1 Introduction and Background

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CSI33 Data Structures

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

Chapter 1 - What s in a program?

First C or C++ Lab Paycheck-V1.0 Using Microsoft Visual Studio

Maciej Sobieraj. Lecture 1

UNIT- 3 Introduction to C++

Engineering Problem Solving with C++, Etter/Ingber

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Programming with C++ as a Second Language

Algorithms and Programming I. Lecture#12 Spring 2015

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Lab Instructor : Jean Lai

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

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.

Introduction to C++ Dr Alex Martin Room 6.10

C++ Programming Lecture 1 Software Engineering Group

Pace University. Fundamental Concepts of CS121 1

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

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

How to approach a computational problem

Operators. Java operators are classified into three categories:

Transcription:

C++ Support Classes (Data and Variables) School of Mathematics 2018

Today s lecture Topics: Computers and Programs; Syntax and Structure of a Program; Data and Variables; Aims: Understand the idea of programming a computer; Write a simple program to input and output data.

An Idealized Computer Memory Input CPU Output CPU - Central Processing Unit CPU and memory work together File Store

An Idealized Computer Memory Input CPU Output File Store CPU - Central Processing Unit CPU and memory work together Input may be from keyboard, mouse or a file Output may be to screen or a file

Programming What is a program?

Programming What is a program? A program is sequence of instructions to enable a computer to complete a task.

Programming What is a program? A program is sequence of instructions to enable a computer to complete a task. Computers can remember programs Originally programs had to be written in machine code - low-level

Programming What is a program? A program is sequence of instructions to enable a computer to complete a task. Computers can remember programs Originally programs had to be written in machine code - low-level Now we write programs in code

Programming What is a program? A program is sequence of instructions to enable a computer to complete a task. Computers can remember programs Originally programs had to be written in machine code - low-level Now we write programs in code - and let the computer write the machine code for us! C/C++ are best described as mid-level languages Higher-level languages include java, Fortran and others. Scripting languages such as python compile on the fly, giving instant results

Writing C++ Raw C code make absolutely no inferences about what you want. C++ code is constantly being updated to become more intelligent. Many scripting languages make assumptions about data types this makes it easier to code...

Writing C++ Raw C code make absolutely no inferences about what you want. C++ code is constantly being updated to become more intelligent. Many scripting languages make assumptions about data types this makes it easier to code... but easier to make mistakes (of the kind you can t spot).

Writing C++ Raw C code make absolutely no inferences about what you want. C++ code is constantly being updated to become more intelligent. Many scripting languages make assumptions about data types this makes it easier to code... but easier to make mistakes (of the kind you can t spot). C++ is the fastest and most powerful language because it lets the user take control

Writing C++ Raw C code make absolutely no inferences about what you want. C++ code is constantly being updated to become more intelligent. Many scripting languages make assumptions about data types this makes it easier to code... but easier to make mistakes (of the kind you can t spot). C++ is the fastest and most powerful language because it lets the user take control Uncle Ben - Spiderman 2002 Remember, with great power, comes great responsibility.

Writing C++ The key elements of C/C++ syntax are: Semicolon used to mark end of statements Case is important Totally free form, lines and names can be as long as you like! Comments take the form /* C style comment */ or // C++ style comment Code blocks are surrounded by braces {}

An example Program Libraries/Methods Program Declare Include libraries and methods Declare data types and variable names Input values for variables Input Calculate Output

An example Program Libraries/Methods Program Declare Input Calculate Include libraries and methods Declare data types and variable names Input values for variables Required calculations are carried out in sequence Output to the screen or a file Output

An example program First we see a library at the top, allowing for input/output

An example program First we see a library at the top, allowing for input/output The top area should contain global options - but not calculations.

An example program First we see a library at the top, allowing for input/output The top area should contain global options - but not calculations. The calculations are carried out underneath. Note that // is used for comments (they appear green).

An example program First we see a library at the top, allowing for input/output The top area should contain global options - but not calculations. The main program is called main. There can be only ONE of these functions in your code.

An example program Curly brackets are used to indicate the start and finish of the function block.

An example program Curly brackets are used to indicate the start and finish of the function block.

An example program Curly brackets are used to indicate the start and finish of the function block. You first set up your code. Each line is ended by a semi colon.

An example program Curly brackets are used to indicate the start and finish of the function block. You first set up your code. Each line is ended by a semi colon. Then we carry out some calculations - later on this may be 10 s, 100 s or even 1000 s of lines long.

An example program Curly brackets are used to indicate the start and finish of the function block. You first set up your code. Each line is ended by a semi colon. Then we carry out some calculations - later on this may be 10 s, 100 s or even 1000 s of lines long. The result from your calculation is output on the screen (or perhaps into a file).

IDEs and simple text files A C++ code is saved as an ASCII text file and can written in any editor. However, in this course we will encourage the use of an Integrated Development Environment (IDE). The two recommended peices of software are: Visual Studio (Windows only) Netbeans (Windows/Linux/Mac) For our purposes in this course, it will not matter on which platform you write the code. Visual Studio has limitations at the University and at home, but is probably the best choice.

Getting started on Visual Studio First open up the start menu, type in visual and select Visual Studio

Getting started on Visual Studio First open up the start menu, type in visual and select Visual Studio Select Not now maybe later when it asks you to sign in.

Getting started on Visual Studio First open up the start menu, type in visual and select Visual Studio Select Not now maybe later when it asks you to sign in. Select FILE > New > Project from the top menu.

Getting started on Visual Studio Now select Visual C++ on the left hand bar, and Empty Project in the centre. You can change the project name if you like.

Getting started on Visual Studio Now select Visual C++ on the left hand bar, and Empty Project in the centre. You can change the project name if you like. Right click on Source Files on right hand bar, select Add > New Item...

Getting started on Visual Studio Now select Visual C++ on the left hand bar, and Empty Project in the centre. You can change the project name if you like. Right click on Source Files on right hand bar, select Add > New Item... Select C++ File (.cpp), you can change the name of the file if you like.

Getting started on Visual Studio Now copy in the text for the Hello World program (see image). Getting lost? Click here to download the code for this demonstration.

Getting started on Visual Studio Now copy in the text for the Hello World program (see image). You are now ready to run the program select DEBUG > Start Without Debugging from the top menu. Getting lost? Dr P. V. Click Johnson here to download the code for this demonstration.

Getting started on Visual Studio Now copy in the text for the Hello World program (see image). Whoops! That disappears before I can see it! We need to edit some more settings... Getting lost? Click here to download the code for this demonstration.

Getting started on Visual Studio Now copy in the text for the Hello World program (see image). Whoops! That disappears before I can see it! We need to edit some more settings... Select PROJECT > Project Properties from the top menu. Getting lost? Click here to download the code for this demonstration.

Getting started on Visual Studio Now on the left hand bar select Configuration Properties > Linker > System. You should see a SubSystem setting appear.

Getting started on Visual Studio Now on the left hand bar select Configuration Properties > Linker > System. You should see a SubSystem setting appear. Press the little triangle button on the right hand side to reveal options, choose Console(/SUBSYSTEM:CONSOLE).

Getting started on Visual Studio Now on the left hand bar select Configuration Properties > Linker > System. You should see a SubSystem setting appear. Press the little triangle button on the right hand side to reveal options, choose Console(/SUBSYSTEM:CONSOLE). Run DEBUG > Start Without Debugging from the top menu and you should see the output!

Saving your work Every time you compile and run the code your work is saved. To transfer the work between different computers, you must set up a new project and copy/paste the code DO NOT TRY TO TRANSFER THE WHOLE DIRECTORY. You CANNOT have more than one program in a project. If you want to do something different, you will NEED to open up a new project! You will need to go through the steps outlined above EVERY time you start a new project.

So, where is it saved? Open up the Visual Studio 2013 folder in My Documents. Now open Projects.

So, where is it saved? Open up the Visual Studio 2013 folder in My Documents. Now open Projects. You should see a folder with the name of your project open it.

So, where is it saved? Open up the Visual Studio 2013 folder in My Documents. Now open Projects. You should see a folder with the name of your project open it. Here you find some folders and a *.sln file. This file does not contain your code!

So, where is it saved? Open up the Visual Studio 2013 folder in My Documents. Now open Projects. You should see a folder with the name of your project open it. The Debug folder contains the executable file and any file outputs default to this location.

Where is it saved? If you open it with notepad you will see it contains some global options about your system.

Where is it saved? If you open it with notepad you will see it contains some global options about your system. Open the file folder with the name of your project and you should see you C++ file.

Where is it saved? If you open it with notepad you will see it contains some global options about your system. Open the file folder with the name of your project and you should see you C++ file. The VC++ Project File file contains information about which C++ files are in your project, as well as external libraries and options.

Libraries Unlike higher level programming languages, there are almost no intrinsic functions in C++ This includes the ability to print to screen. We can include standard libraries for: Input/Output Advanced Storage Strings Mathematical functions The syntax for including libraries is: #include <library name > Include statements must appear before any other statements.

What is in a name? Each variable requires a unique name (in the same scope) In large projects this becomes very difficult A namespace is like adding a surname to a variable The prefix std means functions from the standard library We can use the statement: using namespace std; to assume an undeclared function, data type or variable is in the standard library.

Simple Input and Output We use stream variables to access the screen, keyboard and files. They allow us to associate a name with a physical output. We need to include stream libraries at the top of the program #include<iostream> using namespace std main(){ int i; cout << " Enter a number. " << endl; cin >> i; //read in a number cout << " Your number is " << i << endl; }

Simple Input and Output cout is the standard screen variable, and cin the standard keyboard variable To pass data to and from the stream we use the << and >> operators.

Simple Input and Output cout is the standard screen variable, and cin the standard keyboard variable To pass data to and from the stream we use the << and >> operators. << data is passed right to left, in the example the string is passed to cout >> data is passed left to right, in the example the integer is passed from cin to i

Simple Input and Output cout is the standard screen variable, and cin the standard keyboard variable To pass data to and from the stream we use the << and >> operators. << data is passed right to left, in the example the string is passed to cout >> data is passed left to right, in the example the integer is passed from cin to i Multiple bits of data can be passed to the stream by stringing them together in the same command. Use endl to finish a line.

Simple Input and Output Tasks Try out the following code in your main program, can you predict what will happen? cout << " This "; cout << "is a "; cout << "sentence " << endl; cout << " This "<< endl << "is a "<< endl<< "sentence " << endl; Output strings are enclosed in quotes can you find out how to print a quote? Special characters are preceded by a backslash find out what \r, \n, \b and \\ do to the output? What is the difference between \n and endl?

Finding BUGs Computers are not humans! Computers can only follow the instructions they are given. If they are not doing what you want it is because you haven t told them explicitly what to do!

Finding BUGs Computers are not humans! Computers can only follow the instructions they are given. If they are not doing what you want it is because you haven t told them explicitly what to do! The best way to avoid bugs is to start with working solution and make small changes. I hardly ever make mistakes because of the way I approach coding.

Finding BUGs Computers are not humans! Computers can only follow the instructions they are given. If they are not doing what you want it is because you haven t told them explicitly what to do! The best way to avoid bugs is to start with working solution and make small changes. I hardly ever make mistakes because of the way I approach coding. IDEs can help us spot mistakes as they constantly check your code, lets show an example of this...

Finding BUGs Open up a new empty project, with an empty C++ file.

Finding BUGs Open up a new empty project, with an empty C++ file. Type in an include statement, but misspell some of the words. You will notice they do not look colourful and have a red line underneath.

Finding BUGs Open up a new empty project, with an empty C++ file. Type in an include statement, but misspell some of the words. You will notice they do not look colourful and have a red line underneath. Try to build the project - click yes. It will say there were build errors, click no this time.

Finding BUGs Open up a new empty project, with an empty C++ file. Type in an include statement, but misspell some of the words. You will notice they do not look colourful and have a red line underneath. Try to build the project - click yes. It will say there were build errors, click no this time.

Finding BUGs We need to see what the errors are. Make sure you can view the error list. Click VIEW > Error List from the top bar you should see an error list box at the bottom.

Finding BUGs We need to see what the errors are. Make sure you can view the error list. Click VIEW > Error List from the top bar you should see an error list box at the bottom.

Finding BUGs We need to see what the errors are. Make sure you can view the error list. Click VIEW > Error List from the top bar you should see an error list box at the bottom. Correctly spell include now, iostrem appears slightly darker because it can t be found. Correct this as well.

Finding BUGs We need to see what the errors are. Make sure you can view the error list. Click VIEW > Error List from the top bar you should see an error list box at the bottom. Correctly spell include now, iostrem appears slightly darker because it can t be found. Correct this as well.

Finding BUGs We need to see what the errors are. Make sure you can view the error list. Click VIEW > Error List from the top bar you should see an error list box at the bottom. Correctly spell include now, iostrem appears slightly darker because it can t be found. Correct this as well. Try to build the project - there are unresolved externals errors you haven t got a main function.

Finding BUGs Now add in the main function, it should compile and run without errors.

Finding BUGs Now add in the main function, it should compile and run without errors. Add in the following code: cout << "Hello World" You will see red underlines and errors if you try to run it.

Finding BUGs Now add in the main function, it should compile and run without errors. Add in the following code: cout << "Hello World" You will see red underlines and errors if you try to run it.

Finding BUGs Now add in the main function, it should compile and run without errors. Add in the following code: cout << "Hello World" You will see red underlines and errors if you try to run it. Fix the undefined with the correct namespace at the top. There is still one more error to fix. Add in the semi colon.

Finding BUGs Now add in the main function, it should compile and run without errors. Add in the following code: cout << "Hello World" You will see red underlines and errors if you try to run it. Fix the undefined with the correct namespace at the top. There is still one more error to fix. Add in the semi colon.

Finding BUGs Now add in the main function, it should compile and run without errors. Add in the following code: cout << "Hello World" You will see red underlines and errors if you try to run it. Fix the undefined with the correct namespace at the top. There is still one more error to fix. Add in the semi colon.

Finding BUGs Now add in the main function, it should compile and run without errors. Add in the following code: cout << "Hello World" You will see red underlines and errors if you try to run it. Fix the undefined with the correct namespace at the top. There is still one more error to fix. Add in the semi colon.

Finding BUGs Tasks Open a new project and copy the following code into your project: Click here to download the code for this example. Can you find all the errors in this code and make it work?

Standard Data Types There are six basic data types in C++: char a character (short/long) int integers with different sizes float single precision real number (long) double double (or higher) precision number bool true or false binary number void this is used when a function doesn t return a value The precision of numbers will effect the maximum and minimum values they can take and also how big errors are.

Assigning and Outputting Variables First open up a new project, and follow the initial steps from earlier on Getting lost? Click here to download the code for this demonstration.

Assigning and Outputting Variables First open up a new project, and follow the initial steps from earlier on Don t forget to change the SubSystem to console. Getting lost? Click here to download the code for this demonstration.

Assigning and Outputting Variables First open up a new project, and follow the initial steps from earlier on Don t forget to change the SubSystem to console. Create a simple program that declares an integer variable, and outputs to screen. Getting lost? Click here to download the code for this demonstration.

Assigning and Outputting Variables Now add in a code block using curly brackets and redeclare a variable with the same name Getting lost? Click here to download the code for this demonstration.

Assigning and Outputting Variables Now add in a code block using curly brackets and redeclare a variable with the same name What happens to the value of the variable inside and outside the block? What happens if you remove the curly brackets? Getting lost? Click here to download the code for this demonstration.

Assigning and Outputting Variables Now add in a code block using curly brackets and redeclare a variable with the same name What happens to the value of the variable inside and outside the block? What happens if you remove the curly brackets? Tasks Try declaring each of the six different types of variable and output them to screen. What happens if you print out the variables without assigning them a value? Getting lost? Click here to download the code for this demonstration.

Using Variables Each line of code is called a statement. We can assign a variable a value using = We can do calculations on the right hand side of an = data 1 = 10. + 21.5; The data types on both sides of = must be compatible

Using Variables Each line of code is called a statement. We can assign a variable a value using = We can do calculations on the right hand side of an = data 1 = 10. + 21.5; The data types on both sides of = must be compatible We can add/subtract, multiply/divide most standard data types double a,b,c; b = 5. ; c = 4.1; a = 10. * b + c;

Other Operators We call +-*/ operators Other operators are: a%b :: returns a modulus b a++ :: increment a by 1 a-- :: decrement a by 1 a+=b :: set a equal to a plus b a*=b :: set a equal to a times b

Example: Dividing with integers First open up a new project, create a cpp file with an empty main function. Getting lost? Click here to download the code for this demonstration.

Example: Dividing with integers First open up a new project, create a cpp file with an empty main function. Now declare two integers, ask the user to input the values and read them in. Then output a calculation of one over the other. Getting lost? Click here to download the code for this demonstration.

Example: Dividing with integers First open up a new project, create a cpp file with an empty main function. Now declare two integers, ask the user to input the values and read them in. Then output a calculation of one over the other. The example shows 20/4=5 what happens if you do 1/2=?? Getting lost? Click here to download the code for this demonstration.

Doing Calculations Tasks Try doing some simple calculations with integers and doubles. Work out a x + bx where ax 2 + bx + c a + b x c 4 a = 1.17745, b = 3, c = 1004.1, and x = 24.539. Use brackets to order the calculations. Try declaring the variables a, b, c and x as int, float or double what difference does it make to the solution?

Doing Calculations Tasks Now include the cmath library in your program. Calculate:- where sin(ax) x 5 (a + bx) i i=1 sin(e x a ) a = 1.17745, b = 3, c = 1004.1, and x = 24.539. The functions you will need are sqrt, sin, cos, exp and, pow. Look them up if required. What happens to the result of the top expression if x = 0?.

Todays Lecture Topics: Computers and Programs; Syntax of C++; Data and Variables; Input and Output. Aims: Understand the idea of programming a computer; Write a simple program to input and output data.