4 Input and Output Functions, Study notes of Computer Science

variable-type specifier meaning character. %c character. %s string integer. %d decimal no. %o octal no. %x hexadecimal no. %u unsigned no. floating-point.

Typology: Study notes

2022/2023

Uploaded on 03/01/2023

amritay
amritay 🇺🇸

4.7

(14)

256 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
4 Input and Output Functions
4.1 printf() and scanf() statements
printf() functions
printf("control string", arguments);
cf. Arguments can be omitted.
eg.
printf("output");
output
printf("%c %d\n", ’A’, 10);
A10
%c, %d - numeric conversion specifier
variable-type specifier meaning
character %c character
%s string
integer %d decimal no.
%o octal no.
%x hexadecimal no.
%u unsigned no.
floating-point %f floating point type
values %e exponent type
%g shorter between %f and %e
cf. qualifiers - short : h, long : l, double : l, long double : L
- The number of conversion specifiers should be the number of outputs.
eg.
printf("%c %d\n", 65, 65);
A65
printf("%d", ‘A’-‘8’+‘5’);
4
printf("%d %o %x", 10, 10, 10);
10, 12, a
printf("%f %e\n", 2.4, 2.4);
2.400000 2.400000e+00
printf("%s", ‘‘string’’);
string
15
pf3
pf4
pf5
pf8

Partial preview of the text

Download 4 Input and Output Functions and more Study notes Computer Science in PDF only on Docsity!

4 Input and Output Functions

4.1 printf() and scanf() statements

  • printf() functions

printf("control string", arguments); cf. Arguments can be omitted.

eg.

printf("output"); output printf("%c %d\n", ’A’, 10); A 10 %c, %d - numeric conversion specifier

variable-type specifier meaning character %c character %s string integer %d decimal no. %o octal no. %x hexadecimal no. %u unsigned no. floating-point %f floating point type values %e exponent type %g shorter between %f and %e

cf. qualifiers - short : h, long : l, double : l, long double : L

  • The number of conversion specifiers should be the number of outputs. eg.

printf("%c %d\n", 65, 65); A 65 printf("%d", ‘A’-‘8’+‘5’); 4 printf("%d %o %x", 10, 10, 10); 10, 12, a printf("%f %e\n", 2.4, 2.4); 2.400000 2.400000e+ printf("%s", ‘‘string’’); string

  • user defined output forms

integer real number string %nd or %n.mf or %n.ms or %-nd %-n.mf %-n.ms

n : no. of spaces

  • : starting from left m(in real number) : no of floating points m(in string) : first m characters

eg.

printf("%d\n", 126); : 126 printf("%10d\n", 126); : ####### printf("%-10d\n", 126); : 126#######

eg.

printf("%f\n", 1234.56); : 1234.56#### printf("%e\n", 1234.56); : 1.23456#e+ printf("%3.1f\n", 1234.56); : 1234. printf("%10.3f\n", 1234.56); : ##1234. printf("%10.3e\n", 1234.56); : #1.235e+

eg.

printf("%2s\n", "string"); : string printf("%10s\n", "string"); : ####string printf("%-10.5s\n", "string"); : strin#####

eg.

FILE *in; in = fopen("test". r"); fclose(in) : fclose(file-pointer)

  • notifying the end of a task to the file
  • Input, Output function related to files.
  • character

char ch; FILE *fp;

ch = getchar(); ch = getc(fp); : read one character in the FILE which is indicated by fp. ch = getc(stdin) : ’stdin’ - keyboard

putchar(ch); putc(ch, fp); : write ’ch’ to the ’fp’ putc(ch, stdout); : ’stdout’ - screen

  • string

char str[100]; FILE *fp;

gets(str); fgets(str, max, fp); : read the first max characters in a string of the FILE indicated by fp and assign to str. puts(str); fputs(str, fp); : write the string to the FILE indicated by fp

  • printf & scanf

scanf("control-string", argument_list); fscanf(fp, "control-string", argument_list); : read from the FILE indicated by fp printf("control-string", argument_list); fprintf(fp, "control-string", argument_list) : write to the FILE indicated by fp

eg.

main() { FILE *fp; int age; fp=fopen("sam", "r"); fscanf(fp, "%d", &age); fclose(fp);

fp=fopen("sam", "w"); fprintf(fp, "sam is %d years old\n", age); fclose(fp) }

eg.

if(fp==NULL) printf("error"); else{ ...

  • Programming Linear Modeling (Regression)

the process that determines the linear equation that is the best fit to a set of data points in terms of minimizing the sum of the squared distances between the line and the data points. For the given sample :

(x 1 , y 1 ), (x 2 , y 2 ),... , (xn, yn)

linear model(estimator) :

ˆy(x) = mx + b

problem : for the given sample and estimator, find m and b which mini- mizes E =

∑^ n

k=

(yi − yˆ(xi))^2.

  • ozone measurements progrmamming
  1. problem statement: for a given data (file),
  • determine a linear model for estimating the ozone mixing ratio at a specified altitude and
  • range of altitudes
  1. I/O diagram

›ŒŸ›

™ˆ•ŽŒG–Gˆ“››œ‹Œš

“•Œˆ™G”–‹Œ“G–™G–¡–•ŒG”Ÿ•ŽG™ˆ›– ‹ˆ›ˆ I¡–•ŒXU‹ˆ›I

›ŒŸ›

™ˆ•ŽŒG–Gˆ“››œ‹Œš

“•Œˆ™G”–‹Œ“G–™G–¡–•ŒG”Ÿ•ŽG™ˆ›– ‹ˆ›ˆ I¡–•ŒXU‹ˆ›I

  1. hand example
    • data: (ppmv: parts per million value) altitude (xk) ppmv (yk) 20 3 24 4 26 5 28 6 ∑^4

k=

xk = 98,

∑^4

k=

yk = 18,

∑^4

k=

x^2 k = 2436,

∑^4

k=

xkyk = 464

denominator = (

∑^4

k=

xk)^2 − 4

∑^4

k=

x^2 k = − 140

m∗^ = (

∑^4

k=

xk

∑^4

k=

yk − 4

∑^4

k=

xkyk)/denominator = 0. 37

b∗^ = (

∑^4

k=

xk

∑^4

k=

xkyk −

∑^4

k=

x^2 k

∑^4

k=

yk)/denominator = − 4. 6

  1. algorithm:

(a) read data file (b) compute range of altitudes, and parameters(m & b) of linear model (c) print range of altitudes and linear model

  • variables

FILE fp x, y : data file float sumx, sumy, sumx2, sumxy, denom, m, b : linear model first, last : range integer i, j, k

  • C program

#include<stdio.h> main() { int i=0; float x, y, sumx=0, sumy=0, sumx2=0, sumxy=0, denom, m, b, first, last; FILE *fp;

/* read data file / fp=fopen("zone1.dat","r"); while((fscanf(fp,"%f %f", &x, &y))==2) { i++; if(i==1) first=x; sumx += x; sumy += y; sumx2 += xx; sumxy += x*y; } last=x; fclose(fp);

/* compute m & b / denom=sumxsumx-isumx2; m=(sumxsumy-isumxy)/denom; b=(sumxsumxy-sumx2*sumy)/denom;

/* print range, m & b */ printf("range of altitudes in km: %.2f to %.2f\n", first, last); printf("linear model: m=%.2f, b=%.2f\n", m, b); }