C programming examples. Prof. Gustavo Alonso Computer Science Department ETH Zürich

Similar documents
Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Sources: On the Web: Slides will be available on:

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

System Calls Related to File Manipulation

System Calls and Standard I/O

Shared Memory Segments and POSIX Semaphores 1

FOCUS Service Management Software Version 8.5 for CounterPoint Installation Instructions

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Senem Kumova Metin & Ilker Korkmaz 1

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

1. Can you access the login screen for Blackbaud s online learning environment Centra?

Chapter 6: Continuous Probability Distributions GBS221, Class March 25, 2013 Notes Compiled by Nicolas C. Rouse, Instructor, Phoenix College

Treasury Gateway Getting Started Guide

Otomasyon, Danışmanlık Ltd. Şti. INDILOAD DATACARD M AN UAL. Antetli03.doc / Page 9 of 1. Kibele Hedef mükemmele ulasmaksa.

How To Install Fcus Service Management Software On A Pc Or Macbook

Practical Probability:

CenterPoint Accounting for Agriculture Network (Domain) Installation Instructions

Tutorial on C Language Programming

/* File: blkcopy.c. size_t n

Operating Systems. Privileged Instructions

FOCUS Service Management Software Version 8.5 for Passport Business Solutions Installation Instructions

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Exchanging Files Securely with Gerstco Using gpg4win Public Key Encryption

SBClient and Microsoft Windows Terminal Server (Including Citrix Server)

STIOffice Integration Installation, FAQ and Troubleshooting

TivaWare Utilities Library

Traffic monitoring on ProCurve switches with sflow and InMon Traffic Sentinel

C PROGRAMMING FOR MATHEMATICAL COMPUTING

About the Staples PTA Discount Program

Debugging and Bug Tracking. Slides provided by Prof. Andreas Zeller, Universität des Saarlands

Lex et Yacc, exemples introductifs

VMD - continued: Making Movies

Steps to fix the product is not properly fixed issue for international clients.

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Citizen Service Management

VCB USER GUIDE FOR INDIVIUAL. VCB USER GUIDE FOR INDIVIUAL (FOR INDIVIUAL)

Informatica e Sistemi in Tempo Reale

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Remote Desktop Tutorial. By: Virginia Ginny Morris

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

Your Outlook Mailbox can be accessed from any PC that is connected to the Internet.

NWIRP Phone System Quick Start Guide Version /07/2009

Member Functions of the istream Class

IPC. Semaphores were chosen for synchronisation (out of several options).

Phone support is available if you have any questions or problems with the NASP PRO software during your tournament.

Chapter 3: Cluster Analysis

Meet Moodle Students introduction to Moodle and

ZoomText Network License Installation Guide

FAQs for Webroot SecureAnywhere Identity Shield

Click Studios. Passwordstate. RSA SecurID Configuration

Readme File. Purpose. Introduction to Data Integration Management. Oracle s Hyperion Data Integration Management Release 9.2.

URI and UUID. Identifying things on the Web.

How To Configure A GSM Modem Using HyperTerminal

IT Quick Reference Guides Using Outlook 2011 for Mac for Faculty and Staff

Stacks. Linear data structures

Access EEC s Web Applications... 2 View Messages from EEC... 3 Sign In as a Returning User... 3

Illustration 1: Diagram of program function and data flow

Debugging with TotalView

SoftLayer Development Lab

Lecture 25 Systems Programming Process Control

Process definition Concurrency Process status Process attributes PROCESES 1.3

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Deployment Overview (Installation):

File Handling. What is a file?

ISAM TO SQL MIGRATION IN SYSPRO

VMware View Windows XP Optimization

Logging. Working with the POCO logging framework.

PIC Online Application Help Document

Tail call elimination. Michel Schinz

Contents. Extra copies of this booklet are available on the Study Skills section of the school website (

Microsoft SQL Server Administration

Lecture Data Types and Types of a Language

Batch Prcess in C# Excel

The C Programming Language course syllabus associate level

COUNSELING DEFINITIONS

System Business Continuity Classification

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Remote Setup and Configuration of the Outlook Program Information Technology Group

Computer Science Undergraduate Scholarship

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Outlook Plug-In. Send Conference Invites from Outlook. Downloading Outlook Plug-In CONFERENCING & COLLABORATION RESERVATIONLESS-PLUS

Device Management API for Windows* and Linux* Operating Systems

ViPNet VPN in Cisco Environment. Supplement to ViPNet Documentation

Elementary Name and Address. Conversions

Siemens traffic detectors. With technologies ranging from magnetic field to Bluetooth, we offer the right detector system for any application

Customers FAQs for Webroot SecureAnywhere Identity Shield

HADOOP. Session 1: Introduction to Hadoop & Big Data. Session 2: Hadoop Distributed File Systems. Session 3: Administering Hadoop Cluster

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

HWg-Ares 12/14 MANUAL

Setup O365 mailbox access on MACs

Transcription:

C prgramming examples Prf. Gustav Alns Cmputer Science Department ETH Zürich alns@inf.ethz.ch http://www.inf.ethz.ch/department/is/iks/

Prgramming examples in C Dynamic queue Use f timing functins Generating randm numbers Arguments and main Use f static Cunting bits Prgramming examples taken frm the n-line bk: Prgramming in C UNIX System Calls and Subrutines using C http://www.cs.cf.ac.uk/dave/c/ce.html Gustav Alns, ETH Zürich. C prgramming examples 2

/* */ /* queue.c */ /* Dem f dynamic data structures in C */ #include <stdi.h> #define FALSE 0 #define NULL 0 typedef struct { int dataitem; struct listelement *link; listelement; Gustav Alns, ETH Zürich. C prgramming examples 3 d { vid Menu (int *chice); listelement * AddItem (listelement * listpinter, int data); listelement * RemveItem (listelement * listpinter); vid PrintQueue (listelement * listpinter); vid ClearQueue (listelement * listpinter); main () { listelement listmember, *listpinter; int data, chice; listpinter = NULL; Menu (&chice); switch (chice) { case 1: printf ("Enter data item value t ad scanf ("%d", &data); listpinter = AddItem (listpinter, d break; case 2: if (listpinter == NULL) printf ("Queue empty!\n"); else listpinter = RemveItem (listp break; case 3: PrintQueue (listpinter); break; case 4: break; default: printf ("Invalid menu chice - try ag break; while (chice!= 4); ClearQueue (listpinter); exit (0); /* main */

vid Menu (int *chice) { char lcal; printf ("\nenter\t1 t add item,\n\t2 t remve item\n\ \t3 t print queue\n\t4 t quit\n"); d { lcal = getchar (); if ((isdigit (lcal) == FALSE) && (lcal!= '\n')) { printf ("\nyu must enter an integer.\n"); printf ("Enter 1 t add, 2 t remve, 3 t print, 4 t quit\n"); while (isdigit ((unsigned char) lcal) == FALSE); *chice = (int) lcal - '0'; Gustav Alns, ETH Zürich. C prgramming examples 4

listelement * AddItem (listelement * listpinter, int data) { listelement * lp = listpinter; if (listpinter!= NULL) { while (listpinter -> link!= NULL) listpinter = listpinter -> link; listpinter -> link = (struct listelement *) mallc (sizef (listelement)); listpinter = listpinter -> link; listpinter -> link = NULL; listpinter -> dataitem = data; return lp; else { listpinter = (struct listelement *) mallc (sizef (listelement)); listpinter -> link = NULL; listpinter -> dataitem = data; return listpinter; Gustav Alns, ETH Zürich. C prgramming examples 5

listelement * RemveItem (listelement * listpinter) { listelement * tempp; printf ("Element remved is %d\n", listpinter -> dataitem); tempp = listpinter -> link; free (listpinter); return tempp; vid PrintQueue (listelement * listpinter) { if (listpinter == NULL) printf ("queue is empty!\n"); else while (listpinter!= NULL) { printf ("%d\t", listpinter -> dataitem); listpinter = listpinter -> link; printf ("\n"); vid ClearQueue (listelement * listpinter while (listpinter!= NULL) { listpinter = RemveItem (listpinter Gustav Alns, ETH Zürich. C prgramming examples 6

Use f timing functins /* timer.c */ /* Cmputes the time in secnds t d a cmputatin */ #include <stdi.h> #include <sys/types.h> #include <time.h> main() { int i; time_t t1,t2; (vid) time(&t1); fr (i=1;i<=300;++i) printf(``%d %d %dn'',i, i*i, i*i*i); (vid) time(&t2); printf(``n Time t d 300 squares and cubes= %d secndsn'', (int) t2-t1); Gustav Alns, ETH Zürich. C prgramming examples 7

Generating randm numbers /* randm.c */ #include <stdi.h> #include <sys/types.h> #include <time.h> main() { int i; time_t t1; (vid) time(&t1); srand48((lng) t1); /* use time in secnds t set seed */ printf( 5 randm numbers (Seed = %d):n,(int) t1); fr (i=0;i<5;++i) printf( %d, lrand48()); printf( \n\n ); /* flush print buffer */ Gustav Alns, ETH Zürich. C prgramming examples 8

On randm number generatin in C lrand48() returns nn-negative lng integers unifrmly distributed ver the interval (0, 2**31). A similar functin drand48() returns duble precisin numbers in the range [0.0,1.0). srand48() sets the seed fr these randm number generatrs. It is imprtant t have different seeds when we call the functins therwise the same set f pseud-randm numbers will generated. time() always prvides a unique seed. Gustav Alns, ETH Zürich. C prgramming examples 9

Arguments and main #include <stdi.h> main(int argc, char **argv) { /* prgram t print arguments frm cmmand line */ int i; printf("argc = %d\n\n",argc); fr (i=0;i<argc;++i) printf("argv[%d]: %s\n",i, argv[i]); Gustav Alns, ETH Zürich. C prgramming examples 10

Use f static #include <stdi.h> vid stat(); main() { int cunter; /* lp cunter */ fr (cunter = 0; cunter < 5; cunter++) { stat(); vid stat() { int temprary = 1; static int permanent = 1; (vid)printf("temprary %d Permanent %d\n", temprary, permanent); temprary++; permanent++; Gustav Alns, ETH Zürich. C prgramming examples 11

Cunting bits int bitcunt(unsigned char x) { int cunt; fr (cunt=0; x!= 0; x>>=1) if ( x & 01) cunt++; return cunt; Gustav Alns, ETH Zürich. C prgramming examples 12