alvinleephd
04-20-2006, 12:42 AM
i had to make an array project a few weeks ago, and now my professor told me to revise the assignment to make it a function. not really sure how to do this..
exact text from the project sheet:
Rewrite your program from project 3 to meet the following requirements. Your program will compute and display various statistics for an array of up to 30 values of type double. You shall input an array with the following values as one of your test cases (use -1.0 as a sentinel value):
73.3 83.4 58.0 45.7 25.1 69.0 11.9 95.0 44.4
You shall also input another array from p4.dat as another test case (can use redirection as discussed in class).
73.3 99.0 83.4 58.0 25.1 69.0 1.9 95.0 74.8 55.0
43.5 47.4 95.6 58.9 52.6 37.7 93.8 19.1 23.9 77.2
48.5 67.9 92.9 99.9 67.6 12.7 44.8 1.1 29.2 17.2
-1.0
Your program shall display the original array (max 6 values per line), sorted array in descending order (max 6 values per line), minimum, maximum, average, median, and standard deviation in a readable format using one digit after the decimal place. Write down the pseudocode before attempting your code on the computer. You must properly utilize functions for this project and the following functions must be incorporated in your program (add more functions as needed). In general, you should not print in a Math-like function; just return a value. Be sure to use the same print function to print both the original array and the sorted array. DO NOT use global variables in your program.
* Display instructions and some useful information about your program
* Input a list of values into an array
* Print a list of values in an array (max 6 values per line)
* Sort a list of values in descending order
* Compute various statistics (one or more functions)
here is the code for my previous project:
#include <stdio.h>
#include <math.h>
/* onstant with max. array elements */
#define MAX_ELEMENTS 30
/* struct for each array element */
struct tArrayData {
unsigned int StudentID;
double Score;
char Grade;
};
/* struct with wanted data */
struct tWantedData {
unsigned int NumScores;
double MaxScore;
double Average;
double Median;
double StdDev;
unsigned int NumPassed;
};
void main()
{
/* two arrays, one with original data and one with ordered data */
struct tArrayData DataArray[MAX_ELEMENTS];
struct tArrayData OrderedArray[MAX_ELEMENTS];
/* wanted data */
struct tWantedData WantedData;
/* score readed from stdin and sum of scores */
double ScoreReaded = 0.0, ScoreSum = 0.0;
/* loop variables */
int i, j;
/* ordering variables */
double OrderingMax;
int OrderingMaxIndex;
/* initialize some wanted data */
WantedData.NumScores = 0;
WantedData.MaxScore = 0.0;
WantedData.NumPassed = 0;
WantedData.StdDev = 0.0;
/* get score values */
printf("Enter score value (-1 to quit): " );
scanf("%lf", &ScoreReaded);
while( (ScoreReaded != -1) && (WantedData.NumScores < 30) )
{
if ( (ScoreReaded < 0) || (ScoreReaded > 100) )
{
printf("Are you sure?? %0.1f is either too low or too high!\n", ScoreReaded);
}
else
{
/* save score */
DataArray[WantedData.NumScores].Score = ScoreReaded;
/* student ID */
DataArray[WantedData.NumScores].StudentID = WantedData.NumScores+1;
/* calculate grade */
if ( ScoreReaded >= 90.0 )
DataArray[WantedData.NumScores].Grade = 'A';
else if (ScoreReaded >= 80.0)
DataArray[WantedData.NumScores].Grade = 'B';
else if (ScoreReaded >= 70.0)
DataArray[WantedData.NumScores].Grade = 'C';
else if (ScoreReaded >= 60.0)
DataArray[WantedData.NumScores].Grade = 'D';
else if (ScoreReaded < 60.0)
DataArray[WantedData.NumScores].Grade = 'F';
WantedData.NumScores++;
ScoreSum += ScoreReaded;
if (ScoreReaded > WantedData.MaxScore)
{
WantedData.MaxScore = ScoreReaded;
}
if (ScoreReaded >= 70.0)
{
WantedData.NumPassed++;
}
}
/* asks for another score */
printf("Enter score value (-1 to quit): ");
scanf("%lf", &ScoreReaded);
}
/* prints original data */
printf("\nOriginal data:\n");
for(i=1; i<=WantedData.NumScores; i++)
{
printf("%0.1f ", DataArray[i-1].Score);
/* keeps 8 scores per line */
if ( (i % 8) == 0 )
printf("\n");}
/* order array (descending) */
for(i=0; i<WantedData.NumScores; i++)
{
OrderingMax = -1.0;
for(j=0; j<WantedData.NumScores; j++)
{
/* find max. value from original array */
if (DataArray[j].Score > OrderingMax)
{
OrderingMax = DataArray[j].Score;
/* keep index for later */
OrderingMaxIndex = j;
}
}
/* save last max. score */
OrderedArray[i] = DataArray[OrderingMaxIndex];
/* delete for next loop */
DataArray[OrderingMaxIndex].Score = -1.0;
}
/* calculate average */
WantedData.Average = ScoreSum / WantedData.NumScores;
/* calculate standard deviation */
for(i=0; i<WantedData.NumScores; i++)
{
WantedData.StdDev += ((OrderedArray[i].Score - WantedData.Average)*(OrderedArray[i].Score - WantedData.Average));
}
WantedData.StdDev = WantedData.StdDev / (double)WantedData.NumScores;
WantedData.StdDev = sqrt( WantedData.StdDev );
/* calculate median */
if (WantedData.NumScores % 2)
{
/* odd number of values */
WantedData.Median = OrderedArray[(WantedData.NumScores/2)].Score;
}
else
{
/* even number of values */
WantedData.Median = (OrderedArray[WantedData.NumScores/2].Score +
OrderedArray[(WantedData.NumScores/2)-1].Score) / 2.0;
}
/* print ordered array */
printf("\nOrdered data:\n");
for(i=1; i<=WantedData.NumScores; i++)
{
printf("%0.1f ", OrderedArray[i-1].Score);
/* keep 8 scores per line */
if ( (i % 8) == 0 )
printf("\n");
}
printf("\nNumber of test scores: %d\n", WantedData.NumScores);
printf("\n");
printf("Highest score: %0.1f\n", WantedData.MaxScore);
printf("\n");
printf("Average score: %0.1f\n", WantedData.Average);
printf("\n");
printf("Median score: %0.1f\n", WantedData.Median);
printf("\n");
printf("Standard deviation: %0.1f\n", WantedData.StdDev);
printf("\n");
printf("Percent of the students that passed the class: %0.1f\n", ((double)WantedData.NumPassed / (double)WantedData.NumScores)*100.0 );
printf("\n");
printf("Id\tScore\tGrade\n");
for(i=0; i<WantedData.NumScores; i++)
{
printf("%d\t%0.1f\t%c\n", OrderedArray[i].StudentID, OrderedArray[i].Score, OrderedArray[i].Grade);
}
return 0;
}
any help (in pseudocode of course) would be appreciated, thanks!
exact text from the project sheet:
Rewrite your program from project 3 to meet the following requirements. Your program will compute and display various statistics for an array of up to 30 values of type double. You shall input an array with the following values as one of your test cases (use -1.0 as a sentinel value):
73.3 83.4 58.0 45.7 25.1 69.0 11.9 95.0 44.4
You shall also input another array from p4.dat as another test case (can use redirection as discussed in class).
73.3 99.0 83.4 58.0 25.1 69.0 1.9 95.0 74.8 55.0
43.5 47.4 95.6 58.9 52.6 37.7 93.8 19.1 23.9 77.2
48.5 67.9 92.9 99.9 67.6 12.7 44.8 1.1 29.2 17.2
-1.0
Your program shall display the original array (max 6 values per line), sorted array in descending order (max 6 values per line), minimum, maximum, average, median, and standard deviation in a readable format using one digit after the decimal place. Write down the pseudocode before attempting your code on the computer. You must properly utilize functions for this project and the following functions must be incorporated in your program (add more functions as needed). In general, you should not print in a Math-like function; just return a value. Be sure to use the same print function to print both the original array and the sorted array. DO NOT use global variables in your program.
* Display instructions and some useful information about your program
* Input a list of values into an array
* Print a list of values in an array (max 6 values per line)
* Sort a list of values in descending order
* Compute various statistics (one or more functions)
here is the code for my previous project:
#include <stdio.h>
#include <math.h>
/* onstant with max. array elements */
#define MAX_ELEMENTS 30
/* struct for each array element */
struct tArrayData {
unsigned int StudentID;
double Score;
char Grade;
};
/* struct with wanted data */
struct tWantedData {
unsigned int NumScores;
double MaxScore;
double Average;
double Median;
double StdDev;
unsigned int NumPassed;
};
void main()
{
/* two arrays, one with original data and one with ordered data */
struct tArrayData DataArray[MAX_ELEMENTS];
struct tArrayData OrderedArray[MAX_ELEMENTS];
/* wanted data */
struct tWantedData WantedData;
/* score readed from stdin and sum of scores */
double ScoreReaded = 0.0, ScoreSum = 0.0;
/* loop variables */
int i, j;
/* ordering variables */
double OrderingMax;
int OrderingMaxIndex;
/* initialize some wanted data */
WantedData.NumScores = 0;
WantedData.MaxScore = 0.0;
WantedData.NumPassed = 0;
WantedData.StdDev = 0.0;
/* get score values */
printf("Enter score value (-1 to quit): " );
scanf("%lf", &ScoreReaded);
while( (ScoreReaded != -1) && (WantedData.NumScores < 30) )
{
if ( (ScoreReaded < 0) || (ScoreReaded > 100) )
{
printf("Are you sure?? %0.1f is either too low or too high!\n", ScoreReaded);
}
else
{
/* save score */
DataArray[WantedData.NumScores].Score = ScoreReaded;
/* student ID */
DataArray[WantedData.NumScores].StudentID = WantedData.NumScores+1;
/* calculate grade */
if ( ScoreReaded >= 90.0 )
DataArray[WantedData.NumScores].Grade = 'A';
else if (ScoreReaded >= 80.0)
DataArray[WantedData.NumScores].Grade = 'B';
else if (ScoreReaded >= 70.0)
DataArray[WantedData.NumScores].Grade = 'C';
else if (ScoreReaded >= 60.0)
DataArray[WantedData.NumScores].Grade = 'D';
else if (ScoreReaded < 60.0)
DataArray[WantedData.NumScores].Grade = 'F';
WantedData.NumScores++;
ScoreSum += ScoreReaded;
if (ScoreReaded > WantedData.MaxScore)
{
WantedData.MaxScore = ScoreReaded;
}
if (ScoreReaded >= 70.0)
{
WantedData.NumPassed++;
}
}
/* asks for another score */
printf("Enter score value (-1 to quit): ");
scanf("%lf", &ScoreReaded);
}
/* prints original data */
printf("\nOriginal data:\n");
for(i=1; i<=WantedData.NumScores; i++)
{
printf("%0.1f ", DataArray[i-1].Score);
/* keeps 8 scores per line */
if ( (i % 8) == 0 )
printf("\n");}
/* order array (descending) */
for(i=0; i<WantedData.NumScores; i++)
{
OrderingMax = -1.0;
for(j=0; j<WantedData.NumScores; j++)
{
/* find max. value from original array */
if (DataArray[j].Score > OrderingMax)
{
OrderingMax = DataArray[j].Score;
/* keep index for later */
OrderingMaxIndex = j;
}
}
/* save last max. score */
OrderedArray[i] = DataArray[OrderingMaxIndex];
/* delete for next loop */
DataArray[OrderingMaxIndex].Score = -1.0;
}
/* calculate average */
WantedData.Average = ScoreSum / WantedData.NumScores;
/* calculate standard deviation */
for(i=0; i<WantedData.NumScores; i++)
{
WantedData.StdDev += ((OrderedArray[i].Score - WantedData.Average)*(OrderedArray[i].Score - WantedData.Average));
}
WantedData.StdDev = WantedData.StdDev / (double)WantedData.NumScores;
WantedData.StdDev = sqrt( WantedData.StdDev );
/* calculate median */
if (WantedData.NumScores % 2)
{
/* odd number of values */
WantedData.Median = OrderedArray[(WantedData.NumScores/2)].Score;
}
else
{
/* even number of values */
WantedData.Median = (OrderedArray[WantedData.NumScores/2].Score +
OrderedArray[(WantedData.NumScores/2)-1].Score) / 2.0;
}
/* print ordered array */
printf("\nOrdered data:\n");
for(i=1; i<=WantedData.NumScores; i++)
{
printf("%0.1f ", OrderedArray[i-1].Score);
/* keep 8 scores per line */
if ( (i % 8) == 0 )
printf("\n");
}
printf("\nNumber of test scores: %d\n", WantedData.NumScores);
printf("\n");
printf("Highest score: %0.1f\n", WantedData.MaxScore);
printf("\n");
printf("Average score: %0.1f\n", WantedData.Average);
printf("\n");
printf("Median score: %0.1f\n", WantedData.Median);
printf("\n");
printf("Standard deviation: %0.1f\n", WantedData.StdDev);
printf("\n");
printf("Percent of the students that passed the class: %0.1f\n", ((double)WantedData.NumPassed / (double)WantedData.NumScores)*100.0 );
printf("\n");
printf("Id\tScore\tGrade\n");
for(i=0; i<WantedData.NumScores; i++)
{
printf("%d\t%0.1f\t%c\n", OrderedArray[i].StudentID, OrderedArray[i].Score, OrderedArray[i].Grade);
}
return 0;
}
any help (in pseudocode of course) would be appreciated, thanks!