EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

Similar documents
High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Programming in C. Pointers and Arrays

C-String Library Functions

COMP s1 Lecture 1

Recitation 2/18/2012

Dynamic memory allocation

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Pointers and scanf() Steven R. Bagley

EL2310 Scientific Programming

EL2310 Scientific Programming

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Intermediate Programming, Spring 2017*

Lesson #8. Structures Linked Lists Command Line Arguments

EL2310 Scientific Programming

C++ Lab 02 - Command Line Arguments and Strings

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Chapter 13. Functions and Parameter Passing (Part 2)

ECE264 Fall 2013 Exam 1, September 24, 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CS 0449 Sample Midterm

Procedures, Parameters, Values and Variables. Steven R. Bagley

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

PRINCIPLES OF OPERATING SYSTEMS

Lecture 2, September 4

Recitation: C Review. TA s 20 Feb 2017

COMP 2355 Introduction to Systems Programming

Carnegie Mellon. Cache Lab. Recitation 7: Oct 11 th, 2016

Problem Set 1: Unix Commands 1

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

Figure 1 Ring Structures

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

COL100 Lab 2. I semester Week 2, Open the web-browser and visit the page and visit the COL100 course page.

CSC111 Computer Science II

Lecture XXIV: Review Xuan Guo CSC 3210 Computer Organization and Programming Georgia State University April 23, 2015 Xuan Guo Lecture XXIV: Review

Lecture 03 Bits, Bytes and Data Types

Today s class. Review of more C Operating system overview. Informationsteknologi

CSE 333 SECTION 3. POSIX I/O Functions

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

C programming basics T3-1 -

Introduction to C. Sami Ilvonen Petri Nikunen. Oct 6 8, CSC IT Center for Science Ltd, Espoo. int **b1, **b2;

High Performance Programming Programming in C part 1

Programming the DMCC in C

Cache Lab Implementation and Blocking

Algorithms, Data Structures, and Problem Solving

EL2310 Scientific Programming

Software Development With Emacs: The Edit-Compile-Debug Cycle

518 Lecture Notes Week 3

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Introduction to Programming Block Tutorial C/C++

High Performance Computing MPI and C-Language Seminars 2009

Dynamic Memory Allocation and Command-line Arguments

Project 2: Shell with History1

Today s Learning Objectives

Creating a Shell or Command Interperter Program CSCI411 Lab

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

ECE264 Spring 2013 Exam 1, February 14, 2013

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Fundamentals of Programming Session 19

Friday, February 10, Lab Notes

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers in C. Alan L. Cox

Pointers. Pointers. Pointers (cont) CS 217

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Functions. Arash Rafiey. September 26, 2017

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Introduction to C An overview of the programming language C, syntax, data types and input/output

Reviewing gcc, make, gdb, and Linux Editors 1

just a ((somewhat) safer) dialect.

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

York University Faculty Science and Engineering Fall 2008

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

CpSc 1011 Lab 11 Pointers

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Understanding Pointers

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Memory Corruption 101 From Primitives to Exploit

CS61C : Machine Structures

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Lecture 04 Introduction to pointers

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Overview (1A) Young Won Lim 9/14/17

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

File IO and command line input CSE 2451

Exercise Session 2 Systems Programming and Computer Architecture

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Transcription:

EL2310 Scientific Programming LAB2: C lab session Patric Jensfelt, Andrzej Pronobis

Chapter 1 Introduction 1.1 Reporting errors As any document, this document is likely to include errors and typos. Please report these to pronobis@kth.se. 1.2 Acknowledgements Valuable feedback and error reports on this document have been provided by: T. Gezork 2008 Thank you! 1.3 Before getting started Please see the course materials for links to tutorials about Linux/Unix if you are not familiar with this operating system. If you have access to Linux (on your own Laptop or a CSC computer), you can solve these exercises there. Otherwise, you should download the virtual machine available from the course materials page: http://www.pronobis.pro/teaching/el2310/course-materials. 1

