Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Files-loops-if-else-command line arguments Background information about Critical

ID: 3531545 • Letter: F

Question

Files-loops-if-else-command line arguments
Background information about Critical Path Analysis. A critical path analysis is a technique used to determine the time schedule for a project. This information is important in the planning stages before a project is begun, and it is also useful to evaluate the progress of a project that is partially completed. One method for this analysis starts by dividing a project into sequential events and then dividing each event into various tasks. Although one event must be completed before the next one is started, various tasks within an event can occur simultaneously. The time it takes to complete an event, therefore, depends on the number of days required to finish its longest task. Similarly, the total time it takes to finish a project is the sum of time it takes to finish each event.
Assume that the critical path information for a major construction project has been stored in a data file. Each line of the data file contains an event number, a task number, and the number of days required to complete the task. The data have been stored such that all the task data for the first event are followed by all the task data for the second event, and so on. A typical set of data is shown in the following table.
Event Task Number of Days
1 15 3
1 27 6
1 36 4
2 15 5
3 18 4
3 26 1
4 15 2
4 26 7
4 27 7
5 16 4
You can simply download critical1.txt from the web page or simply open a file critical1.txt and copy/paste just the above numbers into that file.
Programming problem:
Write a program that gets two file names as command line arguments after -input and -output options, for example
main212> myprog

Explanation / Answer

#include<stdio.h>

#include<string.h>

int main(int argc, char * argv[]){

if(argc<5||argc>5){

printf("Provide the input and output file name in specified format ");

printf("progName -input <InputFileName> -output <OutputFileName> ");

printf("OR ");

printf("progName -output <OutputFileName> -input <InputFileName> ");

}else{

char *inputfilename,*outputfilename;

if(!(strcmp(argv[1],"-input")||strcmp(argv[3],"-output"))){

inputfilename=argv[2];

outputfilename=argv[4];

}else if(!(strcmp(argv[3],"-input")||strcmp(argv[1],"-output"))){

inputfilename=argv[4];

outputfilename=argv[2];

}else{

printf("Provide the input and output file name in specified format ");

printf("progName -input <InputFileName> -output <OutputFileName> ");

printf("OR ");

printf("progName -output <OutputFileName> -input <InputFileName> ");

return 0;

}

FILE *fi,*fo;

int eNo,tNo,nDays;

int currEventNo,maxNoDays;

int totDays=0;

fi=fopen(inputfilename,"r");

fo=fopen(outputfilename,"w");

if(fi==NULL){

printf("Error in opening input file: file not found ");

return 0;

}

if(fo==NULL){

printf("Error in creating output file ");

return 0;

}

fscanf(fi,"%d %d %d",&eNo,&tNo,&nDays);

if(!feof(fi)){

currEventNo=eNo;

maxNoDays=nDays;

}

while(!feof(fi)){

if(currEventNo!=eNo){

currEventNo=eNo;

totDays+=maxNoDays;

maxNoDays=nDays;

}else if(maxNoDays<nDays){

maxNoDays=nDays;

}

fscanf(fi,"%d %d %d",&eNo,&tNo,&nDays);

}

totDays+=maxNoDays;

fprintf(fo,"Number of days for project completion = %d ",totDays);

fclose(fi);

fclose(fo);

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote