In: Computer Science
You should assume that there will be an object file named “foreignsub.o” which implements a function whose prototype is given in the header file “foreignsub.h”. The header file “foreignsub.h” consists of the following line.
int sub(int n, int *A, int *B, int *C);
However, you do not know what this function does exactly. For your testing purpose, you may produce such a function, e.g., returning the maximum value among the 3n integers in the three 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 three integers xi, yi, and zi. A sample of “input.txt” is the following.
5
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
You need to write a driver that does the following
Open an input file (for reading) named “input.txt”. You program should output an error message and stop, if the file cannot be opened for reading.
Read the first integer in the file into an integer var named n.
Dynamically allocate memory for an array A of size n, an array B of size n, and an array C of size n.
Read data from the input file into the arrays, so that A[i − 1] = xi, B[i − 1] = yi and C[i − 1] = zi for i = 1,2,...,n.
Close the file “input.txt”.
Open an output file (for writing) named “output1.txt”. You program should output an error message and stop, if the file cannot be opened for writing.
Write n into the file “output1.txt” (in the first line), followed by a newline. Then write A[i], B[i] and C[i] in the next line, for i = 0,1,...,n − 1.
Close the file “output1.txt”.
Call the function sub by the command
result = sub(n, A, B, C);
Open an output file (for writing) named “output2.txt”. You program should output an error message and stop, if the file cannot be opened for writing.
Write n into the file “output2.txt” (in the first line), followed by a newline. Then write A[i], B[i] and C[i] in the next line, for i = 0,1,...,n − 1.
Write the value of result into the file “output2.txt”..
Close the file “output2.txt”.
Stop.
Besides writing the source code of the driver, you also need to create a makefile named “Makefile”. The makefile should produce an executable file named “proj1”. You should submit (in one zip file) the file “Makefile”, “foreignsub.h”, and the source code of your driver, which is “main.cpp”. When we grade your work, we will provide the file “foreignsub.o”, and type in “make” to compile
Grading policies:
You should use C++ as the programming language.
(20 pts) Documentation: You should provide sufficient comment about the variables and operations.
(10 pts) Makefile.
(10 pts) File operations.
(10 pts) I/O operations.
(20 pts) Dynamic memory allocation.
(20 pts) Correct calling of the sub function.
(10 pts) Detecting errors.
The following are some helpful hints (google them).
FILE *fp; int *A;
fp = fopen("input.txt", "r"); if (fp == NULL) exit;
fscanf(fp, "%d", &n);
A = (int *) malloc (n * sizeof (int));
fclose(fp);
run :main.o sub1.o sub2.o
g++ -o run main.o sub1.o sub2.o
main.o :main.cpp sub1.h sub2.h
g++ -c main.cpp
sub1.o :sub1.cpp sub1.h
g++ -c sub1.cpp
sub2.o :sub2.cpp sub2.h g++ -c sub2.cpp
clean :
rm *.o
cleanAll : rm *.o run
//first have foreignsub.cpp and foreignsub.h in present working directory and have the functoon definition and //declaration respectively
//foreignsub.c returns max of elelments of array
int sub(int n, int *A, int *B, int *C)
{
int max = A[0],i;
for( i = 0 ; i < n ; i++)
{
if(max<A[i] )
{
max = A[i];
}
if(max<B[i] )
{
max = B[i];
}
if(max<C[i] )
{
max = C[i];
}
}
return max;
}
//foreignsub.h
int sub(int n, int *A, int *B, int *C);
-------------------------------------------
//have all these files without any content for testing
sub1.c sub1.h, sub2.c , sub2.h
-------------------------------------------------------
//main.c
//for exit system call inlcude stdlib.h
#include<stdlib.h>
#include<stdio.h>
#include"foreignsub.h"
int main()
{
FILE *fp;
int *A,*B,*C ,n,result,i;
fp = fopen("input.txt","r");
if( fp == NULL)
{
perror("not able to open input.txt file\n");
exit(1);
}
fscanf(fp,"%d",&n);
A = (int*)malloc(n*sizeof(int));
B = (int*)malloc(n*sizeof(int));
C = (int*)malloc(n*sizeof(int));
//read values from file input.txt into dynamic array A,array B ,
array C
for( i = 1; i <= n; i++ )
{
fscanf(fp,"%d%d%d",&A[i-1],&B[i-1],&C[i-1]);
}
fclose(fp);
//open file output1.txt file
FILE *out;
out = fopen("output1.txt","w");
if( out == NULL)
{
perror("not able to open output1.txt file\n");
exit(1);
}
//call the function sub
result = sub(n,A,B,C);
if( out == NULL)
{
perror("not able to open output1.txt file\n");
exit(1);
}
//write the array values into file output2.txt
//first output value of n followed by newline
fprintf(out,"%d\n",n);
//output array A,B,C
for( i = 1; i <= n; i++ )
{
fprintf(fp,"%d %d %d\n",A[i-1],B[i-1],C[i-1]);
}
// printf("result = %d\n",result);
fprintf(out,"\n%d",result);
fclose(out);
//free the memory allocated
for( i = 0; i <3; i++ )
{
free(A);
free(B);
free(C);
}
return 0;
}
--------------------------
//first compile foreignsub.c and keep ready foreignsub.o
gcc -c foreignsub.c
check if it geerated object file foreignsub.o
----------------------------------------
//Makefile
run:main.o sub1.o sub2.o
gcc -o run foreignsub.o main.o sub1.o sub2.o
main.o:main.c sub1.h sub2.h
gcc -c main.c
sub1.o:sub1.c sub1.h
gcc -c sub1.c
sub2.o:sub2.c sub2.h
gcc -c sub2.c
clean:
rm *.o
cleanAll :
rm *.o run
------------------------------------
then run makefile as below
make run
gcc -c main.c
gcc -c sub1.c
gcc -c sub2.c
gcc -o run foreignsub.o main.o sub1.o sub2.o
-----------------------------------------
Make sure input.txt is presnt before you execute run
./run
check the output1.txt
//output1.txt
5
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
15
-----------------
if u want to clean object file
run
make clean
rm *.o
//if you want to cleall all
make cleanAll
rm *.o run
-----------------------