Introduction to C/C++

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

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

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

6.096 Introduction to C++ January (IAP) 2009

A First Program - Greeting.cpp

Introduction to Programming

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

Objectives. In this chapter, you will:

2 nd Week Lecture Notes

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

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

Scientific Computing

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

Computer Programming : C++

EC 413 Computer Organization

Introduction to Programming using C++

Summary of basic C++-commands

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

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

Getting started with C++ (Part 2)

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

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

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

Your first C++ program

Input And Output of C++

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

Creating a C++ Program

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

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

C++ Support Classes (Data and Variables)

UEE1302 (1102) F10: Introduction to Computers and Programming

LECTURE 02 INTRODUCTION TO C++

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

C++ Programming Basics

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

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

Understanding main() function Input/Output Streams

Chapter 1 INTRODUCTION

Chapter 1 Introduction to Computers and C++ Programming

CSCI 1061U Programming Workshop 2. C++ Basics

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

UNIT- 3 Introduction to C++

C++ for Python Programmers

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

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

1.1 Introduction to C Language. Department of CSE

Your First C++ Program. September 1, 2010

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

IDE: Integrated Development Environment

CSI33 Data Structures

Programming with C++ as a Second Language

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

From Pseudcode Algorithms directly to C++ programs

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

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

Chapter 2 Basic Elements of C++

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Chapter 2: Introduction to C++

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

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

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

MA400: Financial Mathematics

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

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

The C++ Language. Arizona State University 1

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

Introduction to C ++

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Introduction to Programming

CHAPTER 3 BASIC INSTRUCTION OF C++

COMP322 - Introduction to C++ Lecture 01 - Introduction

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

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

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

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

Computer Science II Lecture 1 Introduction and Background

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

BTE2313. Chapter 2: Introduction to C++ Programming

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

Lecture 3. More About C

Typescript on LLVM Language Reference Manual

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

Professor Peter Cheung EEE, Imperial College

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

CS242 COMPUTER PROGRAMMING

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

Fundamentals of Programming CS-110. Lecture 2

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

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

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

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

Transcription:

Introduction to C/C++ Joseph Spring School of Computer Science Operating Systems and Computer Networks Lecture Introduction to C/C++ 1

Areas for Discussioni Introduction Background Books C++ after Java Linux, xterm and emacs Process Lecture 1 Introduction to C/C++ 2

Areas for Discussion Basic Components and Layout Simplest program cout << and cin << Layout Preprocessor Instructions and functions Program and Function Structure Data types Declarations Assignments Statements t t Further Preprocessor Instructions, if... else and count Characters Lecture 1 Introduction to C/C++ 3

Introduction Lecture 1 Introduction to C/C++ 4

Introduction - Background Last week we looked at using Linux and emacs to create C++ programs. We were interested at that t stage to be able to achieve the following: Getting into Linux, opening the xterm window to execute our programs in Opening up the text editor emacs to enter our source code Lecture 1 Introduction to C/C++ 5

Introduction - Background This means we should now be in a position o to: Create directories Move from one directory to another List the contents of a directory Open files for editing Copy and rename files Compile, link and run our programs using statements of the form: g++ datarate1.cpp./a.out Lecture 1 Introduction to C/C++ 6

Introduction - Background From an editing perspective we should now be in a position to: Move left and right by one character Move up and down by one line Move to the beginning or end of a file Move to the beginning gor end of a line Insert or delete lines and or characters Move lines or characters Save files with the same or different names Lecture 1 Introduction to C/C++ 7

Introduction - Process Programs written in a medium level language ge such as C++ have to be transformed into machine language so that the computer can process the instructions. C++ compilers use a two stage process to achieve this: The compiler translates the program into machine language instructions. i These are stored to memory as an object file The second stage, referred to as linking combines the object file with other object files to produce an executable file With g++ both of these stages are achieved as a single step Once the file is in executable form it can be run and checked to see if it performs as intended Lecture 1 Introduction to C/C++ 8

Basic Components and Layout Lecture 1 Introduction to C/C++ 9

Basic Components and Layout Example 1 The Simplest Possible Program #include<iostream> using namespace std; main( ) { 1. The program begin with preprocessor instructons. These take the form #include<iostream> using namespace std; Lecture 1 Introduction to C/C++ 10

Preprocessor Instructions Preprocessor instructions are: Grouped together at beginning of program Begin with a # character Control the use of libraries of predefined functions and include definitions of symbols used within the program Example 1. #include<iostream> iost eam this file econtains declarations about the standard Input and Output library Lecture 1 Introduction to C/C++ 11

Basic Components and Layout #include<iostream> causes the file iostream to be included in the source file before being compiled by the compiler It is used as a convenient way for including lots of standard d declarations and definitions iti iostream contains declarations about the standard input/output library such as: cout, cin,... Lecture 1 Introduction to C/C++ 12

