Here is my C++ program, the two files are Main.cpp and a header file Sub.h. Plea
ID: 3634004 • Letter: H
Question
Here is my C++ program, the two files are Main.cpp and a header file Sub.h. Please write the makefile to compile this program on a Linux machine. Please run it before answering. Thanks
this is Main.cpp
/*Header files*/
#include "sub.h"
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
/*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*/
This is Sub.h
#ifndef Sub_H
#define Sub_H
//Function definition
int sub(int n,int a[],int b[])
{
int i;//Loop variable
int max=0;
int rowmax=0;
for(i=0;i<n;i++)
{
//Finding maximum among two array elements
rowmax=(a[i]>b[i])?a[i]:b[i];
//Finding total maximum
if(max<rowmax)
max=rowmax;
}
//Returning max
return max;
}//End of function
Explanation / Answer
# Example Makefile for ArcEngine C++ Programming on Linux # # The PROGRAM macro defines the name of the program or project. It # allows the program name to be changed by editing in only one # location # PROGRAM = basic_sample # # Command line parameters: Edit these parameters so that you can # easily run the sample by typing "make -f Makefile.Linux run". # # You will need to: # (1) Describe parameters here. ex: IN_SHAPEFILE is the input shapefile # (2) Define parameters below this comment box. # ex: IN_SHAPEFILE = /mycomp/data/shapefile.shp # (3) Add the parameters to the run target at the end of this file # ex: ./$(PROGRAM) $(IN_SHAPEFILE) # # # The INCLUDEDIRS macro contains a list of include directories # to pass to the compiler so it can find necessary header files. # # The LIBDIRS macro contains a list of library directories # to pass to the linker so it can find necessary libraries. # # The LIBS macro contains a list of libraries that the the # executable must be linked against. # INCLUDEDIRS = -I$(ARCENGINEHOME)/include -I/usr/X11R6/include LIBDIRS = -L$(ARCENGINEHOME)/bin -L/usr/X11R6/lib LIBS = -larcsdk # # The CXXSOURCES macro contains a list of source files. # # The CXXOBJECTS macro converts the CXXSOURCES macro into a list # of object files. # # The CXXFLAGS macro contains a list of options to be passed to # the compiler. Adding "-g" to this line will cause the compiler # to add debugging information to the executable. # # The CXX macro defines the C++ compiler. # # The LDFLAGS macro contains all of the library and library # directory information to be passed to the linker. # CXXSOURCES = basic_sample.cpp # list of source files CXXOBJECTS = $(CXXSOURCES:.cpp=.o) # expands to list of object files CXXFLAGS = -DESRI_UNIX $(INCLUDEDIRS) CXX = g++ LDFLAGS = $(LIBDIRS) $(LIBS) # # Default target: the first target is the default target. # Just type "make -f Makefile.Linux" to build it. # all: $(PROGRAM) # # Link target: automatically builds its object dependencies before # executing its link command. # $(PROGRAM): $(CXXOBJECTS) $(CXX) -o $@ $(CXXOBJECTS) $(LDFLAGS) # # Object targets: rules that define objects, their dependencies, and # a list of commands for compilation. # basic_sample.o: basic_sample.cpp basic_sample.h $(CXX) $(CXXFLAGS) -c -o basic_sample.o basic_sample.cpp # # Clean target: "make -f Makefile.Linux clean" to remove unwanted objects and executables. # clean: $(RM) -f $(CXXOBJECTS) $(PROGRAM) # # Run target: "make -f Makefile.Linux run" to execute the application # You will need to add $(VARIABLE_NAME) for any command line parameters # that you defined earlier in this file. # run: ./$(PROGRAM)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.