lec15-cs110 computational engineering

24
Data I/O Lecture 15 February 11, 2008

Upload: sriharsha-p

Post on 10-Nov-2014

74 views

Category:

Education


3 download

DESCRIPTION

A keynote on Problem Solving using Computers

TRANSCRIPT

Page 1: Lec15-CS110 Computational Engineering

Data I/O

Lecture 15February 11, 2008

Page 2: Lec15-CS110 Computational Engineering

Lab• Lab starts next week again• State in your feedback form if you need a shift

from the current allocated day along withreason. Write your roll number in this case.

• Shift is permitted only if you have toparticipate in other IIT M certified activity.

• Current allocation– RFID 1 - 60 (Mon)– RFID 61-120 (Tue)– RFID 121-180 (Wed)– RFID 181-240 (Thurs)– RFID 241-300 (Fri)

Page 3: Lec15-CS110 Computational Engineering

Lab• NO MORE RE-ALLOCATION POSSIBLE

AFTER TODAY.

Page 4: Lec15-CS110 Computational Engineering

printf() function

• printf(control string, arg1, arg2,…,argn)

Page 5: Lec15-CS110 Computational Engineering

printf()

%c - single character %d - decimal integer %e - Floating point %f - Floating point %i - decimal, hexadecimal or octal

integer %o - octal integer

Page 6: Lec15-CS110 Computational Engineering

printf()

%s - String %u - unsigned decimal %X - hexadecimal

Page 7: Lec15-CS110 Computational Engineering

Small issues

#include <stdio.h>main() { char item[20]; int partno; float cost; printf(“%s %d %f”,item,partno, cost); // printf(“%s%d%f”,item,partno,cost);}

Page 8: Lec15-CS110 Computational Engineering

Small issues

#include <stdio.h>main() { double x = 5000.0, y = 0.0025; printf(“%f %f %f\n”,x,y,x*y); printf(“%e %e %e\n”,x,y,x*y);}5000.000000 0.002500 12.5000005.000000e+03 2.500000e-03 1.250000e+01

Page 9: Lec15-CS110 Computational Engineering

Small issues#include <stdio.h>main() { /*minimum field width specifications */ int i = 12345; float x = 345.678; printf(“%3d %8d\n”,i,i); printf(“%3f %13f\n”,x,x); printf(“%3e %16e\n”,x,x);}12345 bbb12345345.678000 bbb345.6780003.456780e+02 bbbb3.456780e+02

Page 10: Lec15-CS110 Computational Engineering

Small issues#include <stdio.h>main() { /*minimum field width specifications */ float x = 345.678; printf(“%3g %13g\n”,x,x);}345.678 bbbbbb345.678

%g - shortest of the %f and %e representations.

Page 11: Lec15-CS110 Computational Engineering

Small issues#include <stdio.h>main() { /*Floating point precision */ float x = 123.456; printf(“%7f %7.3f %7.1f\n”,x,x,x);}Rounding

123.4560000 123.456 bb123.5

Page 12: Lec15-CS110 Computational Engineering

Small issues#include <stdio.h>main() { /*Floating point precision */ float x = 123.456; printf(“%12e %12.5e %12.3e\n”,x,x,x);}Rounding

1.234560e+02 1.23456e+02 bbb1.235e+02

Page 13: Lec15-CS110 Computational Engineering

Small issues#include <stdio.h>main() { /*Floating point precision */ float x = 123.456; printf(“%e %.5e %.3e\n”,x,x,x);}Rounding and no leading blank spaces

1.234560e+02 1.23456e+02 1.235e+02

Page 14: Lec15-CS110 Computational Engineering

Small issues#include <stdio.h>main() { /*String precision */ char ln[12]; …… printf(“%10s %15s %15.5s %.5s”,ln,ln,ln,ln);}Let ln be hexadecimal; always right justified.

hexadecimal bbbbhexadecimal bbbbbbbbbbhexad hexad

Page 15: Lec15-CS110 Computational Engineering

Interesting stuffs#include <stdio.h>main() { short a,b; long c,d;…… printf(“%5hd %6hx %8lo %lu”,a,b,c,d);}Minimum field width specification for int datatypes.

Page 16: Lec15-CS110 Computational Engineering

Interesting stuffs#include <stdio.h>main() { /* Uppercase conversion */ int a = 0x80ec; float b = 0.3e-12; printf(“%4x %10.2e\n”,a,b); printf(“%4X %10.2E\n”,a,b);}80ec 3.00e-1380EC 3.00E-13

Page 17: Lec15-CS110 Computational Engineering

Interesting Prefixes

• - means left justified• + means + should appear before positive

numbers• 0 means leading zeros instead of blanks• ‘ ‘ means blanks space before positive values• # in front of octal and hex mean print 0 and 0x

in front respectively.• # in front of e-, f-, g- mean put a decimal

point even if whole number

Page 18: Lec15-CS110 Computational Engineering

A sample program#include<stdio.h> main() { int j = 123; printf(“%6d\n”,j); printf(“%-6d\n”,j); printf(“%+6d\n”,j); printf(“%-+6d\n”,j);} bbb123 123bbb bb+123 +123 //The same for justifying strings

Page 19: Lec15-CS110 Computational Engineering

The gets() and puts()

• Reads till end-of-line character#include<stdio.h> main() { char line[80]; gets(line); puts(line);}

Page 20: Lec15-CS110 Computational Engineering

The fprintf()

• Similar to printf() but uses a file pointer• Similarly

– fgets(fpt,..); fputs(fpt,..);– ch = getc(fpt); putc(ch,fpt);

• fclose(fpt); versus fcloseall();

Page 21: Lec15-CS110 Computational Engineering

Binary files

• fopen(“…”,”rt”); open for read as textand is default, the suffix “t” is notneeded– Equivalent to fopen(…,”r”);

• fopen(“…”, “rb”); open for read asbinary– You may use the fwrite() command on this

file

Page 22: Lec15-CS110 Computational Engineering

Binary files

• Advantage is as follows– Four digit integers need 2 bytes– fprintf() etc. stores it in TEXT form - so 4

bytes– fwrite() stores in binary form - 2 bytes on a

file opened in binary write mode– 50% space savings - assume it stores one

million numbers.

Page 23: Lec15-CS110 Computational Engineering

A sample program#include<stdio.h> main() { FILE *fp; int I; if ((fp=fopen(“binval.dat”,”wb”)) == NULL) printf(“\n ERROR\n”); else { for (I = 10001; I <= 11000; I++) fwrite(&I, sizeof(int), 1, fp); } fclose(fp); }

Page 24: Lec15-CS110 Computational Engineering

Creative Question

• Given a Black box that takes as inputtwo lower triangular matrices andoutputs the product of the same, usethe black box to multiple two arbitrarysquare matrices.