
Brian O.
asked 05/31/21programming in c
program in c(not in c++ or c#), using if and while for the following situation:
In a hospital, patients are registered, for each patient the following are registered: patient code (integer), age, gender, type of consultation (1, 2 or 3) and days of hospitalization. Data entry is complete when enter a product whose code is negative or zero.
It should show through a menu:
1. Number of hospitalized patients
2. Number of patients whose code is 799
3. Average number of days spent in hospital among patients over 55 years of age
4. Older age among patients seen between type 2 and 4 consultations
5. Younger age among patients who have been hospitalized for more than 3 days
6. Exit
1 Expert Answer

Patrick B. answered 05/31/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _TPatient
{
int patientCode;
int age;
int gender;
int consultationType;
int numDays;
} * TPatient;
#define PATIENT_SIZE (sizeof(struct _TPatient))
#define MAX_PATIENTS (20000)
int PatientParse(TPatient patient, char * patientRecBuff)
{
int iReturn=0;
patient->patientCode = atoi(strtok(patientRecBuff,","));
//Data Entry Input is Complete when patient code is ZER0 or negative
if (patient->patientCode>0)
{
patient->age = atoi(strtok(NULL,","));
patient->gender = atoi(strtok(NULL,","));
patient->consultationType = atoi(strtok(NULL,","));
patient->numDays = atoi(strtok(NULL,","));
}
else
{
iReturn = -1;
}
return(iReturn);
}
PatientDump(TPatient patient, char * strMsg)
{
if (strMsg!=NULL)
{
printf("**********************************************\n");
printf(strMsg); printf("\n");
}
printf("**************************************************\n");
printf(" patient code : %d \n",patient->patientCode);
printf(" patient age : %d \n",patient->age);
printf(" patient gender : %d \n",patient->gender);
printf(" patient consultation type : %d \n",patient->consultationType);
printf(" patient # of days : %d \n",patient->numDays);
printf("\n");
}
int PatientRead(TPatient A, int * numPatients)
{
int iReturn=0;
int iIndexPos=0;
FILE * fptr = fopen("E:\\patients.dat","r");
char inbuff[255];
char strMsg[25];
struct _TPatient curPatientRec;
int iReadReturn=0;
if (fptr!=NULL)
{
iIndexPos=0;
while (!feof(fptr) && (iIndexPos<MAX_PATIENTS))
{
fgets(inbuff,255,fptr);
iReadReturn = PatientParse(&curPatientRec,inbuff);
if (iReadReturn==0)
{
memcpy(&A[iIndexPos++],&curPatientRec,PATIENT_SIZE);
sprintf(strMsg,"patient rec # %d",iIndexPos);
PatientDump(&A[iIndexPos-1],strMsg);
}
else
{
break;
}
} //while
*numPatients = iIndexPos;
}
else
{
printf(" Error opening input file \n");
iReturn =-1;
}
return(iReturn);
}
int Menu()
{
int iReturn;
do
{
printf("*******************************\n");
printf(" <1> # of patients \n");
printf(" <2> # with code 799 \n");
printf(" <3> Avg # days over age 55 \n");
printf(" <4> Type 2 and 4 max ages \n");
printf(" <5> Min age over 3 days \n");
printf(" <6> EXIT/QUIT \n");
printf("********************************\n");
printf(" Input selection :>");
scanf("%d",&iReturn);
}
while (iReturn<1 || iReturn>6);
return(iReturn);
}
int Count799(TPatient A, int n)
{
int iCount=0;
int iLoop=0;
for (iLoop=0; iLoop<n; iLoop++)
{
if (A[iLoop].patientCode==799)
{
iCount++;
}
}
return(iCount);
}
double Query3(TPatient A, int n)
{
int iTotalNumDays=0;
int iLoop=0;
int iCount=0;
double avgNumDaysReturn=0;
for (iLoop=0; iLoop<n; iLoop++)
{
if (A[iLoop].age>55)
{
iTotalNumDays += A[iLoop].numDays;
iCount++;
}
}
if (iCount>0)
{
avgNumDaysReturn = iTotalNumDays*1.0f/iCount;
}
return(avgNumDaysReturn);
}
int Query4(TPatient A, int n)
{
int iLoop=0;
int maxAge=-1;
int curAge;
int consultationType;
for (iLoop=0; iLoop<n; iLoop++)
{
consultationType = A[iLoop].consultationType;
curAge = A[iLoop].age;
if (consultationType==2 || consultationType==4)
{
if (curAge>maxAge)
{
maxAge = curAge;
}
}
}
return(maxAge);
}
int Query5(TPatient A, int n)
{
int iLoop=0;
int minAge=32767;
int curAge;
int curNumDays;
for (iLoop=0; iLoop<n; iLoop++)
{
curNumDays = A[iLoop].numDays;
curAge = A[iLoop].age;
if (curNumDays>3)
{
if (curAge<minAge)
{
minAge = curAge;
}
}
}
return(minAge);
}
Go()
{
struct _TPatient A[MAX_PATIENTS];
int NumPatients;
int iMenuChoice=-1;
if (PatientRead(A,&NumPatients)==0)
{
while (iMenuChoice != 6)
{
iMenuChoice = Menu();
switch (iMenuChoice)
{
case 1:
{
printf(" # of patients: %d \n",NumPatients);
break;
}
case 2:
{
printf(" # of patients with code 799: %d \n",Count799(A,NumPatients));
break;
}
case 3:
{
printf(" %8.4lf \n",Query3(A,NumPatients));
break;
}
case 4:
{
printf("%d \n",Query4(A,NumPatients));
break;
}
case 5:
{
printf("%d \n",Query5(A,NumPatients));
break;
}
}
} //while
}
}
main() { Go(); }
/****************************************************************/
/*** INPUT FILE ***********/
333,51,1,3,3
216,51,1,3,3
711,43,0,2,5
469,39,0,1,4
534,69,1,2,3
810,77,0,1,2
159,24,0,3,1
799,12,1,1,25
643,31,0,2,2
928,59,2,4,3
-1
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
It is easier to input the data from file rather than input every time the program begins06/01/21