c++ programing to beginners - lmp.uni-saarland.de · • brief introduction to linux essentials and...

26
C++ programing to beginners Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov E-mail: [email protected] 12 April 2019 Lecture 1 1

Upload: others

Post on 31-Aug-2019

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

C++programingtobeginners

Lecturer:Dr.AnleWang&Dr.SergeySukhomlinov

E-mail:[email protected]

12April2019 Lecture1 1

Page 2: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• This course gives a practical introduction toprogram by using C++ language and python(introduction). At the end of the course, youshould be able to:

1. Formulate and write C++ programs to solve simplemathematic problems;

2. Choose proper algorithm for selected problem;

3. Developa proper structure of a C++ code;

4. Debug the existing C++ code and optimize it.

12April2019 Lecture1 2

Overviewofthiscourse

Page 3: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Course Time (12-13 weeks)üMonday 12:00-14:00; CIP pool (tutorial time)

ü Thursday 10:00-12:00; CIP Pool (lecture time)

• Assessmentsüweekly homework (deadline: before next lecture time)

http://www.lmp.uni-saarland.de/index.php/computeranwendungen-ss-2019/

ümidterm (qualified for final exam)

ü 3 projects (TBD)

ü final exam

12April2019 Lecture1 3

Overviewofthiscourse

Page 4: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• The most important thing for programming isPRACTICE!• Try to write your own code or repeat others’ codeby typing it and understanding it!• Try to define and analyze the problem, then code it.• Studying outside the course is extremely important!• Classic C++ reference:C++ primer.www.cplusplus.com; www.cppreference.com

12April2019 Lecture1 4

Overviewofthiscourse

Page 5: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• BriefintroductiontoLinuxessentialsandC++language;• Gnuplot exercises;• AfirstglanceofC++code;

• Afterthislecture,youshouldbeableto:ü IdentifyeachpartofasimpleC++code.ü CreatingandmovingfilesanddirectoriesunderLinuxsystem.

12April2019 Lecture1 5

ContentsofLecture1

Page 6: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

12April2019 Lecture1 6

Linuxessentials

• Linuxisanoperatingsystem.• Canoperatewithcommandlineinterface.• Automatebyscripting.• Efficientforcoding,scientificcalculationswith

considerationofperformance.• …

Page 7: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Usefulcommandsforfileoperation:ü Navigationthroughdirectorytrees:cdü Creatingandmakedirectories:mkdir,rmdir,rm -rü Optionsandmanpages:ls,manü Copyandmovefiles:cp,mvü Executetheprogram:...ü ….

12April2019 Lecture1 7

Linuxessentials

Page 8: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Highlevelprograminglanguage:visualbasic,C,C++,C#,python,fortran (strongabstraction,syntax)• Compiler:Translatehighlevellanguagetomachineidentifiedcodeandexecute.(gcc,g++)

12April2019 Lecture1 8

Evolutionofprograminglanguage

Page 9: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Widelyused,especiallyinlargesystemwhichrequires

performance.(Microsoftoffice,AdobePhotoshop,Chrome,

Visualstudio,etc)

• Compliedlanguage(needacompliertotranslateitto

machinecodeandexecutedbyCPU).

• Object-orientedlanguage.

12April2019 Lecture1 9

C++language

Page 10: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• UseaneditortocreateasourceprograminC++;• Preprocessordirectivesbeginwith#andareprocessedbyathepreprocessor;• Usethecompiler to:• Checkiftheprogramobeystherules;• Translateintomachinelanguage(objectprogram).

12April2019 Lecture1 10

ProcessingaC++program

Page 11: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

12April2019 Lecture1 11

ProcessingaC++program

• Linker:CombinesobjectprogramwithotherprogramsprovidedbytheSDKtocreateexecutablecode

• Loader:Loadsexecutableprogramintomainmemory

• Thelaststepistoexecutetheprogram

SDK:SoftwareDevelopmentKit

Page 12: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

Thisprogramevaluatesanddisplaysthevalueofsumfrom1to100.

12April2019 Lecture1 12

AfirstglanceatC++code

Page 13: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• TheC++sourcefile,consistsofasequenceofstatements,terminatedbyasemicolon; ,Andexecutedinorder.

• Compoundstatementswithbracket{}.12April2019 Lecture1 13

AfirstglanceatC++code

Page 14: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Casesensitive;nTerms isnoequaltonterms.• Indentation,whitespaceandnewlinearefreeform.(Tips:Use

indentationwhenwithnewsubroutineornewloop)12April2019 Lecture1 14

AfirstglanceatC++code

Page 15: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Comments arewrittenbyprogrammer,ignoredbymachine.• Anytextafter// ortextbetween/* and*/ isinterpretedby

comments.12April2019 Lecture1 15

AfirstglanceatC++code

Page 16: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• include<iostream>meanscallingthelibrary ofiostream,whichdefinestheoutputtodisplayonthescreen.

12April2019 Lecture1 16

AfirstglanceatC++code

Page 17: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Thefunctiondefinitiondefinethemainfunction,whichcalledtheprogramstarts.

• Thecodebetweenbracesisthecodeassociatedwiththefunction,calledthebody offunction.12April2019 Lecture1 17

AfirstglanceatC++code

Page 18: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Thesedeclarationsgivesthreenewvariables,andinitialize them.Variablesmustbedeclaredbeforetheyareused.

• doubleandint isthedifferenttypeofvariables.12April2019 Lecture1 18

AfirstglanceatC++code

Page 19: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Thewhile givesaloop whichloopsthestatementbetweenbraces{} untiltheconditionisnotfulfilled.

• Inthiscase,programloopswheni<=100.12April2019 Lecture1 19

AfirstglanceatC++code

Page 20: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Thestatementloopswheni<=100.• Eachlooptheprogramaddi intosumwhichmeanssum=1+2+

3+4+…+100.12April2019 Lecture1 20

AfirstglanceatC++code

Page 21: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Eachloopincreasei byone.Sincewestartfromi=1,andwheni<=100loopends,weloopthestatement100times.

12April2019 Lecture1 21

AfirstglanceatC++code

Page 22: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Thestatementdisplaystheresultofthecalculationonscreen.• std::cout andstd::endl areobjectsfromlibraryiostream.

12April2019 Lecture1 22

AfirstglanceatC++code

Page 23: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• A returnstatementimmediatelyexitthefunction.• Areturninmainfunctionexittheprogram.12April2019 Lecture1 23

AfirstglanceatC++code

Page 24: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• C++installation:install the compiler of C++ on your own platform

(windows, mac, Linux); write, compile and execute thecode shown in the class.

• Write, compile and execute the code showing “Helloworld!” or any other string you like. Please try to runyour codewith command line interface.

12April2019 Lecture1 24

Homework

Page 25: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• InstallaC++complieronyourownplatform,wewillofferhelpifyouhaveanyproblem;• PracticeLinuxcommandstomakelifeeasier;• gnuplot tomakeplotdataeasier;• Writeasmallcodesuchasadd,subtract,“helloworld”,etc.

12April2019 Lecture1 25

TutorialhournextMonday

Page 26: C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and C++ language; ... üCreating and moving files and directories under Linux system

• Basictypes,basicoperatorsandsimpleinput,output stream.

12April2019 Lecture1 26

NextLecture