Question

In: Computer Science

You should assume that there will be an object file named “foreignsub.o” which implements a function...

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

Solutions

Expert Solution

//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

-----------------------


Related Solutions

Invent a recursive function and save the program in a file named " q3c.java” It should...
Invent a recursive function and save the program in a file named " q3c.java” It should not be any of the familiar ones (like fibonacci or binomial coefficients or any of those). Make one up. Make sure it is well-defined (no ambiguity), make sure it isn’t “infinitely recursive”. • Implement it in a Java program and demonstrate it with at least 7 test values. • Add necessary comments to the program to explain it. • In comments, describe your test...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
Assume that there is a class named TestInterface that implements two interfaces A and B. Both...
Assume that there is a class named TestInterface that implements two interfaces A and B. Both the interfaces have a common method with the same signature (example int sampleMethod()). Explain how will the class define this method and how will the compiler identify, to which interface does this method belong to? Will the compiler give an error or will the program execute successfully. Give your code snippet to support your answer.
Write scores of 20 students on a quiz to a file. The file should be named...
Write scores of 20 students on a quiz to a file. The file should be named scores.txt. The scores should be random numbers between 0-10. Next, read the scores from scores.txt and double them and print the scores to screen. c++
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the file are sample inputs and outputs to test your implementation. /* * The price per ticket depends on the number of tickets * purchased, there are 4 ticket pricing tiers. Given the * number of tickets return the total price, formatted to * exactly two decimal places with a leading dollar sign. * Tier 1: *   Minimum number of tickets: 1 *   Price per...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
In Python: Assume a file containing a series of integers is named numbers.txt and exists on...
In Python: Assume a file containing a series of integers is named numbers.txt and exists on the computer's Disk. Write a program that reads al the numbers stored in the file and calculates their total. - create your own text file called numbers.txt and put in a set of 20 numbers (each number should be between 1 and 100). - Each number should be on its own line. - do not assume that the file will always have 20 numbers...
in c++, please provide only a function. the function should be named "tally." it takes no...
in c++, please provide only a function. the function should be named "tally." it takes no parameters but returns an int. the first int should be 0, next being 1, then 2, etc.. will rate if correct. again should only be 1 function.
Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s...
Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT