Summary of basic C++-commands

Similar documents
C++ Overview. Chapter 1. Chapter 2

C++ Programming Lecture 11 Functions Part I

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Chapter 2. Outline. Simple C++ Programs

A SHORT COURSE ON C++

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

Introduction. What is function? Multiple functions form a larger program Modular programming

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

Engineering Problem Solving with C++, Etter/Ingber

Operations. Making Things Happen

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.

Programming Language. Functions. Eng. Anis Nazer First Semester

CSCE 110 PROGRAMMING FUNDAMENTALS

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Chapter 6 - Notes User-Defined Functions I

Scientific Computing

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

Chapter 4: Basic C Operators

Programming. C++ Basics

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

I/O Streams and Standard I/O Devices (cont d.)

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

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:

Introduction to C++ Dr Alex Martin Room 6.10

Using Free Functions

A. Introduction to Function 1. Modular Programming input processing output functions library functions 2. Function 1. Benefit of Using Functions

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

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:

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

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

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

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

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

C++ Input/Output: Streams

CHAPTER 3 BASIC INSTRUCTION OF C++

A First Program - Greeting.cpp

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

CT 229 Java Syntax Continued

6.096 Introduction to C++ January (IAP) 2009

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

Introduction to Programming

Sketchify Tutorial Properties and Variables. sketchify.sf.net Željko Obrenović

Introduction to Programming using C++

Lab Instructor : Jean Lai

REVIEW. The C++ Programming Language. CS 151 Review #2

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

PROGRAMMING EXAMPLE: Checking Account Balance

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

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

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

CS2255 HOMEWORK #1 Fall 2012

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Problem Solving: Storyboards for User Interaction

LECTURE 02 INTRODUCTION TO C++

Objectives. In this chapter, you will:

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Fundamentals of Programming & Procedural Programming

C++ Quick Guide. Advertisements

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

MATLAB QUICK START TUTORIAL

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

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

Chapter 3 - Functions

Introduction to C ++

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

Ch 6. Functions. Example: function calls function

Name Section: M/W T/TH Number Definition Matching (8 Points)

Chapter 3. Numeric Types, Expressions, and Output

Maths Functions User Manual

Introduction to Programming EC-105. Lecture 2

2 nd Week Lecture Notes

Introduction to C++ Introduction and History. Characteristics of C++

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

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Computer Science & Engineering 150A Problem Solving Using Computers

CSCE Practice Midterm. Data Types

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

