Question

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

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

Solutions

Expert Solution

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.


Related Solutions

#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000];...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000]; for(i=0;i<1000;i++) a[i]=1;    for(i=2;i<1000;i++) { if(a[i]==1){ for(j=i+1;j<1000;j++) {if(j%(i)==0) a[j]=0; } } } printf("print numbers are:\n"); for(i=2;i<=1000;i++) if(a[i]==1) printf("%d, ",i); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT