要求:
1、編寫程序,實現如下功能。
(1)隨機生成 1000000 個 0~1 之間的數;
(2)統計分析這些數據,計算均值、方差和分布情況,分布情況按
0.01 的步長進行統計;
(3)使用內存映射,將這些數及計算的均值、方差及分布情況寫入
文件,同時將結果在終端顯示;
隨機數的存放要求使用動態內存分配的方式,注意內存的釋放。
代碼實現:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>#define NUM_SAMPLES 1000000
#define STEP_SIZE 0.01
#define NUM_BINS (int)(1.0 / STEP_SIZE)double calculate_mean(double *data, int size) {//計算數組的平均值double sum = 0.0;for (int i = 0; i < size; i++) {sum += data[i];}return sum / size;
}double calculate_variance(double *data, int size, double mean) {//計算數組的方差double sum = 0.0;for (int i = 0; i < size; i++) {sum += (data[i] - mean) * (data[i] - mean);}return sum / size;
}void calculate_distribution(double *data, int size, int *distribution) {//根據步長統計數組的分布情況for (int i = 0; i < size; i++) {int bin = (int)(data[i] / STEP_SIZE);if (bin < NUM_BINS) {distribution[bin]++;}}
}int main() {srand(time(NULL));double *data = (double *)malloc(NUM_SAMPLES * sizeof(double));if (data == NULL) {perror("Failed to allocate memory for data");exit(EXIT_FAILURE);}for (int i = 0; i < NUM_SAMPLES; i++) {data[i] = ((double)rand() / RAND_MAX);}double mean = calculate_mean(data, NUM_SAMPLES);double variance = calculate_variance(data, NUM_SAMPLES, mean);int *distribution = (int *)calloc(NUM_BINS, sizeof(int));if (distribution == NULL) {perror("Failed to allocate memory for distribution");free(data);exit(EXIT_FAILURE);}calculate_distribution(data, NUM_SAMPLES, distribution);printf("Mean: %lf\n", mean);printf("Variance: %lf\n", variance);printf("Distribution:\n");for (int i = 0; i < NUM_BINS; i++) {printf("[%lf, %lf): %d\n", i * STEP_SIZE, (i + 1) * STEP_SIZE, distribution[i]);}int fd = open("output.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);if (fd == -1) {perror("Failed to open file for memory mapping");free(data);free(distribution);exit(EXIT_FAILURE);}off_t file_size = (NUM_SAMPLES * sizeof(double)) + (NUM_BINS * sizeof(int)) + (2 * sizeof(double)); // Data + Distribution + Mean + Varianceif (ftruncate(fd, file_size) == -1) {perror("Failed to resize file");close(fd);free(data);free(distribution);exit(EXIT_FAILURE);}void *map = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);if (map == MAP_FAILED) {perror("Failed to memory map file");close(fd);free(data);free(distribution);exit(EXIT_FAILURE);}double *mapped_data = (double *)map;int *mapped_distribution = (int *)((char *)map + (NUM_SAMPLES * sizeof(double)));double *mapped_mean = (double *)((char *)map + (NUM_SAMPLES * sizeof(double)) + (NUM_BINS * sizeof(int)));double *mapped_variance = mapped_mean + 1;memcpy(mapped_data, data, NUM_SAMPLES * sizeof(double));memcpy(mapped_distribution, distribution, NUM_BINS * sizeof(int));*mapped_mean = mean;*mapped_variance = variance;close(fd);munmap(map, file_size);free(data);free(distribution);return 0;
}
運行結果:
創建并保存程序文件
使用gcc編譯程序生成可執行文件:在使用gcc編譯的過程中我遇到了無法編譯的情況,gcc 沒有使用 C99 標準來編譯代碼,而是使用了更舊的 C 標準,在其中添加-std解決了問題。
運行該程序得出結果:多次運行后產生的隨機數服從均勻分布。