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); }
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...
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...
#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...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT