lr44/lr19/Project2/Source.cpp
2024-11-12 18:55:44 +04:00

64 lines
1.4 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <Windows.h>
int a[1000]; //ìàññèâ. 1000 - ìàêñèìàëüíî äîïóñòèìîå êîëè÷åñòâî ýëåìåíòîâ
int n; // Ðåàëüíîå êîëè÷åñòâî ýëåìåíòîâ â ìàññèâå
void Load() {
// Îòêðûòèå âõîäíîãî ôàéëà
FILE* fin = fopen("c:\\lab_19\\in1.txt", "rt");
if (fin == NULL) {
printf("Âõîäíîé ôàéë íå íàéäåí\n");
return;
}
// Çàãðóçêà ìàññèâà èç âõîäíîãî ôàéëà
fscanf_s(fin, "%d", &n);
for (int i = 0; i < n; i++) {
fscanf_s(fin, "%d", &a[i]);
}
// Çàêðûòèå âõîäíîãî ôàéëà
fclose(fin);
}
void SaveResult() {
// Âû÷èñëåíèå ñðåäíåãî àðèôìåòè÷åñêîãî
float sa = 0;
float s = 0;
for (int i = 0; i < n; i++) {
s += a[i];
}
sa = s / n;
// Âûÿñíåíèå, ñêîëüêî ýëåìåíòîâ áîëüøå ñð àðèôìåòè÷åñêîãî
int m = 0;
for (int i = 0; i < n; i++) {
if (a[i] > sa) {
m++;
}
}
FILE* fout = fopen("c:\\lab_19\\out1.txt", "wt");
if (fout == NULL) {
printf("Âûõîäíîé ôàéë íå íàéäåí\n");
return;
}
// Ñîõðàíåíèå ýëåìåíòîâ áîëüøèõ ñð àðèôìåòè÷åñêîãî
fprintf(fout, "%d\n", m);
for (int i = 0; i < n; i++) {
if (a[i] > sa) {
fprintf(fout, "%d ", a[i]);
}
}
// Çàêðûòèå ôàéëà
fclose(fout);
}
void main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
printf("Hello! It is Task1!\n");
Load();
SaveResult();
}