# Lec14-CS110 Computational Engineering

0 %
100 %
Education

Published on March 3, 2014

Author: SriHarsha1508

Source: slideshare.net

## Description

A keynote on Problem Solving using Computers

Data I/O Lecture 14 February 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%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

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

Inputs/Outputs-previous code Aa //Inp Aa //Out B //Inp B //Out Aaa //Inp Aaa /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 - null automatically 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 //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

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); }

Output Kama 2 4.5 //Input Vasanth 3 5.6 //Input Kama 2 4.50000 //Output Vasanth 3 5.6 //Output

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.

What was the problem • Input a string with blank space - say TAMIL 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 Errorn”); 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 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); }

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 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

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: 953046398 1917771860 409593197 1347773344 369084052 1262889428 1801733095 61221318 302024713 Output: Same on every 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: 245247744 857714815 1702657041 1337291812 299634782 112628859 1020140206 2134488241 629543352

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

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.

 User name: Comment:

## Related presentations

September 25, 2017

#### Best Oracle course training institute ameerpet Hyd...

September 25, 2017

#### In the good English Private Teacher with Brainy Tu...

September 25, 2017

#### Cima F1 Dumps

September 25, 2017

#### Methods by Which Schools Ensure Comprehensive Deve...

September 25, 2017

#### Need Help in Math Coaching at Vienna? Contact Here

September 25, 2017

## Related pages

### Lec15-CS110 Computational Engineering - Education

Lec15-CS110 Computational Engineering Nov 10, 2014 Education sriharsha-p. ... Lec14-CS110 Computational Engineering. Lec21-CS110 Computational Engineering.

### Lec1- CS110 Computational Engineering - Education

Lec1- CS110 Computational Engineering; Lec1- CS110 Computational Engineering Nov 07, 2014 Education ... Lec14-CS110 Computational Engineering. Login or Join.

### Lec12-CS110 Computational Engineering - Education

1. CS110: Arrays in C Lecture 12 V. Kamakoti 4th February 2008. 2. Loops • for (j = 1; j <= 10; j++) – The initial and final conditions are known.

View 5399 Computational Engineering posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn.

### Lec26-CS110 Computational Engineering - Education

2. Pointers and Multidimensional Arrays • A two dimensional array is a collection of CONTIGUOUS one dimensional arrays. • int x[10][20] - can be ...