What's new

C & C++ File Handling in C: Pahelp po

midnight knight

Journeyman
C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SCORE 40
#define MIN_SCORE 20
#define NUM_SCORE 10

int compare_ints(const void*a, const void *b)
{
    return (*(int *) a - *(int *)b);
}

 tf(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]);
  }
 
}
 
Last edited:
Solution
eto pala idagdag mo fclose(file); after nung full code ng // Write the sorted exam scores to a file para maclose muna yung file connection bago mag append.

eto full code

C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SCORE 40
#define MIN_SCORE 20
#define NUM_SCORE 10

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...
midnight knight
i apply mo lang yung formula ng mean, median at mode. Yung mean is average lang. Yung median at mode, since na sort na using qsort, madali na rin. Eto check mo sample implementation.

reference: You do not have permission to view the full content of this post. Log in or register now.

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]);
  }
  // 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);
}
 
Last edited:
sa pinakababa ng SortedExamScores.txt
salamat po pero wala po kasing lumalabas.
Screenshot (133).png


Working na po kaso bakit po ang dami huhu
Screenshot (134).png
 

Attachments

Last edited:
eto pala idagdag mo fclose(file); after nung full code ng // Write the sorted exam scores to a file para maclose muna yung file connection bago mag append.

eto full code

C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SCORE 40
#define MIN_SCORE 20
#define NUM_SCORE 10

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);
}


1670661015068.png
 

Attachments

Last edited:
Solution
Thank you po! God Bless you!

eto pala idagdag mo fclose(file); after nung full code ng // Write the sorted exam scores to a file para maclose muna yung file connection bago mag append.

eto full code

C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SCORE 40
#define MIN_SCORE 20
#define NUM_SCORE 10

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);
}


View attachment 2392608
Thank you po! God Bless you!
 
Last edited:

Similar threads

Back
Top