Transcription

ilPragneshPatel

ProgrammingLanguages ofinstruc.onstoamachine Splitintotwocomponents,SyntaxandSyman.cs– ls– Syman.cs–providestheruleforinterpre.ngthesyntax

nguages:– Dynamic Datatypesareinferredanddeterminedduringrun.me– Sta.c on– Func.onal puta.on– ObjectOriented aluesandassociatedprocedurescallmethods

DynamicVs.Sta.cPythondata1 100.53;legaldata1 “hahaha”;Cdoubledata1 100.53;illegal/notvaliddata1 “hahaha”

CompiledLanguages C,C andFortranareallcompiledprogramminglanguages Ex.gcc–cmyfunc*on.c rmanexecutable– Ex.gccfile1.ofile2.o- ‐omyprog

Scrip.ngLanguages Scrip.nglanguagesdonotrequireacompiler Areinsteadinterpretedduringrun.me dotorun.meoverhead ExampleLanguages– Anyshelllanguage(bash,csh,zsh,tcsh,fish)– Python,Perl,Ruby,JavaScript

Compiling blebinaryfile Compilingisatwostepprocess– executable EX.– Compile:gcc–chello.c hello.o– Link:gcchello.o–ohello hello

Compilerscont amilyofcompilers– gcc,g ,gfortran rpar.cularsystemsandarchitectures– Intel,PGI,andCray

Fortran yIBMwiththefirstFORTRANcompilerbuiltin1957 FORTRANstandsforFormulaTransla.ngsystem doneata.meintothemachine.NOMONITORS! FortranisnowanObject- ‐Orientedlanguage puta.onalfields

:X 1.0,Y 2.0,Z items:',X,Y,ZWRITE(*,*)ArithMean (X Y Z)/3.0GeoMean (X*Y*Z)**(1.0/3.0)HarmMean 3.0/(1.0/X 1.0/Y 1.0/Z)WRITE(*,*)'Arithme.cmean ',ArithMeanWRITE(*,*)'Geometricmean ',GeoMeanWRITE(*,*)'HarmonicMean ',HarmMeanENDPROGRAMComputeMeans Beginsbydefiningthenameoftheprogram d REAL–floa.ngpointdatatype WRITE(*,*)Printstoscreen Performarithme.cfunc.ons ENDPROGRAM

ninCandC ypetoreturn.

#include stdio.h alive!Beware Firstlinecontainsapre- ndardI/Ofunc.ons ntsrequired) Callfunc.onprin7toprintcharacterstringenclosed s

Variables Variablesaredeclaredwithatype. uecanbestoredatalaterpointinthecodeintmain(){intx isdefinedy l.plevariablescanbedeclaredsimultaneouslyreturn0;} Allvariablescanbemodifiedusingseveraloperators– *,- ‐, ,/, , , ,

oidN/AEmptyreturnorinputdatatypepointer4- ndependentobjectorfunc.oninmemory

Func.ons remainfunc.onFunc.onstructure– Returntype– Func.onname– Inputvariables– Codetoexecute//func.onexample#include stdio.h intaddi.on(inta,intb){intr;r a b;return(r);}intmain(){intz;z }

Pointers yorstruct.– Ex.Intx 2;onlyu.lizes2bytesofmemory– ngvariable– Ex.int*x &y; e &denotesreturningthememoryaddressofvariabley Ifweweretooutputthevalueofxitwouldlooklike- ‐ datatofunc.ons

Pointerscont

CInputandOutput Understdio.hprinx(“string”);– Printstostdoutscanf("%d",&number);– ReadsfromstdinandassignsvaluetonumberFILE*fp fopen(“filename”,”rw”);– r.esfscanf(fp,”%d”,&number);– fp,”string”);– Printsstringvaluetofilefp

CI/Ocont Standardin/outFileI/O#include stdio.h x(“%s”,input);return0;}#include stdio.h intmain(){charinput[80];FILE*fin,fout;fin fopen(“infile”,”r”);fout ose(fout);return0;}

