Help with this programming assignment please? Uses Linux, Files, Dynamic Memory
ID: 3785763 • Letter: H
Question
Help with this programming assignment please? Uses Linux, Files, Dynamic Memory Allocation, Modular Design, Separate Compilation, and Makefile. Use C++.
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 ri, yi, and zi. A sample of "input.txt" is the following. 1 2 3 4 5 6 7 89 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 nExplanation / Answer
---------------------------------------------------------------------------------------------------------------------------
//foreignsub.h
nt sub(int n, int *A, int *B, int *C);
-------------------------------------------------------------------------------------------------------------------------
//main.cpp
#include <iostream>
//for exit system call inlcude stdlib.h
#include<stdlib.h>
#include<stdio.h>
#include"foreignsub.h"
using namespace std;
int main()
{
FILE *fp;
int *A,*B,*C ,n,result;
fp = fopen("input.txt","r");
if( fp == NULL)
{
perror("not able to open input.txt file ");
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
for(int i = 0; i < n; i++ )
{
fscanf(fp,"%d",&A[i]);
}
//read values from file input.txt into dynamic array B
for(int i = 0; i < n; i++ )
{
fscanf(fp,"%d",&B[i]);
}
//read values from file input.txt into dynamic array C
for(int i = 0; i < n; i++ )
{
fscanf(fp,"%d",&C[i]);
}
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 ");
exit(1);
}
//write the array values into file output1.txt
//first output value of n followed by newline
fprintf(out,"%d ",n);
//output array A
for(int i = 0; i < n; i++ )
{
fprintf(fp,"%d ",A[i]);
}
//output Array B
fprintf(out," ");
for(int i = 0; i < n; i++ )
{
fprintf(fp,"%d ",B[i]);
}
//output Array C
fprintf(out," ");
for(int i = 0; i < n; i++ )
{
fprintf(fp,"%d ",C[i]);
}
fclose(out);
//call the function sub
result = sub(n,A,B,C);
//open file output2.txt and output array A,B,C
out = fopen("output2.txt","w");
if( out == NULL)
{
perror("not able to open output1.txt file ");
exit(1);
}
//write the array values into file output1.txt
//first output value of n followed by newline
fprintf(out,"%d ",n);
//output array A
for(int i = 0; i < n; i++ )
{
fprintf(fp,"%d ",A[i]);
}
//output Array B
fprintf(out," ");
for(int i = 0; i < n; i++ )
{
fprintf(fp,"%d ",B[i]);
}
//output Array C
fprintf(out," ");
for(int i = 0; i < n; i++ )
{
fprintf(fp,"%d ",C[i]);
}
//now write result into output2.txt
fprintf(out," %d",result);
fclose(out);
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------
//sub1.cpp,sub2.cpp, sub1.h and sub2.h to just compile using makefile
//sub1.cpp
int sub(int n,int *A, int *B, int *C)
{
return 1;
}
//just create files sub2.cpp,sub1.h,sub2.h and use makefile to make executable proj1
------------------------------------------------------------------------------------------------------------------
Makefile
proj1:main.o sub1.o sub2.o
g++ -o proj1 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 proj1 output1.txt output2.txt
----------------------------------------------------------------------------------------------
at command prompt type
make proj1
it compiles .cpp file and create executable proj1.
to execute type
./proj1
to clean the project use
make clean
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.