Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that there will be an object file named “sub.o\" which implements a funct

ID: 3629516 • Letter: A

Question

Assume that there will be an object file named “sub.o" which implements a function

whose prototype is given in the header file sub.h". The header file sub.h" consists of the following line.

int sub(int n, int *A, int *B);

However, you do not know what this function is doing. For your testing purpose, you may produce such a function, e.g., returning the maximum value among the 2n integers in the two arrays.

You are also given an ASCII file named input.txt". The first line of this file contains an integer

n. There are n additional lines in the file, where the ith additional line contains two integers xi and yi. A sample of "input.txt" is the following.

5

1 2

3 4

5 6

7 8

9 10



You need to write a driver that does the following:



1) Open an input file (for reading) named input.txt". You program should output an error message and stop, if the file does not exist in the current directory.



2) Read the first integer in the file into an integer var named n.

3) Dynamically allocate memory for an array A of size n, and an array B of size n.



4) Read data from the input file into the arrays, so that A[i - 1] = xi and B[i - 1] = yi for

i = 1; 2; …., n.





5) Close the input file.

6) Open an output file (for writing) named output.txt".

7) Write n into the output file (in the first line). Then write A[i] and B[i] in the next line, for

i = 0; 1; : : : ; n - 1.



8) Call the function sub by the command

result = sub(n, A, B);



9) Write the value of result into the output file.

10) Close the output file.

11)Stop.



If the function “sub" returns the maximum among the 2n array elements, the file “output.txt"

corresponding to the sample “input.txt" will be:

5

1 2

3 4

5 6

7 8

9 10

10



Besides writing the source code of the driver, you also need to create a makeFile named “Make-

file". The make¯le should produce an executable file named “proj1". You should submit (in one

tar file) the file MakeFile", “sub.h", and the source code of your driver, which is either “main.c"

or “main.cpp".

Explanation / Answer

/*Header files*/ #include #include #include #include /*Function prototypes*/ int sub(int n,int a[],int b[]); /*Main function*/ void main() { /*file pointers */ FILE *fs,*fd; int i,n,*a,*b,result; clrscr(); /*Opening the file*/ fs = fopen("inputs.txt", "r+"); /*if condition to check file exists or not */ if (fs==NULL) { /*error message */ printf("File not found..."); getch(); exit(0); } else { /*reading the first character of the file */ fscanf(fs,"%d",&n); /* memory allocation to the array variables */ a = (int*)malloc(sizeof(n)); b = (int*)malloc(sizeof(n)); /* Opening the output file*/ fd = fopen("output.txt","w"); /* Placing the first character into the output file */ fprintf(fd,"%d",n); i=0; printf("Data in the file is: "); do { /*reading the integers in two columns*/ fscanf(fs, "%d %d",&a[i],&b[i]); /*Printing on the screen*/ printf(" %d %d", a[i],b[i]); /*Printing on file*/ fprintf(fd, " %d %d",a[i],b[i]); i++;/*Incrementing*/ }while((getc(fs))!=EOF); /*Closing input file*/ fclose(fs); /*Calling sub function*/ result=sub(n,a,b); /*Pprinting maximum number in file*/ fprintf(fd," %d",result); printf(" File created successfully.."); /*Closing output file*/ fclose(fd); }/*End of else*/ getch();/*Pause the system*/ }/*End of main*/ /*Function definition*/ int sub(int n,int a[],int b[]) { int i;/*Loop variable*/ int max=0; int rowmax=0; for(i=0;ib[i])?a[i]:b[i]; /*Finding total maximum*/ if(max