midnight knight
Journeyman
- Joined
- Dec 7, 2022
- Posts
- 25
- Solutions
- 1
- Reaction
- 2
- Points
- 16
paano ko po maiaapply itong ganitong condition?
Note: There is no mode when all observed values appear the same number of times in
the set of exam scores. There may be more than one mode when the highest frequency
was observed for more than one value in the set.
Note: There is no mode when all observed values appear the same number of times in
the set of exam scores. There may be more than one mode when the highest frequency
was observed for more than one value in the set.
C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SCORE 95
#define MIN_SCORE 75
#define NUM_SCORE 50
int compare_ints(const void*a, const void *b)
{
return (*(int *) a - *(int *)b);
}
int main() {
// Seed the random number generator
srand(time(NULL));
// Generate the random exam scores
int scores[NUM_SCORE];
for (int i = 0; i < NUM_SCORE; i++)
{
scores[i] = rand() % (MAX_SCORE - MIN_SCORE + 1) + MIN_SCORE;
}
// Write the exam scores to a file in columns of 10
FILE *file = fopen("RawExamScores.txt", "w");
if (file == NULL) {
perror("Error opening file");
return -1;
}
for (int i = 0; i < NUM_SCORE; i++)
{
fprintf(file, "%d ", scores[i]);
if (i % 10 == 9)
{
fprintf(file, "\n");
}
}
fclose(file);
// Read the exam scores from the file
file = fopen("RawExamScores.txt", "r");
if (file == NULL)
{
perror("Error opening file");
return -1;
}
for (int i = 0; i < NUM_SCORE; i++)
{
fscanf(file, "%d", &scores[i]);
}
fclose(file);
// Sort the exam scores in increasing order
qsort(scores, NUM_SCORE, sizeof(int), compare_ints);
// Write the sorted exam scores to a file
file = fopen("SortedExamScores.txt", "w");
if (file == NULL)
{
perror("Error opening file");
return -1;
}
for (int i = 0; i < NUM_SCORE; i++)
{
fprintf(file, "%d\n", scores[i]);
}
fclose(file);
// MEAN = total_scores / NUM_SCORE
// to find the MEAN, divide the sum(total) of all scores by the num_score
float total_score;
for (int i = 0; i < NUM_SCORE; i++)
{
total_score += scores[i];
}
float mean = total_score / NUM_SCORE;
// MEDIAN
// to find the MEDIAN, get the middle number of the sorted scores (if there are 2 numbers in mid, get the average of the 2 numbers)
float median;
if (NUM_SCORE % 2 == 0)
median = (float)(scores[NUM_SCORE / 2 - 1] + scores[NUM_SCORE / 2]) / 2.0;
else
median = (float)scores[(NUM_SCORE - 1) / 2];
// MODE
// to find the MODE, iterate through the sorted scores array and store each score's maxcount then compare with each succeeding ones
int mode, maxCount = 0;
for (int i = 0; i < NUM_SCORE; ++i)
{
int count = 0;
for (int j = 0; j < NUM_SCORE; ++j)
{
if (scores[j] == scores[i])
++count;
}
if (count > maxCount)
{
maxCount = count;
mode = scores[i];
}
}
// Append the mean, median, mode to SortedExamScores.txt
file = fopen("SortedExamScores.txt", "a");
if (file == NULL)
{
perror("Error opening file");
return -1;
}
fprintf(file, "\nMean = %.2f\nMedian = %.2f\nMode = %d", mean, median, mode);
}