Thread: help finding mistake !

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    help finding mistake !

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define NUM_PATIENT 100
    #define GIVEN_HR    220
    #define LEN         6
     
    void getdata(FILE*spData, int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int* patient);
    void CalcestHRandRatioEstHR( int num, int age, int EstHR[], int MaxHR, float ratioEstHR[]);
    void SortData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient);
    void printHeader();
    void printData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient);
     
    int main (void)
     
    {
            // Global declarations
            FILE* spData;
            int   ID[NUM_PATIENT];
            float AveOfAveHR[NUM_PATIENT];
            int   dayExcrsHR[NUM_PATIENT];
            int   EstHR[NUM_PATIENT];
            float ratioEstHR[NUM_PATIENT];
            float ratioMaxHR[NUM_PATIENT];
            int   num;
            int   patient;
     
     
        // Statements
            if((spData=fopen("HR.txt","r"))== NULL)
         {
              printf("\n Error opennning file!\n");
              exit(100);
         }
        else
        {
            printf("opened file!Data processed!\n");
        }
     
            getdata(spData, ID, AveOfAveHR, dayExcrsHR, EstHR, ratioEstHR, ratioMaxHR, &patient);
            SortData(ID, AveOfAveHR, dayExcrsHR, EstHR, ratioEstHR, ratioMaxHR, patient);
            printHeader();
            printData(ID, AveOfAveHR, dayExcrsHR, EstHR, ratioEstHR, ratioMaxHR, patient);
     
        fclose(spData);
     
            system("PAUSE");
            return 0;
    }
     
    /*======================== getData =======================
    Pre:
    Post:
    */
    void getdata(FILE* spData, int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int* patient)
    {
            // Local Declarations
            int   MaxHR;  
            int   age;
            float aveCommuHR;
            int   maxCommuHr;
            float excersHR;
            int   i =0;
            int   j;
            int size = LEN;
            int   countave = 0;
            float sumAveCommuHR = 0;
            int   countDayExcers = 0;
            int   HighestMaxCommuHR = 0;
        int   count = 0;
            // Statements
            while(i < NUM_PATIENT && (fscanf(spData,"%d %d %d", &ID[i], &MaxHR, &age)) != EOF)
            {
                    for(j = 0; j < size; j++)
                    {
                            fscanf(spData,"%f %d %f", &aveCommuHR, &maxCommuHr, &excersHR);
     
                            if(aveCommuHR > 0)
                                    countave++; //  count number of average commute HR 
                                    sumAveCommuHR += aveCommuHR; // add sum of average commuting HR of days
                            if(excersHR > 0)
                                    countDayExcers++; // count the day that patient excercise
                            if(maxCommuHr > HighestMaxCommuHR)
                                    HighestMaxCommuHR = maxCommuHr; // determine the highest value of Max commuting HR
            }
                    i++;
                countave = 0;
                sumAveCommuHR = 0;
            countDayExcers = 0;
                HighestMaxCommuHR = 0;
                    count++;
     
                    AveOfAveHR[i] = sumAveCommuHR / countave;
                    dayExcrsHR[i] = countDayExcers;
                    CalcestHRandRatioEstHR(i, age, EstHR, MaxHR, ratioEstHR);
                    ratioMaxHR[i] = (float)HighestMaxCommuHR/ (float)EstHR[i] * 100;
     
            }
     
     
            *patient = count;
            return;
    }
     
     
    /*======================= CalcestHRandRatioEstHR ===========================
    Pre:
    Post:
    */
     
    void CalcestHRandRatioEstHR( int num, int age, int EstHR[], int MaxHR, float ratioEstHR[])
    {
               // Statements
               EstHR[num] = GIVEN_HR - age;
               ratioEstHR[num] = ((float)MaxHR / (float)EstHR[num]) * 100;
     
     
    return;
    }
     
    /* ====================== SortData ==========================================
    Pre:
    Post:
    */
    void SortData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient)
    {
     
            // Local Declerations
        float tempOfFloat;
        int   tempOfInt;
        int current;
        int smallest;
        int walk;
            for(current = 0; current < NUM_PATIENT; current++)
              {
                 smallest = current;
                     // one sort pass each loop 
                     for( walk = current +1; walk < patient; walk++)
                             if(ID[walk] < ID[smallest])
                                    smallest = walk;
                     //swap value of ID
                     tempOfInt    = ID[current];
                     ID[current]  = ID[smallest];
                 ID[smallest] = tempOfInt;
                
                     // swap value of AveOfAveHR
     
                     tempOfFloat          = AveOfAveHR[current];
                     AveOfAveHR[current]  = AveOfAveHR[smallest];
                 AveOfAveHR[smallest] = tempOfFloat;
     
                     //swap value of dayExcrsHR
                     tempOfInt    = dayExcrsHR[current];
                     dayExcrsHR[current]  = dayExcrsHR[smallest];
                 dayExcrsHR[smallest] = tempOfInt;
     
                     //swap value of estimate heart rate
                     tempOfInt    = EstHR[current];
                     EstHR[current]  = EstHR[smallest];
                 EstHR[smallest] = tempOfInt;
     
                     //swap value of ratioEstHR
                     tempOfFloat          = ratioEstHR[current];
                     ratioEstHR[current]  = ratioEstHR[smallest];
                 ratioEstHR[smallest] = tempOfFloat;
     
                     //swap value of ratioMaxHR
             tempOfFloat          = ratioMaxHR[current];
             ratioMaxHR[current]  = ratioMaxHR[smallest];
                 ratioMaxHR[smallest] = tempOfFloat;
     
         }
     
    return;
    }
     
     
    /*======================= printHeader ==========================
    Pre:
    Post:
    */
    void printHeader()
    {
    //Local Declaration
            FILE *spOUT;
     
    //Statements
            if(!(spOUT = fopen("output.txt", "w")))
                    printf("Error writing output.txt");
     
            fprintf(spOUT, "COMMUTING AND EXERCISE HEART RATE SUMMARY\n\n");
            fprintf(spOUT, "SUBJECT   AVERAGE   DAYS      ESTIMATED     %%MEASURED    %%MAX\n");
            fprintf(spOUT, "NUMBER    COMMUTING EXERCISED  MAX HR       MAX HR TO    COMMUTING\n");
            fprintf(spOUT, "          HR                                ESTIMATED    HR TO\n");
            fprintf(spOUT, "                                            HR           MEASURED\n");
     
            fclose(spOUT);
            return;
    }
     
     
    /*======================= printData ==========================
    Pre:
    Post:
    */
    void printData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient)
    {
            //Local Declaration
            FILE *spOUT;
            int i;
     
            //Statements    
            if(!(spOUT = fopen("output.txt", "a")))
                    printf("Error writing output.txt");
            for(i = 0; i < patient; i++)
     
                    {
                            fprintf(spOUT ,"%4d      %5.1f        %d         %3d          %5.1f       %5.1f\n",
                                       ID[i], AveOfAveHR[i], dayExcrsHR[i], EstHR[i], ratioEstHR[i], ratioMaxHR[i]);
                    }
     
            fclose(spOUT);  
     
            return;
    }

    i finish this code but the output was wrong can anyone help me me to find the mistake here as you can see there too many parameters that i cant even find the mistake
    ill be attaching the input file my output file ad the right output file
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Don't use system("pause"), there's much better ways of pausing your program > SourceForge.net: Pause console - cpwiki

    Also, your program doesn't print any information upon failure, seriously, it just says if the file opened right and, if it fails, nothing else until the very end where it complains about PAUSE being invalid. Add a bunch of printf() calls where you think the error is, and/or use GDB (or another debugger) to debug it.

    While you're at it, clean up all the non-standard/clunky/repetitive code.

    Code:
            // Local Declerations
    	float tempOfFloat;
    	int   tempOfInt;
    
    /* 
    * for example, stupid variable names, unnecessary comment, and bad usage 
    * instead, use a macro to swap any two types, and then you don't need temps
    */
    Code:
            int   ID[NUM_PATIENT];
            float AveOfAveHR[NUM_PATIENT];
            int   dayExcrsHR[NUM_PATIENT];
            int   EstHR[NUM_PATIENT];
            float ratioEstHR[NUM_PATIENT];
            float ratioMaxHR[NUM_PATIENT];
    
    /* i feel a struct coming on... */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 04:41 PM
  2. What is my mistake ?
    By Freelander1983 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2007, 09:31 AM