Patrick B. answered 04/20/20
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
typedef struct _TScoreRec
{
int studentNum;
double score;
} *TScoreRec;
#define SCORE_REC_SIZE (sizeof(struct _TScoreRec))
#define MAX_NUM_SCORES (60)
typedef struct _TScoreStats
{
int n;
double meanAvg;
struct _TScoreRec maxScore;
struct _TScoreRec minScore;
} * TScoreStats;
#define SCORE_STATS_SIZE (sizeof(struct _TScoreStats))
/**********************************************************
PASS #1: calculates the total,
meanAvg,
min and max
***************************************************/
int inputScores(TScoreRec A, TScoreStats scoreStats)
{
memset(A,0,SCORE_REC_SIZE*MAX_NUM_SCORES);
//inputs the # of scores
int N=-1;
while (N<0)
{
printf(" How many student scores ???? :>");
scanf("%d",&N);
}
if (N>1)
{
if (N>MAX_NUM_SCORES)
{
N=MAX_NUM_SCORES;
}
scoreStats->n = N;
double maxScore = -1;
double minScore = 99999;
double grandTotalSum=0;
double dblScoreVal;
int iIndexPosMin=-1, iIndexPosMax=-1;
int iLoop=0;
for (iLoop=0; iLoop<N; iLoop++)
{
dblScoreVal = -1;
while (dblScoreVal<0)
{
printf(" Please input the score for student # %d :> ",(iLoop+1));
scanf("%lf",&dblScoreVal);
}
A[iLoop].score = dblScoreVal;
A[iLoop].studentNum = iLoop;
if (dblScoreVal<minScore)
{
iIndexPosMin=iLoop;
minScore = dblScoreVal;
}
if (dblScoreVal>maxScore)
{
iIndexPosMax = iLoop;
maxScore = dblScoreVal;
}
grandTotalSum += dblScoreVal;
} //for loop
scoreStats->n = N;
scoreStats->meanAvg = grandTotalSum/N;
scoreStats->minScore.studentNum = iIndexPosMin;
scoreStats->minScore.score = minScore;
scoreStats->maxScore.studentNum = iIndexPosMax;
scoreStats->maxScore.score = maxScore;
} //N>1
return(N);
}
/*********************************************************
PASS #2: calculates the percent of students who
scored above the meanAvg, as it outputs;
then reports the meanAvg, min, and max
*********************************************************/
void OutputScores(TScoreRec A, TScoreStats scoreStats)
{
int N = scoreStats->n;
int hitCount=0;
int iLoop=0;
double grandTotalSum=0;
printf("****************************************************************************************************\n");
printf(" student rec # score \n");
for (iLoop=0; iLoop<N; iLoop++)
{
printf(" %d %12.6lf \n", A[iLoop].studentNum+1, A[iLoop].score);
grandTotalSum += A[iLoop].score;
if (
(A[iLoop].score) >= (scoreStats->meanAvg)
)
{
hitCount++;
}
}
printf("****************************************************************************************************\n");
printf(" TOTAL: %12.6lf \n",grandTotalSum);
printf(" Mean Avg = %12.6lf \n",scoreStats->meanAvg);
printf(" Min Score is student # %d with a score of %12.6lf \n",scoreStats->minScore.studentNum+1,scoreStats->minScore.score);
printf(" Max Score is student # %d with a score of %12.6lf \n",scoreStats->maxScore.studentNum+1,scoreStats->maxScore.score);
printf(" Percent of Students scoring above MeanAvg : %12.6lf %%\n", hitCount*100.0f/N);
printf("******************************************************************************************************\n");
}
int Go()
{
struct _TScoreRec A[MAX_NUM_SCORES];
struct _TScoreStats scoreStats;
int n = inputScores(A,&scoreStats);
if (n>1)
{
OutputScores(A,&scoreStats);
}
return 0;
}
int main()
{
return(Go());
}