lec14-cs110 computational engineering

28
Data I/O Lecture 14 February 7, 2008

Upload: sriharsha-p

Post on 11-Nov-2014

93 views

Category:

Education


3 download

DESCRIPTION

A keynote on Problem Solving using Computers

TRANSCRIPT

Page 1: Lec14-CS110 Computational Engineering

Data I/O

Lecture 14February 7, 2008

Page 2: Lec14-CS110 Computational Engineering

Data Input and Output

• Character I/O– getchar(); putchar();

• String I/O– gets(); puts();

• General I/O– printf(); scanf();

Page 3: Lec14-CS110 Computational Engineering

getchar()#include <stdio.h>main(){ char c; while ((c = getchar()) != EOF) printf("\n%c\n",c);}//Accepts character from keyboard and prints it//Till it sees Ctrl-D which stands for End-Of-File// EOF is defined in stdio.h

Page 4: Lec14-CS110 Computational Engineering

putchar()#include <stdio.h>main(){ char c; while ((c = getchar()) != EOF) putchar(c);}//Prints the character on the screen

Page 5: Lec14-CS110 Computational Engineering

Inputs/Outputs-previous code

Aa //InpAa //OutB //InpB //OutAaa //InpAaa /Out

Page 6: Lec14-CS110 Computational Engineering

scanf()

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

Page 7: Lec14-CS110 Computational Engineering

scanf()

%s - String followed by whitespace - nullautomatically added

%u - unsigned decimal %X - hexadecimal [***] - String with whitespace characters

Page 8: Lec14-CS110 Computational Engineering

scanf()

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

Page 9: Lec14-CS110 Computational Engineering

Output

Kama 2 4.5 //InputKama 2 4.5000 //OutputSince “\n” is a whitespace character

We may enter each input in different lines or two inone line and the remaining in the other line.

What went wrong?Nothing went wrong in the above toy codeLet us see the code in next slide

Page 10: Lec14-CS110 Computational Engineering

The new code#include <stdio.h>main() { char item[20], item1[20]; int partno, partno1; float cost, cost1; scanf(“%s %d %f”,item,&partno,&cost); scanf(“%s %d %f”,item1,&partno1,&cost1); printf(“%s %d %f\n”,item,partno,cost); printf(“%s %d %f\n”,item1,partno1,cost1);}

Page 11: Lec14-CS110 Computational Engineering

Output

Kama 2 4.5 //InputVasanth 3 5.6 //InputKama 2 4.50000 //OutputVasanth 3 5.6 //Output

Page 12: Lec14-CS110 Computational Engineering

OutputKama 2 4.5re //InputVasanth 3 5.6 //InputKama 2 4.50000 //Outputre <garbage> <garbage> //OutputNow, assume that these two scanf functions

are very far off in the code and no I/Ofunction in between, such problems canhappen.

Page 13: Lec14-CS110 Computational Engineering

What was the problem

• Input a string with blank space - sayTAMIL NADU

• We saw yesterday - two ways– scanf(“%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]”,line);– scanf(“%[^\n]”,line);

Page 14: Lec14-CS110 Computational Engineering

The attractive scanf()

#include <stdio.h> main() { int a, b,c; scanf(“%3d %3d %3d”,&a,&b,&c); }

Page 15: Lec14-CS110 Computational Engineering

I/O

If you enter 1 2 3 then a=1;b=2;c=3; 123 456 789 then a=123;b=456;c=789; 123456789 then a=123;b=456;c=789; 1234 5678 9 then a=123;b=4;c=567;When reading from a file where the

data is already stored in a given format,no other go.

Page 16: Lec14-CS110 Computational Engineering

Reading from file#include <stdio.h> main() { FILE *fpt; /* File/Stream Pointer - points to

next character to be read in a file */ fpt = fopen(“sample.dat”, “r”); //Open for read if (fpt == NULL) printf(“\n Error\n”); else { .. } fclose(fpt); //File has to be closed}

Page 17: Lec14-CS110 Computational Engineering