Fundamentals of Programming CS-110. Lecture 2

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

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 Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

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

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

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

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

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 (!

Introduction to C/C++

Chapter 2: Introduction to C++

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

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

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

Transcription:

Summary of basic C++-commands K. Vollmayr-Lee, O. Ippisch April 13, 2010 1 Compiling To compile a C++-program, you can use either g++ or c++. g++ -o executable_filename.out sourcefilename.cc c++ -o executable_filename.out sourcefilename.cc Each command in C++ is followed by ;. Carriage return has no meaning in C++. This means you can also distribute a command over several lines. 2 Comments Essential for the writing of clear programs are comments, which are explanations for your program, and which are ignored by the compiler. /... / puts the text... into comment (more than one line possible) //... puts text... for rest of same line into comment 3 Data Types Data Type Variable Declaration Assignment Range (Example) (Example) integer short i1, i2 ; i1 = 3; [ 32768 : 32767] int i1, i2 ; [ 2.14 10 9 : 2.14 10 9 ] long i1,i2 ; [ 2.14 10 9 : 2.14 10 9 ] unsigned i1,i2; [0 : 4.29 10 9 ] unsigned long i1,i2; [0 : 4.29 10 9 ] real float f1, f2 ; f1 = 3.2; f2 = 2.1E 3; ±[10 38 : 10 38 ] 7 digits precision double f1,f2; ±[10 308 : 10 308 ] 16 digits precision long double f1,f2; ±[10 4932 : 10 4932 ] 19 digits precision single char c1,c2; c1 = R character string of string s1,s2; s1 = "Farmer" characters logical bool b1,b2; b1 = true; b2 = false 3.1 Constants A constant value is defined with: 1

const type variablename = value; The value of a constant can not be altered after the declaration. Examples: const int numpoints = 1 0 ; const double e p s i l o n = 1E 20; 4 Input And Output 4.1 Input/Output With Screen: To be able to use the following commands you need to write #include <iostream > using namespace std ; at the beginning of your program. If you leave out the line using namespace std; you have to use std :: cout, std :: endl and std :: cin instead of cout, endl and cin. 4.1.1 output cout << " string of characters " cout << v a r i a b l e << endl ; 4.1.2 input c i n >> v a r i a b l e ; 4.2 Input/Output With Files: To be able to use the following commands you need to write #include <fstream > using namespace std ; at the beginning of your program. If you leave out the line using namespace std; you have to use std :: ofstream, and std :: ifstream instead of ofstream and ifstream. 4.2.1 output ofstream o u t f i l e v a r i a b l e ( " outputfilename ", i o s : : out ) ; o u t f i l e v a r i a b l e << value ; will write the value into the file with name outputfilename. 4.2.2 input i f s t r e a m i n f i l e v a r i a b l e ( " inputfilename ", i o s : : in ) ; i n f i l e v a r i a b l e >> a ; will read a value from the file with name outputfilename and store it in the variable a. 5 Arithmetic Calculations 5.1 Operations + / 2

5.2 Mathematical Functions To be able to use all of the following functions you need to to write at the beginning of your program: #include <cmath> C++ name function pow(x,y) x y sin(x) cos(x) tan(x) asin(x) sin 1 (x) in range [ π/2, π/2] acos(x) cos 1 (x) in range [0, π] atan(x) tan 1 (x) in range [ π/2, π/2] sinh(x) cosh(x) tanh(x) exp(x) e x log(x) ln(x) sqrt(x) x fabs(x) x floor (x) largest integer not greater than x; example: floor (5.768) = 5 ceil (x) smallest integer not less than x; example: ceil (5.768) = 6 fmod(x,y) floating-point remainder of x/y with the same sign as x x \% y remainder of x/y, both x and y integers 6 Decision Statements 6.1 Comparison Operators C++ name function example == = i1 == i2!= i1!= i2 > > i1 > i2 < < i1 < i2 >= i1 >= i2 <= i1 <= i2 && and (i1!= i2)&& (i1 == i3) or (i1 == i2) (i1 == i3) Be carefull: using the assignment = instead of the comparison == is one of the very common errors in C++. 6.2 Statements 3

else else i f else switch ( c a s e v a r i a b l e ) case value1a : case value1b : break ; case value2a : case value2b : break ; default : 6.3 Repetitions while ( c o n d i t i o n s ) for ( i n i t ; c o n d i t i o n s ; update ) do // t h e s e s t a t e m e n t s are done // b e f o r e t h e w h i l e statement i s checked while ( c o n d i t i o n ) ; 4

7 Functions A function is a set of commands. It is useful to write a user-defined function, i.e. your own function, whenever you need to do the same task many times in the program. All programs start execution at the function main. Three steps are important in the use of functions: 1. Before a function is used it has to be declared: function_type function_name(types_of_parameter_list); double f e e t i n c h t o m e t e r ( int, double ) ; void m e t e r t o f e e t i n c h ( double, int &, double &); The function type declares which variable is passed back from the function (void means none). The variables without & are all input parameters, i.e. only passed to the function and are not changed within the function. The variables with & may be passed both to and from the function and may be changed in the function. 2. In the program you use the function with: function_name(actual_parameter_list); Examples: f e e t i n c h t o m e t e r ( 5. 0, 3. 2 ) ; m e t e r t o f e e t i n c h ( 1. 3, f e e t, inch ) ; 3. At some place the function has to be defined with: function_type function_name(parameter_types_and_names) declarations and If a function is defined before its first usage, the declaration (see above, number 1) is unnecessary. Examples: double f e e t i n c h t o m e t e r ( int f e e t, double inch )... ; void m e t e r t o f e e t i n c h ( double m, int &f e e t, double &inch )... ; 5