Need Help

This is a discussion on Need Help within the C Programming forums, part of the General Programming Boards category; This program can read from Critical1.txt file and display timetable in Output1.txt but I don't know how to make it ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question Need Help

    This program can read from Critical1.txt file and display timetable in Output1.txt but I don't know how to make it to Display the Total Number of Days. How would you do it?


    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


    Write a program to read the critical path information from critical1.txt file and to print a project completion timetable into output1.txt.
    Timetable lists each event number, number of tasks under this event, the maximum number of days for this event, and the total number of days for the project completion.

    For example, your output1.txt will look like as follows based on the above input file:

    Project completion timetable
    ---------------------------------------------------------------
    Event____Num of tasks_____Max num.of days
    ------ ----------------- ----------------------
    1__________3_________________6
    2__________1_________________5
    3__________2_________________4
    4__________3_________________7
    5__________1_________________4
    ---------------------------------------------------------------
    Total number of days to finish the project: 26 days



    Code:
    #include <stdio.h>
    int main()
    {
        FILE *infp, *outfp;
        
        int event, task, numday, prev_event, totald, numtask, maxnumday;
         
        
        if ((infp = fopen("critical1.txt", "r"))==NULL) {
                  printf("input file cannot be opened\n");
                  return -1;
                   }
    
        if ((outfp = fopen("output1.txt", "w"))==NULL){
                   printf("output file cannot be opened\n");
                   return -1;
                   }
          
         prev_event =-1;
         totald = 0 ;         
        while( fscanf (infp, "%d %d %d", &event, &task, &numday) == 3) {
        
         if(prev_event != event){
            if(prev_event != -1){
                 fprintf(outfp, "%d %d %d\n", prev_event, numtask, maxnumday);
                 totald += maxnumday;
            }
            prev_event=event;
            numtask=1;
            maxnumday=numday;
         }  else {
            numtask ++;
            if(maxnumday < numday)
                  maxnumday = numday;    
         }
       }        
                                 
       if(prev_event != -1){
                 fprintf(outfp, "%d %d %d\n", prev_event, numtask, maxnumday);
                 totald += maxnumday;
       }          
       
        ????Printf(outfp, "Total Number of Days:" "%d %d %d\n", prev_event, numtask, maxnumday);????
                             
         
        
        fclose(infp);
        fclose(outfp);
        
        
        return 0;
        }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Write a loop that iterates through each day in an array, adding the the current number to a running total - this could probably be integrated, so it gets done while you're reading in the data.

    Then just output the last number you had for the running total.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He already asked this here: Reading a Input File and outputing desired information using if-else,loop,and if-else

    And we're still not going to write it for you.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21