windows C语言中的文件处理:如何查找平均值、中位数和众数?[closed]

jfewjypa  于 2022-12-14  发布在  Windows
关注(0)|答案(1)|浏览(143)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

4天前关闭。
Improve this question
创建一个执行以下操作的C程序:
1.生成50个随机考试分数,然后将生成的分数写入文件RandomExamScores.txt,每列10个。可能的最高和最低分数分别为40和20。
1.从文件RandomExamScores.txt中读取考试分数,按升序对考试分数进行排序,然后将排序后的分数写入文件SortedExamScores.txt。
1.确定考试分数的平均值、中值和众数。
1.将(3)中的结果附加到文件SortedExamScores.txt。
我是编程新手,我急需帮助。
以下是我目前所做的工作。

#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]);
  }
  
}
8zzbczxx

8zzbczxx1#

我已经为你编写了这个作为参考。我使用了一个二维数组,我用它来生成随机数,并将它们打印到一个文件中。然后将相同的数组设置为全零,并用于从生成的文件中读取值。还要注意的是,我没有手工编写排序算法,但是使用了stdlib.h中的函数qsort。但是如果你想自己尝试一下,这里有非常好的关于排序算法的解释。GeeksforGeeks - Sorting Algoritms
如果你对此有任何问题,请告诉我。我希望这对你有帮助。:)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>

#define COL 10
#define ROW 5
#define SIZE (COL * ROW)

void PrintArray(int numbers[ROW][COL]) {
  for (int i = 0; i < ROW; i++) {
    for (int j = 0; j < COL; j++)
      printf("%d  ", numbers[i][j]);
    printf("\n");
  }
}

void WriteValuesToFile(int arr[ROW][COL]) {
  FILE *fptr;
  fptr = fopen("RandomExamScores.txt", "w+");
  fprintf(fptr, "Random Exam Scores: \n");

  for (int i = 0; i < ROW; i++) {
    fprintf(fptr, "\n");
    for (int j = 0; j < COL; j++)
      fprintf(fptr, "%4d ", arr[i][j]);
  }

  fclose(fptr);
}

int GetRandomNumber(int min, int max) {
  return (rand() % (max - min + 1)) + min;
}

void CreateRandomExamScores(int arr[ROW][COL]) {

  int lower = 20, upper = 40;
  srand(time(NULL));
  for (int i = 0; i < ROW; i++)
    for (int j = 0; j < COL; j++)
      arr[i][j] = GetRandomNumber(lower, upper);
}

void Fill2DArrayFromFile(int arr[ROW][COL]) {
  int i;
  int j;
  FILE *fptr = fopen("RandomExamScores.txt", "r");

  i = 0;
  j = 0;
  bool firstDigitWritten = false;
  bool gotWhiteSpace = false;
  while (!feof(fptr)) {
    char c = fgetc(fptr);
    if (isdigit(c)) {
      arr[i][j] = arr[i][j] * 10 + (c - '0');
      firstDigitWritten = true;
      gotWhiteSpace = false;
    } else if (c == ' ' && firstDigitWritten && !gotWhiteSpace) {
      j++;
      gotWhiteSpace = true;
    } else if (c == '\n' && firstDigitWritten) {
      i++;
      j = 0;
    }
  }
  fclose(fptr);
}

// compare function is used as a function pointer in the qsort function
int compare( const void* a, const void* b)
{
  int int_a = * ( (int*) a );
  int int_b = * ( (int*) b );
  if ( int_a == int_b ) return 0;
  else if ( int_a < int_b ) return -1;
  else return 1;
}

// the qsort function is built into the stdlib
void SortArray(int arr[ROW][COL]) {
  for (int i = 0; i < ROW; i++)
    qsort( arr[i], COL, sizeof(int), compare);
}

int main() {
  int numbers[ROW][COL] = { 0 };
  CreateRandomExamScores(numbers);  // generate random numbers in array
  WriteValuesToFile(numbers); // write random values to file

  memset(numbers, 0, sizeof(int) * SIZE); // reset array to 0
  Fill2DArrayFromFile(numbers); // read the values from the file to the array
  SortArray(numbers); // sort the array
  WriteValuesToFile(numbers); // write the values from the array to the file

  PrintArray(numbers);
}

相关问题