Read Character by Character#include <stdio.h> main() { FILE *fp; char ch, fname[50]; printf(“Enter the Input File name with extension\n”); gets(fname); if ((fp = fopen(fname,”r”)) == NULL) printf(“\n File I/O error\n”); else { fscanf(fp, “%c”,&ch); while (!feof(fp)) { printf(“%c”,ch); fscanf(fp, “%c”,&ch); } } fclose(fp);}

Page 18: Lec14-CS110 Computational Engineering

The attractive scanf()

#include <stdio.h> main() { int I; float x; char c; scanf(“%3d %5f %c”,&i,&x,&c); }If you enter 10 256.875 T; answer is i = 10;

x = 256.8 and c = ‘7’

Page 19: Lec14-CS110 Computational Engineering

The scanf() prefixes#include<stdio.h> main() { short ix,iy; long lx,ly; double dx,dy; scanf(“%hd %ld %lf”,&ix,&lx,&dx); //short int, long int, double precision scanf(“%3ho %7lx %12le”,&iy,&ly,&dy); } // Short octal 3 characters, long hex and double precision.

Page 20: Lec14-CS110 Computational Engineering

The scanf() prefixes#include<stdio.h> main() { short ix,iy; long lx,ly; double dx,dy; scanf(“%hd %D %lf”,&ix,&lx,&dx); //short int, long int, double precision scanf(“%3ho %7X %12le”,&iy,&ly,&dy); } // Upper case for long, i.e. %ld = %D; %lx = X

Page 21: Lec14-CS110 Computational Engineering

Assignment Suppression

#include <stdio.h> main() {

char item[20]; int partno; float cost; scanf(“ %s %*d %f”, item, &partno, &cost); } // partno will not be updated with the new

values. Again useful in FILE I/O

Page 22: Lec14-CS110 Computational Engineering

No white space in format#include <stdio.h> main() { char c1, c2, c3; scanf(“ %c%c%c”,&c1,&c2,&c3); } Input is a b c, then c1 = a; c2 = blank; c3 = b; Solution is leave blank in format string or enter

without blanks or use scanf(“ %c%1s%1s”, &c1,&c2,&c3);

Page 23: Lec14-CS110 Computational Engineering

Rule

Non-format characters in the character stringare expected to be matched by the samecharacter in the input data.

#include <stdio.h> main() { int j; float x; scanf(“%d p %f”, &j, &x); } // If input is 2 p 4.0, then j = 2 and x = 4.0

Page 24: Lec14-CS110 Computational Engineering

A small detour

• Generating random numbers in C• The following functions in <stdlib.h>

– srand(u); - Initialize– rand(); - returns an int

• No initialization then cannot re-create• Search for solution in huge search space

– Equal probability of getting the required solution.

Page 25: Lec14-CS110 Computational Engineering

Example#include <stdio.h>#include <stdlib.h>main() { unsigned i; int j; i = 567798; srand(i); //Random seed for (j = 1; j <10; j++) printf("%d \n", rand());}

Output is:953046398191777186040959319713477733443690840521262889428180173309561221318302024713Output: Same onevery run

Page 26: Lec14-CS110 Computational Engineering

Change the Seed#include <stdio.h>#include <stdlib.h>main() { unsigned i; int j; i = 14592; srand(i); //Random seed for (j = 1; j <10; j++) printf("%d \n", rand());}

Output is:2452477448577148151702657041133729181229963478211262885910201402062134488241629543352

Page 27: Lec14-CS110 Computational Engineering

No Seed#include <stdio.h>#include <stdlib.h>main() { int j; for (j = 1; j <10; j++) printf("%d \n", rand());}

Output is:168072824752491622650073984943658114410893047021127210102754414578508781458777923// Default sequence//Cannot change//Compiler decides

Page 28: Lec14-CS110 Computational Engineering

Creative Question

• Given a Black box that takes as input alower triangular matrix and an uppertriangular matrix and outputs theproduct of the same, use the black boxto multiple two arbitrary squarematrices.