120 likes | 277 Views
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 Re-read the script of the 1 st year workshop. Purpose of the Workshop.
E N D
2nd 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 • Re-read the script of the 1st year workshop
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
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
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
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) • StartProgramInstallerNX Client click • StartAllProgramsNX Client click SPECTRE click • Logon to SPECTRE using you UoL IT username and password • Start a terminal window on SPECTRE • Applications Accessories Terminal click
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 inadirectory • 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 readusing computer programs
Creating a text file with an editor • Use the command • $ neditafile.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
Creating/Compiling a C program • Use the text editor to create a source text file • $ neditmyprog.c & • Use the compiler to translate (compile) the program into object code (in a binary file) • $ cc myprog.c –omyprog • Check your files – source file and executable file • $ ls • myprogmyprog.c • Run the program • $ ./myprog
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 1st and 2nd 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
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
The anatomy of a C program /* prog1 - a simple first program in C */ #include <stdio.h> #include <stdlib.h> #include <math.h> 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); }
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…)