In: Computer Science
#include <stdio.h>
#include<math.h>
int main (void).
{
int I, j, k, num_elem;
double x[20], y[20],z[20];
FILE*infile, *outfile;
infile = fopen (“c6_3.IN”, “r”);
outfile = fopen (“c6_3.OUT”, “w”);
k = fscanf (infile, “%lf %lf”, &x[0], &y[0]);
fprintf (outfile,”k= %d\n”,k);
fprintf (outfile, “value of EOF = %d\n”, EOF);
i =1;
while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF)
i++;
num_elem =I;
fprintf(outfile,” x[i] y[i] z[i]\n”);
For (j=0; j<num_elem; j++)
{
Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]);
fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);
}
fclose(infile);
fclose(outfile);
return 0;
}
FELS/KFS2143/AUG20
CONFIDENTIAL/7
Input File C6_3.IN
3.0 4.0
6.0 8.0
9.0 12.0
Output File C6_3.OUT
k = 2
Value of EOF = -1
X[i] Y[i] Z[i]
3.0 4.0 5.0
6.0 8.0 10.0
9.0 12.0 15.0
a) Explain the usage of fscanf() to read array data from a file.
Give the example from the program given.
[5 Marks]
b) Does the function fscanf() return a value, that can be used on
the right side of an assignment statement? Justify your answer by
using the program given.
[5 Marks]
c) Explain the EOF and elaborate how it being used in the C
standard library and relate it with the program given.
[10 Marks]
d) Find the errors, if any, in each of these declarations
i) int a[2] [0];
ii) float a23b [99] [77], 1xy [66] [77];
iii) double city [36] [34], town (12) (34);
iv) int a (2,3) = {11,22,33,44};
a.) "fscanf()" function is used to read data from an input stream, this input strem could be anything for example a file or standard input device keyboard. "fscanf()" function is beign used to read formatted data. Here in this provided code, "fscanf()" is being used to read from a file, pointed by a pointer variable "infile". If you use "fscanf()" to read from standard input device, it will work same as "scanf()" function.
b.) Yes, "fscanf()" returns a value of integer type, this returned value is the number of data items read by "fscanf()" function. Here, in the given program line "k = fscanf (infile, “%lf %lf”, &x[0], &y[0]);" is storing this returned value in variable "k", after this statement "k" will be having 2 because fscanf() is reading 2 data items.
c.) EOF stands for end of file, in more general term, it denotes end of an input stream when you hit "Ctrl+d" on console, system takes it as end of input stream from standard input device unlike when you hit "enter", system takes "enter" as end of line, there is a difference between end of line and end of input stream. EOF is also being used as end of file for an input file. Here in this provided program "while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++;" uses this EOF to check end of file. EOF is a predefined macro whose value is -1, getchar() always returns an unsigned integer for characters, a positive number thats why we compare it with -1 to check end of stream because all other characters would give a positive value.
d.) (i) No Error.
(ii) Compile time error, indentifiers can never start with a digit in C.
(iii) Compile time error, array declarations must contain square brackest "[", "]" not paranthesis.
(iv) Compile time error.