Thread: use FILE, loop, and if-else Program

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

    Question use FILE, loop, and if-else Program

    I know this long to read but please i really need help, I attempt this problem the best i could although I'm a beginner on this stuff, I'm not really sure if what i did makes much sense but i hope you can understand it. I just followed similar examples i have with me.


    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 max(numevents, numtasks, maxnumdays, totalnumdays);
        int min(numevents, numtasks, maxnumdays, totalnumdays);
        
        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;
                   }
                   
        while( fscanf (infp, "%d %d %d %d", &numevents, &numtasks, &maxnumdays, &totalnumdays ) == 4) {
        
        if (numevents > maxnumdays)
                   return numevents;
                   else
                   return maxnumdays;
                   }
        if (numevents < maxnumdays)
                   return numevents;
                   else
                   return maxnumdays;
                   }
                   
        fprintf(outfp, "Total number of days: %d  %d  %d \n", numtasks, maxnumdays, numtasks + maxnumdays); }
        
        
        
        fclose(infp);
        fclose(outfp);
        
        
        return 0;
         
         }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your indenting makes your code look very different from it's logic. Please indent *as if*, you were sane, even if you're not, before you drive us crazy!

    I can see what you did: you sat down at the computer and tried to figure this out ==> which is way wrong.

    If you don't know how to do something, you can't program a computer to do it.

    Sit down with a pencil and paper, and simulate what you're trying to program, by hand, *first*, then afterward, you can get working on the code.

    You should be tallying up the number of tasks for each event, and the number of days for each task. Don't "return" anything, until the tallying is complete. And why compare numevents with maxnumdays? Those are separate quantities entirely.

    Please, edit your posted code so the closing curly brace lies directly under the opening brace, or the first letter of the word that begins that statement:

    Code:
    while(fscanf(//etc.)  {
       //tally up your numtasks, maxnumdays, etc., here
       //add other supporting logic as needed, here
    } //and put the closing brace where it belongs.
    You and everyone else, will find it much easier to spot problems with your code, that way.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    First of all, the number of arguments you are trying to read in from the input file do not match what your example shows --- where does 'totalnumdays' come from in the input list??

    I think a simple approach to use might be using an integer array to hold the values you read in, e.g., cps[10][3] (ten rows to hold three values for each row). If you do'nt know the size to start with, then choose an arbitrarily large number like 50 for the rows.

    After each read of the file, enter the values in the rows, then have a counter to implement the rows and restart the column count.

    Once you've read in the data to the array, then you can go through the array and cull out the information you need to present it in your output file.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    PHP Code:
     FILE *infp, *outfp;
        
    /**************************************************
        int max(numevents, numtasks, maxnumdays, totalnumdays);
        int min(numevents, numtasks, maxnumdays, totalnumdays);
        **************************************************/
        
    if ((infp fopen("critical1.txt""r"))==NULL) {
                  
    printf("input file cannot be opened\n");
                  return -
    1
    i have to ask what it is you are doing here in the section i commented out.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    37
    Quote Originally Posted by cmay View Post
    PHP Code:
     FILE *infp, *outfp;
        
    /**************************************************
        int max(numevents, numtasks, maxnumdays, totalnumdays);
        int min(numevents, numtasks, maxnumdays, totalnumdays);
        **************************************************/
        
    if ((infp fopen("critical1.txt""r"))==NULL) {
                  
    printf("input file cannot be opened\n");
                  return -
    1
    i have to ask what it is you are doing here in the section i commented out.
    to read the max and min numbers of the chart(I know its totally wrong)

    where it says If...It supposed to read the input.txt file( I think)


    Well actually i have very little clue how to do this, but I'm going to ask a tutor and when i have a better idea how to do this I'll come back here. Thanks for your feedback

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    ok.
    i was asking since it looked like a pair of function prototypes and if so you should have placed those above main and also given return types . it could have worked if there was as example int max (int value,int value ); and the calculations you are doing was in functions which should be under main.

    i did not read all the code yet since i have really bad eyesight today so had it been the only syntaxt error in the program it was a simple one to fix.

Popular pages Recent additions subscribe to a feed