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)...
#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...
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); }
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT