🔒 Closed Help poo para sa paghanap ng mode

Status
Not open for further replies.

midnight knight

Grasshopper
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.

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);
}
 
iloop mu na lang uli para maextract lahat scores na same sa maxCount


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 maxCount = 0;
  int scoreCount[NUM_SCORE];

  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;
    }
    scoreCount[i] = count;
    if (count > maxCount)
    {
      maxCount = count;
    }
  }

  // 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\n", mean, median);
 
  // no mode when all scores appear the same number of times
  int noMode = 0;
  for (int i = 0; i < NUM_SCORE; ++i)
  {
    if (scoreCount[i] == maxCount)
      noMode++;
  }
 
  if (noMode != NUM_SCORE)
  {
    fprintf(file, "Mode = ");
    // loop through the scores array to check scores with same maxCount
    for (int i = 0; i < NUM_SCORE; ++i)
    {
      if (scoreCount[i] == maxCount)
        if (scores[i] != scores[i-1])
          fprintf(file, "%d ", scores[i]);
    }
  }
  else
    fprintf(file, "No Mode");
   
  fclose(file);
}



1670706144211.png


1670706183389.png
 
iloop mu na lang uli para maextract lahat scores na same sa maxCount


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 maxCount = 0;
  int scoreCount[NUM_SCORE];

  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;
    }
    scoreCount[i] = count;
    if (count > maxCount)
    {
      maxCount = count;
    }
  }

  // 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\n", mean, median);
 
  // no mode when all scores appear the same number of times
  int noMode = 0;
  for (int i = 0; i < NUM_SCORE; ++i)
  {
    if (scoreCount[i] == maxCount)
      noMode++;
  }
 
  if (noMode != NUM_SCORE)
  {
    fprintf(file, "Mode = ");
    // loop through the scores array to check scores with same maxCount
    for (int i = 0; i < NUM_SCORE; ++i)
    {
      if (scoreCount[i] == maxCount)
        if (scores[i] != scores[i-1])
          fprintf(file, "%d ", scores[i]);
    }
  }
  else
    fprintf(file, "No Mode");
  
  fclose(file);
}



View attachment 2393895

View attachment 2393896
thank you po! nagawa ko na po.
 
Status
Not open for further replies.

About this Thread

  • 4
    Replies
  • 496
    Views
  • 2
    Participants
Last reply from:
midnight knight

Trending Topics

Online now

Members online
982
Guests online
918
Total visitors
1,900

Forum statistics

Threads
2,277,922
Posts
28,979,805
Members
1,228,172
Latest member
gdarpawijaya
Back
Top