I am writing a program which creates a linked list. Each node of the list stores 4 data about, a patient. This part works. After the user decides to output all the data. The average height of the people within the database is to be calculated. I am able to do this within the main function, but I fail to do that with an outside function void avr_height(LIST_HEAD *InpList)
.
My question is what am I doing wrong here? I suspect that, due to my limited understanding of pointers I mess up something about iterating through the list in the outside function. Or pass the list incorrectly to the outside function.
That's the part that calculates the average within the main function
pTemp = DataBase.pFirst_elmt;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum iCurrent;
pTemp = pTemp->next;
iNumb ;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("\n\nThe average height of these patients is: %.2f\n\n", fAvrageH);
and this is my outside function which is supposed to do the same.
void avr_height(LIST_HEAD *InpList)
{
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
NODE *pTemp = NULL;
pTemp = InpList;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum iCurrent;
pTemp = pTemp->next;
iNumb ;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("The average height of these patients is: %f", fAvrageH);
}
To the function void avr_height(LIST_HEAD *InpList)
I pass a struct LIST_HEAD Database
, as an argument. This is done by the call avr_height(&DataBase)
All of the code looks as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 80
//List element's data
typedef struct{
int iID;
int iAge;
int iHeight;
//Defining a character array for storing the name
char sName[STRSIZE];
} PATIENT;
//Defining the list elements (German: KNOTEN)
typedef struct NODE{
//Initialising a Patient of the data type PATIENT. Then pointing to the next element
PATIENT Patient;
struct NODE *next;
} NODE;
//Defining the list element's head
typedef struct{
NODE *pFirst_elmt;
int iNumber;
}LIST_HEAD;
//Function for creating the actual list
void add_patient(LIST_HEAD *List)
{
//Allocating memory for list elements
NODE *pTemp;
pTemp = (NODE*) malloc(sizeof(NODE));
//Creating new elements of the linked list (and checking input validity)
printf("\nEnter patient ID:");
if(scanf("%d", &pTemp->Patient.iID) == 0){
printf("Invalid input...");
}
printf("Enter patient's name:");
if(scanf("%s", pTemp->Patient.sName) == 0){
printf("Invalid input...");
}
printf("Enter patient age:");
if(scanf("%d", &pTemp->Patient.iAge) == 0){
printf("Invalid input...");
}
printf("Enter patient height:");
if(scanf("%d", &pTemp->Patient.iHeight) == 0){
printf("Invalid input...");
}
//Setting the first element of the list AS the the next element, in front of the new element
pTemp -> next = List -> pFirst_elmt;
//Designating the new element of the list as the first element of the list. Increase the number of elements in the list
List -> pFirst_elmt = pTemp;
List -> iNumber ;
printf("\n");
}
//Calculating the average height of the patients: i.e. ITTERATING OVER A LINKED LIST
void avr_height(LIST_HEAD *InpList)
{
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
NODE *pTemp = NULL;
pTemp = InpList;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum iCurrent;
pTemp = pTemp->next;
iNumb ;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("The average height of these patients is: %f", fAvrageH);
}
int main()
{
//Variable initialisation
int i = 0;
LIST_HEAD DataBase;
DataBase.pFirst_elmt = NULL;
DataBase.iNumber = 0;
NODE *pTemp = NULL;
//Variables for the height calculation
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
//Loop for creating list elements if specified by user:
do
{
printf("[0] Output the stored data [1] Enter data\n");
printf("What do you wish to do?\n");
//Checking input validity
if(scanf("%i", &i) == 0){
printf("\nInvalid input...");
}
if(i == 1){
add_patient(&DataBase);
}
} while(i);
pTemp = DataBase.pFirst_elmt;
printf("\n\n");
//Outputting the linked list: i.e. ITTERATING OVER A LINKED LIST
while(pTemp != NULL)
{
printf("\nPatient ID: %d Patient name: %s Patient age: %d Patient height: %d", pTemp->Patient.iID, pTemp->Patient.sName, pTemp->Patient.iAge, pTemp->Patient.iHeight);
pTemp = pTemp->next;
}
//Calculating the average height
pTemp = DataBase.pFirst_elmt;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum iCurrent;
pTemp = pTemp->next;
iNumb ;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("\n\nThe average height of these patients is: %.2f\n\n", fAvrageH);
//Alternative way of calculating the height with a linked list
avr_height(&DataBase);
printf("\n\n");
return 0;
}
All help is appreciated, thanks in advance!
CodePudding user response:
The compiler should issue a message for the function
void avr_height(LIST_HEAD *InpList)
{
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
NODE *pTemp = NULL;
pTemp = InpList;
//...
because the pointers pTemp and InpList have different types.
You need to write
pTemp = InpList->pFirst_elmt;
Also this variable is redundant
int iNumb = 0;
because the list already has the data member int iNumber;
.