help with parallel array

This is a discussion on help with parallel array within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define NUM_PATIENT 10 #define NUM_DAY 50 void getData(int ID[], int maxHR[], int age[], float aveCommuteHR[], ...

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

    help with parallel array

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define NUM_PATIENT 10
    #define NUM_DAY 50
    void getData(int ID[], 
    			int maxHR[], 
    	        int age[], 
    	        float aveCommuteHR[], 
    	        float maxCommuteHR[], 
    	        float excerciseHr[]);  
    
    int main(void)
    {
    	// Declare variables
    	FILE* spData;
    	int ID[NUM_PATIENT];
    	int maxHR[NUM_PATIENT];
    	int age[NUM_PATIENT];
    	float aveCommuteHR[NUM_DAY];
    	float maxCommuteHR[NUM_DAY];
    	float excerciseHr[NUM_DAY];
    
    	//statements 
    	 getData( ID, maxHR, age, aveCommuteHR, maxCommuteHR, excerciseHr);  
    
    
    	if((spData=fopen("C:\\Users\\khoa\\Documents\\Visual Studio 2010\\Projects\\lab 1\\HR.txt","r"))== NULL)
    	 {
              printf("\n Error opennning file!\n");
              exit(100);
         }
    	else
    	{
    		printf("opened file!\n");
    	}
    	fclose(spData);
    	return 0;
    }
    
    /*=================================getData=================================
    Pre:
    Post:
    */
    
    void getData(int ID[], 
    			int maxHR[], 
    	        int age[], 
    	        float aveCommuteHR[], 
    	        float maxCommuteHR[], 
    	        float excerciseHr[])
    {
    	// local declaration
    	FILE* spData;
    	int i;
    	while(!EOF)
    	{
    		for(i=0; i< NUM_PATIENT; i++)
    		{
    			fscanf(spData,"%d %d %d", &ID[i], &maxHR[i], &age[i]);
    			fprintf(spData,"%d %d %d", ID[i], maxHR[i], age[i]);
    		}
    		for(i=0; i< NUM_DAY; i++)
    		{
    		    fscanf(spData,"%d %d %d", &aveCommuteHR[i], &maxCommuteHR[i], &excerciseHr[i]);
    		}
    
    	}
    
    return;
    }
    can any one help me to check if this code is doing what it suppose to do

    there is a file with data attach below


    The file consists of three columns, with six lines of data for each person. The first line stores each person’s ID number (integer), clinically measured maximum heart rate (integer), and age (integer). The following five lines contain the day’s average commuting heart rate, maximum commuting heart rate, and exercise heart rate for five consecutive working days. Then, the six line sequence is repeated for the next person. At times the heart rate monitors reacted to nearby power lines or other objects, and gave false readings. These incorrect measurements were rejected and show up in the data file as -1. On the days the person did not exercise, the exercise heart rate value is zero.
    PROCESSING; Use precisely six parallel arrays: one for subject numbers and five for the calculated values as described below.
    Using this information, calculate
    1. Average of the average commuting heart rates for each person.
    2. Number of days that the person exercised on his/her own.
    3. Estimated maximum heart rate = 220 – age.
    4. Ratio (%) of measured maximum heart rate to estimated maximum heart rate. The maximum heart rate for each person has been measured during a maximum oxygen uptake test in the laboratory. During the test, the person exercised with increasing intensity until exhaustion, and then the maximum heart rate was measured close to the end of the test. Using this measured maximum heart rate and the estimated maximum heart rate, calculate the percentage of the measured maximum heart rate with respect to the estimated maximum heart rate. Note that this percentage can exceed 100%.
    5. Ratio (%) of highest commuting heart rate to measured maximum heart rate. Find the highest maximum commuting heart rate for each person, and then calculate the percentage of this value with respect to the measured maximum heart rate.

    this is what i was suppose to do :
    Write getdata(). At this stage there is a for loop to read each of six days nested inside a while not end of file type loop. Use debug printf() statements to check that the data is being input correctly. These must be removed before final submission of project. Debug.
    Attached Files Attached Files
    • File Type: txt HR.txt (916 Bytes, 45 views)

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Put all of that data into a struct (get rid of the arrays), and make an array of those structs to pass from function to function. Remember to use dynamic allocation.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    The (silly) requirement for the assignment precludes the use of a struct:
    Use precisely six parallel arrays: one for subject numbers and five for the calculated values as described below.
    Must not have covered them yet.

    can any one help me to check if this code is doing what it suppose to do
    Why don't you compile it and run it and tell us?

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    322
    Quote Originally Posted by rags_to_riches View Post
    Why don't you compile it and run it and tell us?
    Seriously.

    I can tell the OP right now that this program will most definitely not work. HINT: check for uninitialized variables.

    Also, I suggest reading the fscanf manpage on how to check for EOF properly. The way you are doing it is certainly not correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to parallel threads
    By nabi in forum C Programming
    Replies: 0
    Last Post: 09-19-2010, 12:44 PM
  2. Parallel structures
    By tasha302 in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2007, 02:03 PM
  3. Parallel Array Problem
    By Moose112 in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2006, 12:08 PM
  4. Replies: 2
    Last Post: 07-22-2004, 02:25 AM
  5. Parallel port
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-30-2002, 08:11 PM

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