Skip to content

Instantly share code, notes, and snippets.

@G36maid
Created December 30, 2024 17:34
Show Gist options
  • Save G36maid/9087ee88410d300465d11037fa1c2d40 to your computer and use it in GitHub Desktop.
Save G36maid/9087ee88410d300465d11037fa1c2d40 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#include"myscore.h"
#include<math.h>
int transformScores(double *arr, int n, double pref_avg, double pref_stddev);
void findMinMax(const double *arr, int n, double *min, double *max);
int err_test();
int main(){
double **ppScores = NULL;
setupCourse(&ppScores, 10); // 10 classes
int student_per_class[10];
int total_students = 0;
for(int i = 0; i < 10; i++){
student_per_class[i] = rand() % 50 + 1; // Random number of students between 1 and 50
total_students += student_per_class[i];
setupClass(ppScores, i+1, student_per_class[i]);
}
double student_scores[total_students];
int index = 0;
for(int i = 0; i < 10; i++){
setupClass(ppScores, i+1, student_per_class[i]);
for (int j = 0; j < student_per_class[i]; j++){
double score = (rand() % 101) + 0.0; // Random score between 0 and 100
inputScore(ppScores, i+1, j+1, score);
student_scores[index++] = score;
}
}
double best, worst;
double best_ta, worst_ta;
int points = 0;
points += (findBest(ppScores, &best)==0);
points += (findWorst(ppScores, &worst)==0);
findMinMax(student_scores, total_students, &worst_ta, &best_ta);
if ( best - best_ta < 0.01 ){
points += 1;
}
if ( worst - worst_ta < 0.01 ){
points += 1;
}
points += (tscore(ppScores, 50, 10)==0); // Preferred average 50, preferred standard deviation 10
transformScores(student_scores, total_students, 50, 10);
findBest(ppScores, &best);
findWorst(ppScores, &worst);
findMinMax(student_scores, total_students, &worst_ta, &best_ta);
if ( best - best_ta < 0.01 ){
points += 1;
}
if ( worst - worst_ta < 0.01 ){
points += 1;
}
points += err_test();
freeCourse(ppScores);
printf("Points: %d\n", points);
return 0;
}
int err_test(){
int points = 0;
//inputScore, findBest, findWorst, tscore
// should return -1 if ppScores is NULL
double **ppScores = NULL;
double score;
points += (inputScore(ppScores, 1, 1, 100) == -1);
points += (findBest(ppScores, &score) == -1);
points += (findWorst(ppScores, &score) == -1);
points += (tscore(ppScores, 50, 10) == -1);
return points -1;
}
int transformScores(double *arr, int n, double pref_avg, double pref_stddev) {
// Validate input
if (arr == NULL || n <= 0) {
return -1; // Error: Invalid input
}
// Calculate mean (oldMean)
double sum = 0.0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
double oldMean = sum / (double)n;
// Calculate standard deviation (oldStdDev)
double sqSum = 0.0;
for (int i = 0; i < n; i++) {
double diff = arr[i] - oldMean;
sqSum += diff * diff;
}
double oldStdDev = sqrt(sqSum / (double)n);
// Transform scores to T-scores
if (oldStdDev == 0.0) {
// If all scores are identical, a simple fallback: set everything to preferredAverage
for (int i = 0; i < n; i++) {
arr[i] = pref_avg;
}
}else{
for (int i = 0; i < n; i++) {
arr[i] = pref_stddev * (arr[i] - oldMean) / oldStdDev + pref_avg;
}
}
return 0; // Success
}
void findMinMax(const double *arr, int n, double *min, double *max) {
if (arr == NULL || n <= 0) {
*min = *max = 0; // 處理無效輸入
return;
}
*min = *max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < *min) {
*min = arr[i];
}
if (arr[i] > *max) {
*max = arr[i];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment