Where we are Distributed and Parallel Technology. Union Types. A Tree Structure. t tag node. C Revision (Part II)

Similar documents
CSC 1107: Structured Programming

CSC 1107: Structured Programming

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

F28HS2 Hardware-Software Interface. Lecture 2: Programming in C - 2

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

File Access. FILE * fopen(const char *name, const char * mode);

File IO and command line input CSE 2451

25.2 Opening and Closing a File

A case study of delegates and generics in C#

EL2310 Scientific Programming

2009 S2 COMP File Operations

C programming basics T3-1 -

Contents. A Review of C language. Visual C Visual C++ 6.0

Review of the C Programming Language for Principles of Operating Systems

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

The C Programming Language

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Programming in C Quick Start! Biostatistics 615 Lecture 4

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Procedural programming with C

Model Viva Questions for Programming in C lab

F28HS Hardware-Software Interface: Systems Programming

EL2310 Scientific Programming

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Course organization. Course introduction ( Week 1)

Answer all questions. Write your answers only in the space provided. Full marks = 50

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Review of the C Programming Language

Standard File Pointers

CSCI 171 Chapter Outlines

Scientific Programming in C X. More features & Fortran interface

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

C-LANGUAGE CURRICULAM

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Tin học cơ sở 4$ Structures!

Short Notes of CS201

Following is the general form of a typical decision making structure found in most of the programming languages:

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

MODULE 3: Arrays, Functions and Strings

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

CS201 - Introduction to Programming Glossary By

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Introduction to C++ with content from

Industrial Programming

A Fast Review of C Essentials Part I

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

C library = Header files + Reserved words + main method

Input / Output Functions

Bit Manipulation in C

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

File Handling in C. EECS 2031 Fall October 27, 2014

Simple Output and Input. see chapter 7

F28HS Hardware-Software Interface: Systems Programming

Text Output and Input; Redirection

Long Questions. 7. How does union help in storing the values? How it differs from structure?

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

C# Data Manipulation

Basic and Practice in Programming Lab 10

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Arrays (1A) Young Won Lim 1/27/17

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Distributed and Parallel Technology

MTH 307/417/515 Final Exam Solutions

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Lab # 4. Files & Queues in C

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

Multiple Choice Questions ( 1 mark)

EM108 Software Development for Engineers

Computer Science & Engineering 150A Problem Solving Using Computers

Kurt Schmidt. October 30, 2018

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

C# Data Manipulation

Main differences with Java

What is recursion. WAP to find sum of n natural numbers using recursion (5)

Module 6: Array in C

Introduction to Programming Using Java (98-388)

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Data Type Fall 2014 Jinkyu Jeong

Lesson #8. Structures Linked Lists Command Line Arguments

CS240: Programming in C

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

Summer June 15, 2010

Computer Programming Unit v

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Pace University. Fundamental Concepts of CS121 1

COMP 2355 Introduction to Systems Programming

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

ECE264 Fall 2013 Exam 1, September 24, 2013

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

Transcription:

Where we are Distributed and Parallel Technology C Revision (Part II) Hans-Wolfgang Loidl http://www.macs.hw.ac.uk/~hwloidl School of Mathematical and Computer Sciences Heriot-Watt University, Edinburgh So far we covered: C Program Layout C Types: Basic C Types Arrays Structures Pointers Type Coercions 0 No proprietary software has been used in producing these slides Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 1 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 2 / 34 Union Types Union types can take different types of value at different times To declare a union type, use union identifier { type1 id1; typen idn ; union identifier is the name of a new type can be either type1, accessed through id1, or type2 accessed through id2 etc space is allocated for the largest typei other types are aligned within this space NB: no way to tell at run time which type is intended (use an explicit tag) To declare a variable of union type, use union identifier1 identifier2; identifier2 is a variable of type union identifier1 A Tree Structure /* enumeration of possible tags of type int */ typedef enum { Leaf = 0, Branch = 1 Tag; /* define composition of a branch */ struct branch { struct tree *left, *right; ; /* define alternatives of a node */ union node { struct branch b; int value; ; /* define a tree as a tagged node */ struct tree { Tag tag; union node n; ; /* allocate a pointer to a tree */ struct tree *t; /* allocate a tree structure */ t = (struct tree *) malloc(sizeof(struct tree)) t tag node Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 3 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 4 / 34

A Tree Structure /* enumeration of possible tags of type int */ typedef enum { Leaf = 0, Branch = 1 Tag; /* define composition of a branch */ struct branch { struct tree *left, *right; ; /* define alternatives of a node */ union node { struct branch b; int value; ; /* define a tree as a tagged node */ struct tree { Tag tag; union node n; ; /* allocate a pointer to a tree */ struct tree *t; /* allocate a tree structure */ t = (struct tree *) malloc(sizeof(struct tree)) t t >n.b tag t >n.b.left t >n.b.right Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 5 / 34 b A Tree Structure /* enumeration of possible tags of type int */ typedef enum { Leaf = 0, Branch = 1 Tag; /* define composition of a branch */ struct branch { struct tree *left, *right; ; /* define alternatives of a node */ union node { struct branch b; int value; ; /* define a tree as a tagged node */ struct tree { Tag tag; union node n; ; /* allocate a pointer to a tree */ struct tree *t; /* allocate a tree structure */ t = (struct tree *) malloc(sizeof(struct tree)) t tag value t >n.value Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 6 / 34 Example Tree int a[4] = { 1, 2, 3, 4 ; t = mktree(0,3,a); t B B n.b.left n.b.right n.b.left n.b.right B n.b.left n.b.right L 1 L 2 L 3 L 4 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 7 / 34 Example Code: Building a Tree struct tree * mktree(int from, int to, int *arr) { if (from>to) { return (struct tree *)NULL; else if (from==to) { struct tree * t; t = (struct tree *) malloc(sizeof(struct tree)); t->tag = Leaf; t->n.value = arr[from]; return t; else { struct tree * t, *left, *right; int mid = (from + to) / 2; left = mktree(from,mid,arr); right = mktree(mid+1,to,arr); t->tag = Branch; t->n.b.left = left; t->n.b.right = right; return t; Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 8 / 34

Exercise Control Constructs Useful exercises, involving this tree representation, are at the end of this set of slides. Strong syntactic similarity to C# or Java BUT: without object-orientation, ie. semantic differences Assignment expression1 = expression2; set address from expression1 to value from expression2 block statement { declarations; statements declarations are optional declared values are local to statements Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 9 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 10 / 34 Conditional Constructs: if Conditional Constructs: switch if (expression) statement1; else statement2; A (single-selection) conditional statement NB: in C there are no true or false values 0 is interpreted as false every other value is interpreted as true the else branch is optional switch(expression) { case constant1: statements1; default: statements; A (multi-selection) conditional statement statements can be one or more statement a break leaves the switch statement NB: it s possible to fall-through to the next block, if there is no break (unlike in C#) the default branch applies, if none of the previous cases matches Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 11 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 12 / 34

Loops: while Loops: for for (statement1;expression;statement2) statement3; while (expression) statement; A While loop: execute statements, while expression is true statement is optional to combine statements, use the blocking statement {... A For loop statement1 performs initialisation (once) expression checks for termination (start of each iteration) statement2 prepares for the next iteration, eg. increases the counter (end of each iteration) statement3 is the loop body (optional) break can be used in while or for loops to terminate the loop; continue can be used to jump to next iteration Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 13 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 14 / 34 Functions Parameter Passing To define a function, use type identifier(id1,id2) type1 id1; type2 id2; { declarations; statements; The return type is optional by default the type is int if the function does not return a value, the type is void declarations is optional (they declare local variables) id1, id2... are the formal parameters can also be defined inline as: type identifier(type1 id1,type2 id2,) Parameter passing is call by value, i.e. value of the actual parameter is copied to the space for the formal parameter thus, a change to formal does not change actual use address operators for call by reference call function with &actual in function manipulate *formal to change actual NB: arrays effectively passed by reference Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 15 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 16 / 34

Input/Output Files are character sequences Access files via a FILE * To open a file, use FILE * fopen(char *string, char mode) string must contain the path to a file mode can be r for read, w for write or a for append the return value is a file pointer or NULL if no file To close a file, use fclose(file *fd) for input/output, C handles characters as ints i.e. 16 bit values To read the next value, use int getc(file *fd) This returns EOF at the end of the file. To write the next value, use int putc(int c,file *fd) This returns EOF on failure Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 17 / 34 Input/Output To read formatted data, use int fscanf(file *fd, char *string, lval1, lval2) string specifies format of input, using these conversion specifications %c for character %d for integer %f for double lval is address of where to put converted byte sequence must be one conversion spec for each lval To write formatted data, use int fprintf(file *fd, char *string, exp1, exp2) string is format for ouput mix text with conversion specs must be one conversion spec for each exp conversion spec as above additionally: %s for string can precede format character with width/precision info eg. %4.2f for double, at least 4 chars wide with 2 chars after decimal point Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 18 / 34 Input/Output For keyboard input, use int getchar() same as getc(stdin) or scanf(char *string, lval1, lval2) same as fscanf(stdin, string, lval1, lval2) For screen output, use int putchar(int n) same as putc(int,stdout) or printf(char *string, exp1, exp2) fprintf(stdout,string,exp1,exp2) For command line input, use main(argc,argv) int argc; char ** argv; argc is count of number of command line arguments argv are the command line arguments (including the name of the executable at position 0) Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 19 / 34 Example: Matrix Multiplication We want to define integer matrices, held in file as: matrix1: row 1/col 1 matrix1: row 1/col n matrix1: row m/col 1 matrix1: row m/col n using 2D array representation with fixed maximum sizes Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 20 / 34

Example: Matrix Multiplication #include <stdio.h> #include <stdlib.h> #define MAXR 5 #define MAXC 5 /* read a matrix from a stream */ readmatrix(fin,m,m,n) FILE * fin; /* input stream */ int M[][MAXC]; /* matrix to store values in */ int m,n; /* number of rows and columns*/ { int i,j; for(i=0;i<m;i++) /* iterate over all rows */ for(j=0;j<n;j++) /* iterate over all columns */ fscanf(fin,"%d",&(m[i][j])); /* read data */ must specify size of column for 2D array parameter use &(M[i][j]) to pass address of element i/j to fscanf Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 21 / 34 Example: Matrix Multiplication Similarly, for writing a matrix: /* write a matrix to a stream */ writematrix(fout,m,m,n) FILE * fout; /* output stream */ int M[][MAXC]; /* matrix to read values from */ int m,n; /* number of rows and columns */ { int i,j; for(i=0;i<m;i++) /* iterate over all rows */ { for(j=0;j<n;j++) /* iterate over all columns */ fprintf(fout,"%d ",M[i][j]); /* write data */ putc( \n,fout); Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 22 / 34 Example: Matrix Multiplication Now, the classical matrix multiplication function, as 3 nested loops: Matrix Multiplication /* Input: integer matrices M1 (m row, n columns), M2 (n row, m columns) */ /* Output: integer matrices M3 (m row, m columns), s.t. M3 = M1 * M3 */ matrixprod(m1,m2,m3,m,n) int M1[][MAXC]; /* 1st input matrix */ int M2[][MAXC]; /* 2nd input matrix */ int M3[][MAXC]; /* Output matrix */ int m,n; /* Number of rows and columns of M1 */ { int i,j,k; for(i=0;i<m;i++) /* iterate over all rows */ for(j=0;j<m;j++) /* iterate over all columns */ { M3[i][j]=0; for(k=0;k<n;k++) /* compute dot product */ M3[i][j] = M3[i][j]+M1[i][k]*M2[k][j]; Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 23 / 34 0 Picture from http://en.wikipedia.org/wiki/matrix_multiplication Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 24 / 34

File Format m n matrix1: row 1/col 1 matrix1: row 1/col n matrix1: row m/col 1 matrix1: row m/col n matrix 2: row 1/col 1 matrix2: row 1/col m matrix 2: row n/col 1 matrix2: row n/col m Main Function main(argc,argv) int argc; char ** argv; { FILE * fin; int m1[maxr][maxc]; int m2[maxr][maxc]; int m3[maxr][maxc]; int m,n; fin = fopen(argv[1],"r"); fscanf(fin,"%d %d",&m,&n); readmatrix(fin,m1,m,n); readmatrix(fin,m2,n,m); fclose(fin); writematrix(stdout,m1,m,n); putchar( \n ); writematrix(stdout,m2,n,m); putchar( \n ); matrixprod(m1,m2,m3,m,n); writematrix(stdout,m3,m,m); Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 25 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 26 / 34 Main Function Example: Matrix Multiplication 2 Discussion open file in read mode using argv[1] as file name pass addresses for int variables m and n to fscanf stdout as file pointer for writematrix/fprintf Disadvantages of 2D array representation: have to allocate worst case space poor representation for parallelisation may want to locate parallelism in row/column manipulation Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 27 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 28 / 34

Example: Matrix Multiplication 2 Instead, use an array of arrays int * allocvector(int n) { return (int *)malloc(n*sizeof(int)); int ** allocmatrix(int m, int n) { int ** newm = (int **)malloc(m*sizeof(int *)); int i; for (i=0; i<m; i++) newm[i] = allocvector(n); return newm; Discussion allocmatrix returns array of m arrays of n ints in other functions, only need to change matrix type from [][] to ** Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 29 / 34 Array data layout Layout of a matrix generated by allocmatrix: a[0] a[0][0] a[0][4] a[1] a[2] a[3] a[4] a[1][0] a[2][0] a[3][0] a[4][0] a[1][4] a[2][4] a[3][4] a[4][4] Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 30 / 34 Example: Matrix Multiplication 2 Example: Matrix Multiplication 2 readmatrix(fin,m,m,n) FILE * fin; int ** M; writematrix(fout,m,m,n) FILE * fout; int ** M; matrixprod(m1,m2,m3,m,n) int **M1,**M2,**M3; main(argc,argv) int argc; char ** argv; { FILE * fin; int ** m1; int ** m2; int ** m3; int m,n; m1 = allocmatrix(m,n); m2 = allocmatrix(n,m); m3 = allocmatrix(m,m); Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 31 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 32 / 34

Example: Optimised Matrix Multiplication matrixprod(m1,m2,m3,m,n,z) int **M1,**M2,**M3; int m,n,z; { int i,j,k,ii,jj,kk,temp; int *pa, *pb; for (jj=0; jj<m; jj=jj+z) for (kk=0; kk<n; kk=kk+z) for (i=0; i<m; i++) for (j=jj; j < jj+z; j++) { pa = &M1[i][kk]; pb = &M2[kk][j]; temp = (*pa++)*(*pb); for (k=kk+1; k < kk+z; k++) { pb = pb+m; temp += (*pa++)*(*pb); M3[i][j] += temp; Exercise Write a function that checks, whether a tree is balanced. Write a function struct node *readtree(file *fin, int n) that reads n integer values from a file and builds a balanced tree. Write a function void insert(struct node *t, int n) that inserts a value n into a tree, whose leaves are sorted in ascending order. For an input array of length n, how much space does such a tree consume? Change the tree data structure, to use a more efficient representation of the tag. Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 33 / 34 Hans-Wolfgang Loidl (Heriot-Watt Univ) F21DP2 2016/2017 C Revision (Part II) 34 / 34