
Patrick B. answered 04/06/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
typedef struct _TTimeRec
{
int min;
int sec;
long secs;
} * TTimeRec;
#define TIME_REC_SIZE (sizeof(struct _TTimeRec))
typedef struct _TTimeRec TimeRec;
typedef struct _TListNode
{
TimeRec timeRec;
struct _TListNode * next;
} * TListNode;
#define LIST_NODE_SIZE (sizeof(struct _TListNode))
typedef struct _TLinkedList
{
TListNode first;
TListNode last;
int count;
} *TLinkedList;
List_Init(TLinkedList list)
{
list->first = list->last = NULL;
list->count = 0;
}
List_Push(TLinkedList list, TTimeRec timeRec)
{
TListNode newNode = (TListNode) malloc(LIST_NODE_SIZE);
memcpy(&newNode->timeRec,timeRec,TIME_REC_SIZE);
newNode->next = NULL;
if (list->count==0)
{
list->first = list->last = newNode;
}
else
{
list->last->next = newNode;
list->last = list->last->next;
}
list->count++;
}
List_Destroy(TLinkedList list)
{
TListNode tempPtr = list->first;
for (int iLoop=0; iLoop<list->count; iLoop++)
{
list->first = list->first->next;
free(tempPtr);
tempPtr=list->first;
}
list->count=0;
}
TimeRecList2Array(TLinkedList list, TTimeRec timeRecs)
{
TListNode cur= list->first;
for (int iLoop=0; iLoop<list->count; iLoop++)
{
memcpy(&timeRecs[iLoop],&cur->timeRec,TIME_REC_SIZE);
// cout << "transferred " << timeRecs[iLoop].min << ":" << timeRecs[iLoop].sec << endl;
cur=cur->next;
}
}
TimeRecCompare(const void * rec1, const void * rec2)
{
TTimeRec timeRecPtr1 = (TTimeRec)rec1;
TTimeRec timeRecPtr2 = (TTimeRec)rec2;
long timeSecs1 = timeRecPtr1->secs;
long timeSecs2 = timeRecPtr2->secs;
int iReturn=0;
if (timeSecs1 != timeSecs2)
{
iReturn = (timeSecs1 > timeSecs2) ? 1 : -1;
}
return(iReturn);
}
Go()
{
struct _TLinkedList listTimeRecs;
List_Init(&listTimeRecs);
int iMin=-2,iSec=61;
cout << " Input -1 for BOTH minutes and seconds to complete the input....." << endl;
do
{
iMin=-2; iSec=61;
while (iMin<-1)
{
cout << "Please input # of minutes or -1 to quit :>";
cin >> iMin;
if (iMin<-1) { cout << "Invalid minutes ! " << endl; }
}
while ((iSec<-1) || (iSec>59))
{
cout << "Please input # of seconds or -1 to quit :>";
cin >> iSec;
if ((iSec<-1) || (iSec>59)) { cout << "Invalid seconds ! " << endl; }
}
if ((iMin>=0) && (iSec>=0))
{
struct _TTimeRec curTimeRec;
curTimeRec.min = iMin;
curTimeRec.sec = iSec;
curTimeRec.secs = iMin*60+iSec;
List_Push(&listTimeRecs,&curTimeRec);
}
} while ((iMin>=0) && (iSec>=0));
int N=listTimeRecs.count;
TTimeRec timeRecs = (TTimeRec)malloc(TIME_REC_SIZE*N);
TimeRecList2Array(&listTimeRecs,timeRecs);
qsort(timeRecs,N,TIME_REC_SIZE,TimeRecCompare);
long totalSumSecs = 0;
cout << " Min Sec " << endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << timeRecs[iLoop].min << ":" << setfill('0') << setw(2) << timeRecs[iLoop].sec;
totalSumSecs += timeRecs[iLoop].secs;
if (iLoop==0)
{
cout << " <---- FASTEST TIME " << endl;
}
else if (iLoop==N-1)
{
cout << " <---- SLOWEST TIME " << endl;
}
else
{
cout << endl;
}
}
long avgSecs = ceil(totalSumSecs/N);
cout << "-----------------------------------------------" << endl;
cout << " AVERAGE TIME " << avgSecs/60 << ":" << avgSecs%60 << endl;
free(timeRecs);
List_Destroy(&listTimeRecs);
}
int main()
{
Go();
}