SlideShare a Scribd company logo
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.

More Related Content

What's hot

Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
Muthuganesh S
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
Dr. Loganathan R
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Sabik T S
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
Abhishek Sinha
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
Guru Janbheshver University, Hisar
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
Dr. Loganathan R
 
Simple c program
Simple c programSimple c program
Simple c program
Ravi Singh
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
Rumman Ansari
 
C fundamentals
C fundamentalsC fundamentals
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
Dr. Loganathan R
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
Input and output in c
Input and output in cInput and output in c
Input and output in c
Rachana Joshi
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
argusacademy
 
Computer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlabComputer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlab
Shankar Gangaju
 
Compiler design lab
Compiler design labCompiler design lab
Compiler design lab
ilias ahmed
 
ภาษาซีพื้นฐาน
ภาษาซีพื้นฐานภาษาซีพื้นฐาน
ภาษาซีพื้นฐาน
Krunee Thitthamon
 

What's hot (20)

Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
 
Simple c program
Simple c programSimple c program
Simple c program
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Input and output in c
Input and output in cInput and output in c
Input and output in c
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
Computer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlabComputer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlab
 
Compiler design lab
Compiler design labCompiler design lab
Compiler design lab
 
ภาษาซีพื้นฐาน
ภาษาซีพื้นฐานภาษาซีพื้นฐาน
ภาษาซีพื้นฐาน
 

Similar to Lec14-CS110 Computational Engineering

I o functions
I o functionsI o functions
I o functions
Dr.Sandhiya Ravi
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
JAYA
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
JavvajiVenkat
 
Tharun prakash.pptx
Tharun prakash.pptxTharun prakash.pptx
Tharun prakash.pptx
TharunPrakashD
 
Input And Output
 Input And Output Input And Output
Input And Output
Ghaffar Khan
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdf
mhande899
 
2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt
swateerawat06
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
Pradipta Mishra
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
Pradipta Mishra
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
yogini sharma
 
C language
C languageC language
C language
Priya698357
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
comp2
comp2comp2
comp2
franzneri
 
C Programming lab
C Programming labC Programming lab
C Programming lab
Vikram Nandini
 
C
CC
C basics
C basicsC basics
C basics
MSc CST
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
alish sha
 
Technical quiz 5#.pptx
Technical quiz 5#.pptxTechnical quiz 5#.pptx
Technical quiz 5#.pptx
kamalPatelposhala
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
Srichandan Sobhanayak
 
Muzzammilrashid
MuzzammilrashidMuzzammilrashid
Muzzammilrashid
muzzammilrashid
 

Similar to Lec14-CS110 Computational Engineering (20)

I o functions
I o functionsI o functions
I o functions
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
Tharun prakash.pptx
Tharun prakash.pptxTharun prakash.pptx
Tharun prakash.pptx
 
Input And Output
 Input And Output Input And Output
Input And Output
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdf
 
2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
C language
C languageC language
C language
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
comp2
comp2comp2
comp2
 
C Programming lab
C Programming labC Programming lab
C Programming lab
 
C
CC
C
 
C basics
C basicsC basics
C basics
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
 
Technical quiz 5#.pptx
Technical quiz 5#.pptxTechnical quiz 5#.pptx
Technical quiz 5#.pptx
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
Muzzammilrashid
MuzzammilrashidMuzzammilrashid
Muzzammilrashid
 

More from Sri Harsha Pamu

Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec13
Lec13Lec13
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational Engineering
Sri Harsha Pamu
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational Engineering
Sri Harsha Pamu
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
Sri Harsha Pamu
 
Android..imp google
Android..imp googleAndroid..imp google
Android..imp google
Sri Harsha Pamu
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability study
Sri Harsha Pamu
 

More from Sri Harsha Pamu (20)

Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational Engineering
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational Engineering
 
Lec13
Lec13Lec13
Lec13
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational Engineering
 
Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational Engineering
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational Engineering
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational Engineering
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational Engineering
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational Engineering
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational Engineering
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational Engineering
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational Engineering
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
 
Android..imp google
Android..imp googleAndroid..imp google
Android..imp google
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability study
 

Recently uploaded

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 

Recently uploaded (20)

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 

Lec14-CS110 Computational Engineering

  • 2. Data Input and Output • Character I/O – getchar(); putchar(); • String I/O – gets(); puts(); • General I/O – printf(); scanf();
  • 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. putchar() #include <stdio.h> main() { char c; while ((c = getchar()) != EOF) putchar(c); } //Prints the character on the screen
  • 5. Inputs/Outputs-previous code Aa //Inp Aa //Out B //Inp B //Out Aaa //Inp Aaa /Out
  • 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. scanf() %s - String followed by whitespace - null automatically added %u - unsigned decimal %X - hexadecimal [***] - String with whitespace characters
  • 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. 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. 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. Output Kama 2 4.5 //Input Vasanth 3 5.6 //Input Kama 2 4.50000 //Output Vasanth 3 5.6 //Output
  • 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. 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. The attractive scanf() #include <stdio.h> main() { int a, b,c; scanf(“%3d %3d %3d”,&a,&b,&c); }
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.