lec14-cs110 computational engineering
DESCRIPTION
A keynote on Problem Solving using ComputersTRANSCRIPT
Data I/O
Lecture 14February 7, 2008
Data Input and Output
• Character I/O– getchar(); putchar();
• String I/O– gets(); puts();
• General I/O– printf(); scanf();
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
putchar()#include <stdio.h>main(){ char c; while ((c = getchar()) != EOF) putchar(c);}//Prints the character on the screen
Inputs/Outputs-previous code
Aa //InpAa //OutB //InpB //OutAaa //InpAaa /Out
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
scanf()
%s - String followed by whitespace - nullautomatically added
%u - unsigned decimal %X - hexadecimal [***] - String with whitespace characters
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);}
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
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);}
Output
Kama 2 4.5 //InputVasanth 3 5.6 //InputKama 2 4.50000 //OutputVasanth 3 5.6 //Output
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.
What was the problem
• Input a string with blank space - sayTAMIL NADU
• We saw yesterday - two ways– scanf(“%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]”,line);– scanf(“%[^\n]”,line);
The attractive scanf()
#include <stdio.h> main() { int a, b,c; scanf(“%3d %3d %3d”,&a,&b,&c); }
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.
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}
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);}
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’
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.
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
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
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);
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
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.
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
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
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
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.