This repository has been archived on 2026-07-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
college-work/Задания по C и C++/c/30.c
T

53 lines
1.5 KiB
C
Executable File

/*
30.Загадать случайным образом 50 действительных чисел в диапазоне [-5.5;5.5].
Записать их в файл «binfloat.dat» - каждое число записывается в 4-х байтном представлении.
Прочитать данные из файла «binfloat.dat», найти максимальное значение.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
const int MIN = -55;
const int MAX = 55;
const int AMOUNT = 50;
float* getRandomFloatArray() {
srand(time(0));
float *array = malloc(sizeof(float)*AMOUNT);
for (int i = 0; i < AMOUNT;i++) {
array[i] = (rand() % (abs(MIN) + abs(MAX) + 1) - abs(MIN))/10.0f;
}
return array;
}
void writeData(char* filename) {
FILE *file = fopen(filename, "wb");
float *array = getRandomFloatArray();
for (int i = 0; i < AMOUNT; i++) {
fwrite(&array[i], sizeof(float), 1, file);
}
fclose(file);
}
float getMaximumNumber(char* filename) {
FILE *file = fopen(filename, "rb");
float number, maximum = 0;
while(fread(&number, sizeof(float), 1, file) == 1) {
maximum = maximum < number ? number : maximum;
}
fclose(file);
return maximum;
}
int main() {
char *filename = "binfloat.dat";
writeData(filename);
printf("Maximum: %.1f", getMaximumNumber(filename));
return 0;
}