Preprocessor Instructions 2. using namespace std; the elements of the standard C++ library are declared within the namespace with the name std. to access its functionality we declare with this expression that we will be using these entities. The line using namespace std; is frequently employed in C++ programs using the standard library Lecture 1 Introduction to C/C++ 13

Basic Components and Layout main( ) { 1. The main declaration is an unusual function 1. It is called by operating system not from C++ 2. It can return an int value to the operating system such that 1. 0 denotes normal termination 2. Other values denote abnormal termination 2. Arguments may be included or ignored as required Lecture 1 Introduction to C/C++ 14

Basic Components and Layout Example 1 The Simplest Possible Program #include<iostream> using namespace std; main( ) { 1. All C/C++ programs must have reference to the function main 2. When C/C++ programs run they look for and adhere to the definition of main Lecture 1 Introduction to C/C++ 15

Basic Components and Layout Example 2 The Next Simplest Possible Program #include<iostream> using namespace std; int main( ) { cout << Hello World << \n ; Lecture 1 Introduction to C/C++ 16

Basic Components and Layout int main( ) { cout << Hello World << \n ; 1. The cout << call is used to print whatever follows to the terminal screen 2. The above cout << call prints Hello World folllowed by a new line 3. \n newline, \t is a tab, \b backspace, \\ a backslash Lecture 1 Introduction to C/C++ 17

Program and Function Structure Lecture 1 Introduction to C/C++ 18

Program and Function Structure 1 /* comments these can occur throughout */ Preprocessor Instructions /* main program */ main( ) { statement 1; statement 2;... statement n; /* function definitions */ function1( () {... functionm( ) {... Lecture 1 Introduction to C/C++ 19

Program and Function Structure 1 General form: /* comments these can occur throughout */ Preprocessor Instructions /* main program */ main( () { statement 1; statement 2;... statement n; Lecture 1 Introduction to C/C++ 20

Program and Function Structure 1 main() {... /* function definitions */ function1( ) { statement 1;... statement n; functionm( () {... Lecture 1 Introduction to C/C++ 21

Program and Function Structure 1 /* comments these can occur throughout */ Preprocessor Instructions /* main program */ main( ) { statement 1; statement 2;... statement n; /* function definitions */ function1( () {... functionm( ) {... Lecture 1 Introduction to C/C++ 22

Program Structure 1. Comments which can occur throughout 2. Preprocessor instructions 3. The main program 4. Function definitions A C/C++ program must contain: 1. The function main( ) 2. Program statements contained between the curly brackets { and Lecture 1 Introduction to C/C++ 23

Data Types, Declarations, Assignments and Statements Lecture 1 Introduction to C/C++ 24

Programs pot.cpp #include<iostream.h> using namespace std; int main( ) { int pot = 1; while(pot < 10000) { cout << pot << \n ; pot = pot * 2; Lecture 1 Introduction to C/C++ 25

Data Types The integer family int machine dependent, but normally 32 bits long 32 bit short 16 bit char 8 bit (Note, char is a very short int) The unsigned family these are obtained by preceding the int family with unsigned They denote positive values Lecture 1 Introduction to C/C++ 26

Data Types The floating point type double is usually 64 bits float is usually 32 bit The boolean or logical type bool yields true or false Example while(pot < 10000) The expression pot < 10000 is either true or fl false; boolean. Lecture 1 Introduction to C/C++ 27

Declarations Declarations involve a type (such as int) followed by variable(s) Example 1: int pot = 1; Example 2: double rate=0.0, time=0.0, mb=0.0; Note the use of commas in the second example Lecture 1 Introduction to C/C++ 28

Statements The while statement is one that you will have met in the tutorial. It s form is: while (expression) statement Theexpression is evaluated if true then the statement t t that t follows is executed If false then the while statement stops and statements following the while statement are executed In the Powers of Two program pot.cpp the expression is pot < 10000 Lecture 1 Introduction to C/C++ 29

Assignments C++ just uses = for the assignment operator In the pot.cpp program we meet: pot = pot * 2 In this line two times the pot variable is assigned to the pot variable In the datarate1.cpp program the mb variable is assigned the product of the rate and time variables Note the use of the assignment operator in fibon.cpp Lecture 1 Introduction to C/C++ 30

Programs datarate1.cpp #include<iostream.h> using namespace std; int main( ) { double rate =0.0, time=0.0, mb=0.0; while(true) { cout << input rate in Mb/sec: ; cin >> rate; cout << input time in seconds (1 milli=0.001): ; cin >> time; mb = rate * time; cout << data transferred = << mb << Mb\n ; cout << data transferred = << mb/8 << MB\n ; Lecture 1 Introduction to C/C++ 31

Programs datarate2.cpp #include<iostream.h> using namespace std; int main( ) { double rate =0.0, 0 time=0.0, 0 mb=0.0; 0; while(true) { cout << input rate in Mb/sec: ; cin >> rate; cout << input amount of data in MB (megabytes): ; cin >> mb; time = (mb*8) / rate; cout << time for transfer = << time << secs\n ; Lecture 1 Introduction to C/C++ 32

Programs fibon.cpp #include<iostream.h> using namespace std; int main( ) { int last, last_ but_ one, new_ last; last_but_one = 0; last = 1; while(last< 10000) { cout << last << ; new_last =last+last last_but_one; last_but_one = last; last = new_last; cout << \n ; Lecture 1 Introduction to C/C++ 33

Further Preprocessor Instructions, The if... else... Conditional Statement, and count Lecture 1 Introduction to C/C++ 34

Programs largest.cpp #include <iostream> #include <limits.h> using namespace std; int main( (){ int num, count=0, max_so_far=int_min; while ( cin >> num ) { if ( num > max_so_far ) max_so_far = num; count = count + 1; if ( count > 0 ) cout << "largest = " << max_so_far _ << "\n"; else cout << "no numbers input\n"; Lecture 1 Introduction to C/C++ 35

Further Preprocessor Instructions The program largest.cpp introduces a new preprocessor instruction: #include <limits.h> limits.h contains definitions of a range of integer constants, for example, the largest and smallest int, unsigned int, float, double,... The program largest.cpp uses the symbolic constant INT_MIN the smallest int value defined in limits.h The definitions are dependent upon the implementation employed limits.hit is from Chence the use of.h in the name Lecture 1 Introduction to C/C++ 36

The if... else... Conditional Statement The program largest.cpp introduces the following conditional statement: if ( count > 0 ) cout << "largest = " << max_so_far << "\n"; else cout << "no numbers input\n"; The general form for conditional statements is: if (expression) statement 1 else statement 2 Here the expression is evaluated. If it is true the statement 1 is executed. If it is false then statement 2 is executed Lecture 1 Introduction to C/C++ 37

The count variable The program largest.cpp also introduces the count variable count=0;... count = count t+ 1; if ( count > 0 ) cout << "largest = " << max_so_far << "\n"; " else cout << "no numbers input\n ; The count variable is useful in the above context and also when printing the first n values in a sequence For example, the first 18 Fibonacci numbers Lecture 1 Introduction to C/C++ 38

Characters Lecture 1 Introduction to C/C++ 39

Programs copy1.cppcpp #include <iostream> using namespace std; int main() { char c; while ( cin.get(c) ) cout.put(c); Lecture 1 Introduction to C/C++ 40

copy1.cppcpp This program copies the standard input to the standard output The statement cin.get(c) reads in characters from standard input into the variable c The statement t t while ( cin.get(c) ())) cout.put(c); (); returns the value true if a character is stored in the variable c and executes cout.put(c) writing the character to the standard output Lecture 1 Introduction to C/C++ 41

copy2.cppcpp This program also copies the standard input to the standard output, however it omits spaces, newlines and tabs for example Output from the program copy2.cpp is given on the Running copy2.cpp cpp slide Lecture 1 Introduction to C/C++ 42

Programs copy2.cppcpp #include <iostream> using namespace std; int main() { char c; while ( cin >> c ) cout << c; Lecture 1 Introduction to C/C++ 43

Running copy2.cppcpp rabbit(273)$ g++ copy2.cpp pp rabbit(274)$./a.out < copy2.cpp #include<iostream>intmain(){charc;while(cin>>c)cout<<c; Lecture 1 Introduction to C/C++ 44

Programs wrap.cpp #include <iostream> const int LINELENGTH = 30; using namespace std; int main() { int pos = 0; char c; while ( cin.get(c) ) { if (c == \n ) pos = 0; else pos = pos + 1; if (pos > LINELENGTH) { cout << \n ; pos = 1; cout << c; Lecture 1 Introduction to C/C++ 45

wrap.cpp wrapping text and equality The program wrap.cpp ensures reads standard input and prints the text tto the standard doutput t(terminal) ensuring that each line of output is no longer than 30 characters If the linelength is greater than 30 then the newline character \n is inserted and the text continues on the next line Note the test for equality cin == \n uses the double equal sign. Recall a single equals sign is used for assignment or within an equation. The corresponding sign for not equal to would be!= Lecture 1 Introduction to C/C++ 46

wrap.cpp character constants Character constants are given in... Examples \n linefeed \b backspace \o null \t tab \r carriage return Lecture 1 Introduction to C/C++ 47

Summary Introduction Background Books C++ after Java Linux, xterm and emacs Process Basic Components and Layout Simplest program cout << and cin << Layout Preprocessor Instructions and functions Program and Function Structure Data types Declarations Assignments Statements Further Preprocessor Instructions, if... else and count Characters Lecture 1 Introduction to C/C++ 48