The objectives of this project are making sure you 1 can imp

The objectives of this project are making sure you (1) can implement an algorithm on general.asu.edu;

(2) can program in C++; (3) know how to use dynamic memory allocation; (4) know how to read

from and write to a file; (5) know how to write a program in multiple modules; and (6) can use a

simple Makefile.

You should program on general.asu.edu, and use the programming language C++. No other

programming language is accepted. This is an individual project.

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.

1

• 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 andC[i 1] = zifor 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 “Make-

file”. 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”.

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

3

sub2.o :sub2.cpp sub2.h

g++ -c sub2.cpp

clean :

rm *.o

cleanAll :

rm *.o run

4

Solution

//Have dummy files sub1.cpp, sub1.h, sub2.cpp, sub2.h , and define sub function to show it returns the value in present directory , even foreignsub.o should be presnt in present working directory before using make command to produce exe proj1*/

//foreignsub.h

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

//foreignsub.cpp

int sub(int n, int *A, int *B, int *C)
{
int max = A[0];
for(int 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;
}

//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,array B , array C
for(int 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\ \");
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 = 1; i <= n; i++ )
{
fprintf(fp,\"%d %d %d\ \",A[i-1],B[i-1],C[i-1]);
}
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 output2.txt
//first output value of n followed by newline
fprintf(out,\"%d\ \",n);
//output array A
for(int i = 1; i <= n; i++ )
{
fprintf(fp,\"%d %d %d\ \",A[i-1],B[i-1],C[i-1]);
}
  
fprintf(out,\"\ %d\",result);
fclose(out);
//free the memory allocated
for(int i = 0; i <3; i++ )
{
free(A);
free(B);
free(C);
}
return 0;
}

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

to generate object file of foreignsub ie foreignsub.o , type below command

g++ -c foreignsub.cpp

Using make file first make executable proj1

type below line

make proj1 it shows below lines to show it successfully execcuted commans in make file and made exe by name proj1   

g++ -c main.cpp

g++ -c sub1.cpp

g++ -c sub2.cpp

g++ -o proj1 main.o sub1.o sub2.o foreignsub.o

after thet type

./proj1

yoy see output1.txt and output2.txt files

content of output1.txt

5
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15

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

content of output2.txt

5
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15

15

output2.txt contains the return value from function sub ,ie max vale from array elements A, B, C

The objectives of this project are making sure you (1) can implement an algorithm on general.asu.edu; (2) can program in C++; (3) know how to use dynamic memory
The objectives of this project are making sure you (1) can implement an algorithm on general.asu.edu; (2) can program in C++; (3) know how to use dynamic memory
The objectives of this project are making sure you (1) can implement an algorithm on general.asu.edu; (2) can program in C++; (3) know how to use dynamic memory
The objectives of this project are making sure you (1) can implement an algorithm on general.asu.edu; (2) can program in C++; (3) know how to use dynamic memory
The objectives of this project are making sure you (1) can implement an algorithm on general.asu.edu; (2) can program in C++; (3) know how to use dynamic memory

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site