Download presentation
Presentation is loading. Please wait.
Published byAlexina Richardson Modified over 9 years ago
1
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R 2 nd Year C and R Workshop Part of module PA2930 – 2.5 credits Venue: Computer terminal rooms G+H Duration: 4x3 hour workshop sessions Script on web: http://www.star.le.ac.uk/zrw/compshop http://www.star.le.ac.uk/zrw/compshop Re-read the script of the 1 st year workshop
2
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Purpose of the Workshop Programming in C on a Linux machine (SPECTRE) –Editing source files, compiling and running a C program –Variables and data types, arithmetic and mathematical functions –Printing output, reading data –Symbolic constants and preprocessor –Repetition –Logical and relational operators, conditional structures –Arrays and pointers –Defining and using functions Using R to plot data created by a C program Using R to call C functions directly
3
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R What is LINUX (UNIX)? It is an operating system widely used by scientific programmers It is designed for software development rather than just software usage The simple interface between the user and the system is called a shell There is a core of commands/applications common to all flavours Now has a full Windows-like interface
4
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Why C? It is probably the best general purpose programming language It is ubiquitous It is easy to translate programs from C into other procedural languages, e.g. Fortran It is an excellent vehicle for teaching low- level procedural programming Programming using C is a useful skill for many careers
5
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Getting started with SPECTRE Logon to a UoL IT Windows machine Use the X-Terminal client NX to connect to SPECTRE (only need to install NX once) –Start Program Installer NX Client click –Start All Programs NX Client click SPECTRE click Logon to SPECTRE using you UoL IT username and password Start a terminal window on SPECTRE –Applications Accessories Terminal click
6
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Shell commands and files The default shell on SPECTRE is Bash (Bourne again shell) –prompt will look like [zrw@spectre~]$ Files are organised in a directory tree Commands are like R but no () required –pwd print working directory – cd to change directory – ls to list files in a directory There are 2 basic forms of file –Text files – contain just printable and simple control characters (carriage return, line feed, tab) – can be read or printed out –Binary files – contain binary codes, binary numbers… can only write or read using computer programs
7
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Creating a text file with an editor Use the command –$ nedit afile.txt & $ is the shell prompt nedit is the command which starts the text editor afile.txt is the name of the file & runs the editor as a background task Use the File tab (top left) to Save the file (but leave the editor running) Use the mouse to switch focus to terminal Use command –$ more afile.txt –This lists file at the terminal
8
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Creating/Compiling a C program Use the text editor to create a source text file $ nedit myprog.c & Use the compiler to translate (compile) the program into object code (in a binary file) $ cc myprog.c –o myprog Check your files – source file and executable file $ ls myprog myprog.c Run the program $./myprog
9
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Algorithms and procedural programming An algorithm is a specified sequence of steps which will solve a problem Procedural programming is used to implement algorithms In the 1 st and 2 nd Year workshops you learn the elements of procedural programming using R and C These same elements are common to all languages They enable you to solve many, many problems
10
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R The structure of algorithm/program There are usually 3 phases: –Setting up – defining variables, getting data –Performing a sequence of manipulations, arithmetic, decisions and repetitions –Listing or saving the answers and clearing up In many languages the program is broken into lines or statements. There are 2 kinds of statement: –Instructions to the compiler to set up the problem – these do something during the compilation process –One step in the algorithm or procedure- these do something when the program is run
11
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R The anatomy of a C program /* prog1 - a simple first program in C */ #include int main() { /* Declare variables */ float a, b, sum; /* Assign values to variables */ a = 10.0; b = 2.0; /* Calculate the sum, print it out */ sum = a + b; printf("The sum is %f\n", sum); }
12
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R Elements of procedural programming Operators – arithmetic, logical In-built functions Scalar variables, arrays, structures Referencing components Listing – controlling precision Input/Output (IO) of data (from/to terminal, files…) User defined functions Conditional structures (If else…) Repetition (for, while…)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.