Chapter 2 Lab instructions 2.1 Getting help On Unix/Linux systems it is possible to get help regarding the syntax of a certain function by using the man command. These so called man-pages contains information about what include files to use, what arguments to pass and what the return values are. Test the man function on the functions atoi and atof. The syntax is man atof 2.2 Repetition Repeating a certain calculation or operation a number of times is a very common task. In C there are (at least) three ways to accomplish this. These are for, while and do-while. Write programs loop-for.c, loop-while.c and loop-do-while.c which generates the following output on the screen using different ways to do the iteration 1. 1 2 4 8 16 32 1 For more information see slides from lecture 7 2

2.3 Command line arguments In many cases you want to be able to control the execution of your program somehow. One way to do this is to have the user input information during the execution. Another way that is frequently used in the Unix/Linux world is to provide the program with command line arguments. 2.3.1 Syntax To get command line argument you need to add some input parameters to your main function. int main(int argc, char *argv[]) Here argc tells how many input arguments there are and argv contains the input arguments. Write a program, parse input.c that iterates through the input arguments and prints them one by one on the screen. Remember that char *argv[] is an array of char*, i.e. character arrays or strings (C style). Try with the following.\parse input.\parse input 1.\parse input hello world.\parse input -h.\parse input -x 4 What is argv[0]? 2.3.2 Converting char* to int and double You often want be able to input numerical values to your program. However, all inputs from the keyboard are in the form of characters (arrays). To convert from character array to a number we can use the functions atof (char* to double) and atoi (char* to int). Write a program to test atoi and atof. Try for example the following printf( %d\n, toi( 1 )); printf( %f\n, tof( 1 )); printf( %d\n, toi( 1.2 )); printf( %f\n, tof( 1.2 )); printf( %d\n, toi( 1Sven )); printf( %d\n, toi( Sven )); 3

2.3.3 Parsing of command line arguments If you only want to pass in a single value it is easy to do by simply using the argv-array directly. However when you start to get many different possible command line arguments and in addition you might want to be able to put them in different order on the command line the following construction is quite useful #include <unistd.h>... int main(int argc, char *argv[]) { const char *optstring = "i:j"; char o = getopt(argc, argv, optstring); while (o!= -1) { switch (o) { case i : printf( The argument for -i is \"%s\"\n",optarg); break; case j : printf( This option has no argument"); break; case? : fprintf(stderr, "Usage:... ); return -1; } o = getopt(argc, argv, optstring); }... } Write a program, coords.c, which you can send in x and y coordinates with command line options -x and -y. The variables x and y should be given default values x=1 and y=2 which can be overridden on the command line. The program should print out the value of x and y at the end and the sum of these. The following two calls should be possible.\coords -x 3 -y 4.\coords -y 4 -x 3.\coords -x 3.\coords -y 4 2.4 Pointers Pointers are special variables which contain the address of a variable. Knowing the address to where the variable is stored makes it possible to change the value of the variable for example. The following code declares an integer variable and 4

a pointer to an integer, this pointer is then set to point to the integer and finally the value of the integer variable is changed using the pointer to it. int a; int *p; p = &a; p = 4; Write a program with one function (in addition to the main function). The main function should call the other function which return the evaluation of a function, say cos(x) where x is the first argument to the function. The function should be written such that a second output containing the derivative of the function can be given by using a second pointer argument, i.e. double eval(double x, double *dfdx);. The function should be implemented so that the caller can choose not to have the derivative calculated. Hint: You can test if the address passed in is NULL for the second argument. 2.5 Pointers to functions Just like you can define pointers to variables you can define pointers to functions. To define a variable p which is a pointer to a function that returns a double and takes a double as argument you would write double (*p)(double); Notice that p is the name of the function. You assign a value to the pointer by p = fcn1; assuming that fcn1 is the name of a function with the same interface as above. You can then use the function by dereferencing the pointer (*p)(4.2) You can also have an array of function pointers. double (*p[4])(double); declares an array of 4 function pointers. You assign values by p[0] = fcn1; and you use them as (*p[0])(4.2); Write a program that defines several different functions with the above interface, i.e. returning a double and taking a double as argument. Create an array of pointers to these functions and loop over them and call them with some argument and print the return value. The functions could be for example x+x, x*x, sqrt(x),.... Use this to create a nice looking table along the lines of x x+x x*x sqrt(x) 1.1 2.2 1.21 1.05 1.2... 5