C++ programing to beginners - lmp.uni-saarland.de · • Brief introduction to Linux essentials and...

Post on 31-Aug-2019

6 views 0 download

transcript

C++programingtobeginners

Lecturer:Dr.AnleWang&Dr.SergeySukhomlinov

E-mail:anle.wang@uni-saarland.de

12April2019 Lecture1 1

• 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

• 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

• 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

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

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

12April2019 Lecture1 5

ContentsofLecture1

12April2019 Lecture1 6

Linuxessentials

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

considerationofperformance.• …

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

12April2019 Lecture1 7

Linuxessentials

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

12April2019 Lecture1 8

Evolutionofprograminglanguage

• Widelyused,especiallyinlargesystemwhichrequires

performance.(Microsoftoffice,AdobePhotoshop,Chrome,

Visualstudio,etc)

• Compliedlanguage(needacompliertotranslateitto

machinecodeandexecutedbyCPU).

• Object-orientedlanguage.

12April2019 Lecture1 9

C++language

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

12April2019 Lecture1 10

ProcessingaC++program

12April2019 Lecture1 11

ProcessingaC++program

• Linker:CombinesobjectprogramwithotherprogramsprovidedbytheSDKtocreateexecutablecode

• Loader:Loadsexecutableprogramintomainmemory

• Thelaststepistoexecutetheprogram

SDK:SoftwareDevelopmentKit

Thisprogramevaluatesanddisplaysthevalueofsumfrom1to100.

12April2019 Lecture1 12

AfirstglanceatC++code

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

• Compoundstatementswithbracket{}.12April2019 Lecture1 13

AfirstglanceatC++code

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

indentationwhenwithnewsubroutineornewloop)12April2019 Lecture1 14

AfirstglanceatC++code

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

comments.12April2019 Lecture1 15

AfirstglanceatC++code

• include<iostream>meanscallingthelibrary ofiostream,whichdefinestheoutputtodisplayonthescreen.

12April2019 Lecture1 16

AfirstglanceatC++code

• Thefunctiondefinitiondefinethemainfunction,whichcalledtheprogramstarts.

• Thecodebetweenbracesisthecodeassociatedwiththefunction,calledthebody offunction.12April2019 Lecture1 17

AfirstglanceatC++code

• Thesedeclarationsgivesthreenewvariables,andinitialize them.Variablesmustbedeclaredbeforetheyareused.

• doubleandint isthedifferenttypeofvariables.12April2019 Lecture1 18

AfirstglanceatC++code

• Thewhile givesaloop whichloopsthestatementbetweenbraces{} untiltheconditionisnotfulfilled.

• Inthiscase,programloopswheni<=100.12April2019 Lecture1 19

AfirstglanceatC++code

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

3+4+…+100.12April2019 Lecture1 20

AfirstglanceatC++code

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

12April2019 Lecture1 21

AfirstglanceatC++code

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

12April2019 Lecture1 22

AfirstglanceatC++code

• A returnstatementimmediatelyexitthefunction.• Areturninmainfunctionexittheprogram.12April2019 Lecture1 23

AfirstglanceatC++code

• 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

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

12April2019 Lecture1 25

TutorialhournextMonday

• Basictypes,basicoperatorsandsimpleinput,output stream.

12April2019 Lecture1 26

NextLecture