ControlStructuresForLoopsfor(inti 0;i 6;i ){x x i;}X ?WhileLoopswhile(x 4){x x 1;}X ?Ifstatementsif(x 0){dosomething}elseif(x 2){dosomething}else{dosomething}

Exercise- ‐1 ntitbacktothescreen

Exercise- ‐1 ntitbacktothescreen#include stdio.h ”,str);}

Exercise2 PerformExercise1withafileinstead

Exercise2 PerformExercise1withafileinstead#include stdio.h intmain(){intx;FILE*file1,file2;file1 ”,x);file2 ”,x);fclose(file1);fclose(file2);}

Exercise- ‐3 oinputvariablesandprinttothescreen

Exercise- ‐3doublecalcmean(doublex,doubley){return(x y)/2;};intmain(){doublex,y,z;x 2;y 4;calcmean(x,y);return(0);}

Exercise4 roceedingfunc.on

Exercise4#include stdio.h intfunc.on1(intx){returnfunc.on2(x) 1;};intfunc.on2(intx){returnfunc.on3(x) 1;};intfunc.on3(intx){returnfunc.on4(x) %d”,func.on1(2));return0;}

C C (hencethe ) Allfunc.onsusedinCarealsoimplementedinC eSTLandtemplateprogramming. iewofObjectOrientedprogramming

STDNamespace edunderonenameAllC thstd::– compiler

Input/Output InputandoutputishandleddifferentlythanC #include iostream bringsinallthefunc.onalityforstdI/Oinc – Providescout&cinamongstothers FileI/Odependson fstream Inser.onoperators“ “and“ ”areusedtotransferthestreamobjects Examples– std::cout “Hello”;–infile string1;

HelloWorld#include iostream intmain(intargc,char**argv){std::cout "Heybuddy" std::endl;return0;}Heybuddy iostream includesfunc.onscoutandendl String“Heybuddy”isinsertedtostd::cout std::endlactsasacarriagereturn

#include iostream #include fstream #include string ::ifstreaminfile("input.txt");infile readtxt;infile.close();std::cout "Thefirstitemofthefilereads" readtxt std::endl;std::ofstreamouxile("output.txt");ouxile readtxt;ouxile.close();return0;}FileI/O

Exercise- ‐1 filesCreateinputstringReadindataPrintoutdata

Exercise- ‐1#include iostream #include string intmain(){std::stringinputstr;std::cin inputstr;std::cout inputstr;return0;}

Exercise2 penfilePrintstringtofileCloseFile

Exercise2#include fstream #include string ::stringinstr;infile xile”);ouxile instr;ouxile.close();}

netCandC programmingtutorials– cplusplus.com– cprogramming.com

GNUMakeWhatisMake?– leprogramsandlinksexternallibraries– Canrebuildthingsforyouautoma.cally:.mestamp Itsbehaviorisdependentonrulesdefinedbytheuser ”andreadsthatfile. Differentmakefilescanbespecifiedwitha–fop.on

thefollowingshape:target.:prerequisites.recipe. thenameofanac.ontocarryout– Canbeexecutableorobjectname entupon.– Canbeanothertarget hetarget

Variables Makefilescanalsomakeuseofvariables– Variablescanbealistoffiles SOURCES main.cfunc.on1.func.on2.c– Canbeanexecutablecommand CC g – Op.onstopasstoacompiler CFLAGS - ‐g–WallO3 Variablesarereferencedusinga sign– “ (CC)–c (SOURCES)”– Equivalentto“g - ‐cmain.cfunc.on1.cfunc.on2.c”

Execu.ngaMakefile make(firsttarget) argetname– makeclean

:exercise1.cgcc–cexercise1.c

exe

silychangecompilers?Letsuseavariable.Add:“cc lateronwith (cc)Makefilecc e1.o:exercise1.c (cc)–cexercise1.c

