What's new

C & C++ Pa help po, File Handling in C: How do I sort the numbers saved in the file? Paano din po mag read ng file?

midnight knight

Journeyman
Joined
Dec 7, 2022
Posts
25
Solutions
1
Reaction
2
Points
16
Create a C program that does the following:

  1. Generates 50 random exam scores and then writes the generated scores to the file RandomExamScores.txt in columns of 10. The highest and lowest possible scores are 40 and 20, respectively.
  2. Reads the exam scores from the file RandomExamScores.txt, sorts the exam scores in increasing order, and writes the sorted scores to the file SortedExamScores.txt.
  3. Determines the mean, median, and mode of the exam scores.
  4. Appends to the file SortedExamScores.txt the results in (3).
I am new to programming, I badly need help.

Here is what I have done so far. Perhaps, I need help in sorting out the numbers I have in the file "RandomExamScores.txt"

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

#define SIZE 50

void RandomExamScores()
{
FILE *fptr;
fptr = fopen("RandomExamScores.txt", "w");
int count;
int num[SIZE];
int lower = 20, upper = 40;
srand(time(NULL));

for (count = 0; count < SIZE; count++)
num[count] = (rand() % (upper - lower + 1)) + lower;

for(count = 0; count < SIZE; count++)
{

if(count % 10 == 0)
fprintf(fptr, "\n");

fprintf(fptr, "%4d ", num[count]);

}

}

int main()
{

fprintf(fptr, "Random Exam Scores: \n");
RandomExamScores();

fclose(fptr);

}
 
wait try ko sa generator ko

wait try ko sa generator ko
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SCORE 40
#define MIN_SCORE 20
#define NUM_SCORES 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_SCORES];
  for (int i = 0; i < NUM_SCORES; 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("RandomExamScores.txt", "w");
  if (file == NULL) {
    perror("Error opening file");
    return -1;
  }
  for (int i = 0; i < NUM_SCORES; i++) {
    fprintf(file, "%d ", scores[i]);
    if (i % 10 == 9) {
      fprintf(file, "\n");
    }
  }
  fclose(file);

  // Read the exam scores from the file
  file = fopen("RandomExamScores.txt", "r");
  if (file == NULL) {
    perror("Error opening file");
    return -1;
  }
  for (int i = 0; i < NUM_SCORES; i++) {
    fscanf(file, "%d", &scores[i]);
  }
  fclose(file);

  // Sort the exam scores in increasing order
  qsort(scores, NUM_SCORES, 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_SCORES; i++) {
    fprintf(file, "%d\n", scores[i]);
  }

  // Calculate the mean, median, and mode of the exam scores
  double mean = 0.0;
  for (int i = 0; i < NUM_SCORES; i++) {
    mean += (double)scores[i] / NUM_SCORES;
  }

  double median;
  if (NUM_SCORES % 2 == 0) {
    median = ((double)scores[NUM_SCORES / 2 - 1] + (double)scores[NUM_SCORES / 2]) / 2;
  } else {
    median = (double)scores[NUM_SCORES / 2];
  }

  int mode = 0;
  int max_count = 0;
  for (int i = 0; i < NUM_SCORES; i++) {
    int count = 0;
    for (int j = 0; j < NUM_SC
 
Screenshot (129).png


thank you po! pero ganyan po ba talaga nageerror?
 

Attachments

#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX_SCORE 40 #define MIN_SCORE 20 #define NUM_SCORES 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_SCORES]; for (int i = 0; i < NUM_SCORES; i++) { scores = rand() % (MAX_SCORE - MIN_SCORE + 1) + MIN_SCORE; } // Write the exam scores to a file in columns of 10 FILE *file = fopen("RandomExamScores.txt", "w"); if (file == NULL) { perror("Error opening file"); return -1; } for (int i = 0; i < NUM_SCORES; i++) { fprintf(file, "%d ", scores); if (i % 10 == 9) { fprintf(file, "\n"); } } fclose(file); // Read the exam scores from the file file = fopen("RandomExamScores.txt", "r"); if (file == NULL) { perror("Error opening file"); return -1; } for (int i = 0; i < NUM_SCORES; i++) { fscanf(file, "%d", &scores); } fclose(file); // Sort the exam scores in increasing order qsort(scores, NUM_SCORES, 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_SCORES; i++) { fprintf(file, "%d\n", scores); } // Calculate the mean, median, and mode of the exam scores double mean = 0.0; for (int i = 0; i < NUM_SCORES; i++) { mean += (double)scores / NUM_SCORES; } double median; if (NUM_SCORES % 2 == 0) { median = ((double)scores[NUM_SCORES / 2 - 1] + (double)scores[NUM_SCORES / 2]) / 2; } else { median = (double)scores[NUM_SCORES / 2];

okaay po, pero error parin po eh kahit walang semi colon
 
Back
Top