Patrick B. answered 06/01/19
Math and computer tutor/teacher
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM_RECS (5)
typedef struct _TItem
{
double price;
double amount;
double unit;
} * TItem;
#define ITEM_REC_SIZE (sizeof(struct _TItem))
int Input(TItem * itemList)
{
int recCount=0;
int iLoop=0;
double flAmt;
printf(" How many items ???? :>");
scanf("%d",&recCount);
if (iLoop>MAX_NUM_RECS)
{
iLoop = MAX_NUM_RECS;
}
for (iLoop=0; iLoop<recCount; iLoop++)
{
printf("--------- Item # %d -------------- \n",(iLoop+1));
itemList[iLoop]= (TItem)malloc(ITEM_REC_SIZE);
if (itemList[iLoop]!=NULL)
{
printf(" Please input the price :>");
scanf("%lf",&flAmt);
itemList[iLoop]->price = flAmt;
printf(" Please input the unit :>");
scanf("%lf",&flAmt);
itemList[iLoop]->unit = flAmt;
printf(" Please intput the amount :>");
scanf("%lf",&flAmt);
itemList[iLoop]->amount = flAmt;
}
}
return(recCount);
}
void Output(TItem * itemList, int recCount)
{
int iLoop=0;
double subtotal;
TItem itemRecPtr;
double totalAmt;
double total;
totalAmt = 0;
total = 0;
printf(" PRICE UNIT AMOUNT SUBTOTAL \n");
for (iLoop=0; iLoop<recCount; iLoop++)
{
itemRecPtr = itemList[iLoop];
subtotal = itemRecPtr->amount * itemRecPtr->price * itemRecPtr->unit;
printf(" %lf %lf %lf %lf \n", itemRecPtr->price, itemRecPtr->unit, itemRecPtr->amount, subtotal);
totalAmt += itemRecPtr->amount;
total += subtotal;
}
printf(" TOTALS %lf %lf",totalAmt, total);
}
int main()
{
int iReturn=0;
TItem * dbArrayItem;
dbArrayItem = (TItem*) malloc( sizeof(TItem)*MAX_NUM_RECS);
if (dbArrayItem != NULL)
{
int recCount = Input(dbArrayItem);
Output(dbArrayItem, recCount);
}
else
{
printf(" Memory Allocation Error : no array - sorry \n");
iReturn = -1;
}
return(iReturn);
}