AddingFLAGS mpilerop.ons. S - ‐Wall” Add“ (CFLAGS)”tothecompileline– “ (cc)–c (CFLAGS)exercise1.c”Makefilecc gccCFLAGS - ‐Wallmyexe:exercise1.o (cc)exercise1.o–omyexeexercise1.o:exercise1.c (cc)–c (CFLAGS)exercise1.c

SourceCodeManagement Uselistofsourcesinstead– “SOURCES exercise1.c” Createalistofobjectfilesfromthesourcelist– “OBJECTS (SOURCES:.cpp .o)” Addtargettoconvertsourcecodeintoatargetfile– “%.o:%.c”– “ (cc)–c (CFLAGS) “ “ “referencesallprerequisites

NewMakefilecc gccCFLAGS - ‐WallSOURCES exercise1.cOBJECTS (SOURCES:.c .o)%.o:%.c (cc)–c (CFLAGS) myexe: (OBJECTS) (cc) (OBJECTS)–omyexe

MakefileTemplate#AssignCompilersCC g cc gcc„n gfortran#ExecutableNameEXECUTABLE #ListofSourcesSOURCES #CompilerFlagsCFLAGS FFLAGS #MakeTargets#all: (EXECUTABLE) (EXECUTABLE): (OBJECTS)# (CC) (OBJECTS)- ‐o @# (cc) (OBJECTS)- ‐o @# („n) (OBJECTS)- ‐o @ (OBJECTS):# (CC)- ‐c (CFLAGS) (INCLUDE FLAGS) (SOURCES)# (cc)- ‐c (CFLAGS) (INCLUDE FLAGS) (SOURCES)# („n)- ‐c (FFLAGS) (INCLUDE FLAGS) (SOURCES)clean:rm- ‐rf (OBJECTS) (EXECUTABLE)

e/make/manual/make.html

oundonalmostanyLinux/Unixbasedcomputersystem. Scrip.nglanguageaswellcommandlineinterface inputforapajernmatch ent.meWill.meanexecutedcommand/program

uage

BashScrip.ng Simpletextfilebegunwith#!/bin/bash– locatedunderthe/bindirectory ashexample

Execu.ngBashScripts Notinherentlyexecutable.Onlyatextfile. Mustuse“chmodu shls–la 1execute:“./mylsexercise1.c” n. 2, 3etc.wouldfollow– @willpassinallargumentsfollowing

BashLoops Forloop#/bin/bashforiin (ls);doechoitem: idone WhileLoop#!/bin/bashcount 1while[ count- ‐le9]doecho" count"sleep1((count ))done

Python Pythonisahigh- ‐level(typically)scrip.nglanguage ObjectOriented Useswhite- riinrange(5):printi

la.onstheysa.sfy"number1 raw input("Pleaseenterthefirstinteger:")number1 int(number1)number2 raw input("Pleaseenterthesecondinteger:")number2 int(number2)ifnumber1 mber1! fnumber1 umber1 ifnumber1 mber2)ifnumber1 ,number2)dummy raw input()

nrange(0,10,5):printstr(x) Wonderland)countsbackwards:"forxinrange(10,0,- ‐1):printstr(x) ,actually,it'sprejyeasywhenyou'reastring."word raw input("Enteraword:")forlejerinword:printlejer

GitWhatisVersionControl?– leandsimultaneouslytracktheirhistory– ackto– ocallyorremotely– Allowsforbranching

GotoGithub Gotogithub.comandcreateanaccount Writedownusernameonpaperpassedaround

Cloneourfirstdirectory Makeadirectorytohousethetraininggitrepository– “mkdirgitrepo” Changetothenewdirectoryandthenclonetherepo– “cd”– “gitclonehjps://github.com/jics- ‐csure/training.git”GitCommands gitadd gitrm gitstatus gitmv gitpush p”

Resources FORTRAN- ‐hjp://www.cs.mtu.edu/ ng.comC - ‐cplusplus.comBashPythonGit