0
Upcoming SlideShare
Loading in...5
×

# Lec14-CS110 Computational Engineering

63

Published on

A keynote on Problem Solving using Computers

Published in: Education
0 Comments
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

No Downloads
Views
Total Views
63
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript of "Lec14-CS110 Computational Engineering"

1. 1. Data I/O Lecture 14 February 7, 2008
2. 2. Data Input and Output • Character I/O – getchar(); putchar(); • String I/O – gets(); puts(); • General I/O – printf(); scanf();
3. 3. getchar() #include <stdio.h> main() { char c; while ((c = getchar()) != EOF) printf("n%cn",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
4. 4. putchar() #include <stdio.h> main() { char c; while ((c = getchar()) != EOF) putchar(c); } //Prints the character on the screen
5. 5. Inputs/Outputs-previous code Aa //Inp Aa //Out B //Inp B //Out Aaa //Inp Aaa /Out
6. 6. 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
7. 7. scanf() %s - String followed by whitespace - null automatically added %u - unsigned decimal %X - hexadecimal [***] - String with whitespace characters
8. 8. 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); }
9. 9. Output  Kama 2 4.5 //Input  Kama 2 4.5000 //Output  Since “n” is a whitespace character  We may enter each input in different lines or two in one line and the remaining in the other line.  What went wrong?  Nothing went wrong in the above toy code  Let us see the code in next slide
10. 10. 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 %fn”,item,partno,cost); printf(“%s %d %fn”,item1,partno1,cost1); }
11. 11. Output Kama 2 4.5 //Input Vasanth 3 5.6 //Input Kama 2 4.50000 //Output Vasanth 3 5.6 //Output
12. 12. Output Kama 2 4.5re //Input Vasanth 3 5.6 //Input Kama 2 4.50000 //Output re <garbage> <garbage> //Output  Now, assume that these two scanf functions are very far off in the code and no I/O function in between, such problems can happen.
13. 13. What was the problem • Input a string with blank space - say TAMIL NADU • We saw yesterday - two ways – scanf(“%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]”,line); – scanf(“%[^n]”,line);
14. 14. The attractive scanf() #include <stdio.h> main() { int a, b,c; scanf(“%3d %3d %3d”,&a,&b,&c); }
15. 15. 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.
16. 16. 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 Errorn”); else { .. } fclose(fpt); //File has to be closed }
17. 17. Read Character by Character #include <stdio.h> main() { FILE *fp; char ch, fname[50]; printf(“Enter the Input File name with extensionn”); gets(fname); if ((fp = fopen(fname,”r”)) == NULL) printf(“n File I/O errorn”); else { fscanf(fp, “%c”,&ch); while (!feof(fp)) { printf(“%c”,ch); fscanf(fp, “%c”,&ch); } } fclose(fp); }
18. 18. 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’
19. 19. 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.
20. 20. 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
21. 21. 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
22. 22. 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);
23. 23. Rule Non-format characters in the character string are expected to be matched by the same character 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
24. 24. 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.
25. 25. 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: 953046398 1917771860 409593197 1347773344 369084052 1262889428 1801733095 61221318 302024713 Output: Same on every run
26. 26. 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: 245247744 857714815 1702657041 1337291812 299634782 112628859 1020140206 2134488241 629543352
27. 27. No Seed #include <stdio.h> #include <stdlib.h> main() { int j; for (j = 1; j <10; j++) printf("%d n", rand()); } Output is: 16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 // Default sequence //Cannot change //Compiler decides
28. 28. Creative Question • Given a Black box that takes as input a lower triangular matrix and an upper triangular matrix and outputs the product of the same, use the black box to multiple two arbitrary square matrices.
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.