




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





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
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
integer real number string %nd or %n.mf or %n.ms or %-nd %-n.mf %-n.ms
n : no. of spaces
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)
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
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
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{ ...
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.
GG
GGG¡GG I¡XUI
GG
GGG¡GG I¡XUI
k=
xk = 98,
k=
yk = 18,
k=
x^2 k = 2436,
k=
xkyk = 464
denominator = (
k=
xk)^2 − 4
k=
x^2 k = − 140
m∗^ = (
k=
xk
k=
yk − 4
k=
xkyk)/denominator = 0. 37
b∗^ = (
k=
xk
k=
xkyk −
k=
x^2 k
k=
yk)/denominator = − 4. 6
(a) read data file (b) compute range of altitudes, and parameters(m & b) of linear model (c) print range of altitudes and linear model
FILE fp x, y : data file float sumx, sumy, sumx2, sumxy, denom, m, b : linear model first, last : range integer i, j, k
#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); }