Вариант А1
Написать программу, вычисляющую среднее арифметическое N целых чисел. Исходные данные должны находиться в файле input.txt, результат работы записан в файл output.txt.
Формат файла input.txt:
5
1 2 3 4 5
Формат файла output.txt:
3.0
Спойлер
// Вариант А1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define f_path "../input.txt"
#define out_path "../output.txt"
int main() {
int c = 0, i, b = 0, f = 0;
float a = 0, sred = 0;
FILE *fil, *outp;
fil = fopen(f_path, "r");
if (fil == NULL) {
printf("Error open file to read\n");
return -1;
} printf("File open to read\n");
fscanf(fil, "%i", &b);
for (f = 0; f < b; f++) {
fscanf(fil, "%i", &c);
printf("%i ", c);
a += c;
} printf("\nFile read\n");
i = fclose(fil);
outp = fopen(out_path, "w");
if (outp == NULL) {
printf("File error");
return -1;
} printf("File open to write\n");
printf("%.0f\n", a);
printf("%i\n", b);
fprintf(outp, "%f", sred = a / b);
printf("%f", sred);
printf("\nFile read OK\n");
i = fclose(outp);
if (i != 0) {
printf("Error close file\n");
return -1;
} printf("File closed");
}
Вариант А2
Написать программу, выводящую в текстовый файл таблицу квадратов всех целых чисел, расположенных между A и B. Исходные данные должны находиться в файле input.txt, результат работы записан в файл output.txt.
Формат файла input.txt:
2 6
Формат файла output.txt:
2 4
3 9
4 16
5 25
6 36
Спойлер
// Вариант А2
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define f_path "../input.txt"
#define out_path "../output.txt"
int main() {
int i, a, b, z, x;
FILE *fil, *filout;
fil = fopen(f_path, "r");
if (fil == NULL) {
printf("Error open to read\n");
return -1;
} printf("Open to read\n");
fscanf(fil, "%i%i", &a, &b);
printf("%i%i", a, b);
printf("\nRead OK\n");
i = fclose(fil);
if (i != 0) {
printf("Error to close");
return -1;
} printf("File closed\n");
filout = fopen(out_path, "w");
if (filout == NULL) {
printf("Error open to write");
return -1;
} printf("\nFile open to write\n");
i = 0;
for (z = a; z <= b; z++) {
x = z * z;
fprintf(filout, "%i %i\n", z, x);
printf("%i %i\n", z, x);
} printf("File write OK");
i = fclose(filout);
if (i != 0) {
printf("Error to close");
return -1;
} printf("\nFile closed");
}
Вариант А3
Написать программу, сортировки одномерного массива из N вещественных чисел. Исходные данные должны находиться в файле input.txt, результат работы записан в файл output.txt. Массив создавать динамически.
Формат файла input.txt:
5
1.1 5.4 2.3 4.2 3.5
Формат файла output.txt:
1.1 2.3 3.5 4.2 5.4
Спойлер
// Вариант А3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define f_path "../input.txt"
#define out_path "../output.txt"
int main() {
int i, j, minnum, n;
float *a, min, temp;
FILE* fil;
fil = fopen(f_path, "r");
if (fil == NULL) {
printf("Error open file to read\n");
return -1;
} printf("Open to read\n");
fscanf(fil, "%i", &n);
a = new float[n];
for (i = 0; i < n; i++) {
fscanf(fil, "%f", &a[i]);
printf("a[%i]=%.1f\n", i, a[i]);
} printf("\nGotovo");
for (i = 0; i < n - 1; i++) {
min = a[i];
minnum = i;
for (j = i + 1; j < n; j++) {
if (a[j] < min) {
min = a[j];
minnum = j;
}
}
temp = a[i];
a[i] = a[minnum];
a[minnum] = temp;
}
for (i = 0; i < n; i++) {
printf("\na[%i]=%.1f", i, a[i]);
}
i = fclose(fil);
if (i!=0) {
printf("Error to close\n");
} printf("\nClosed OK\n");
fil = fopen(out_path, "w");
if (fil == NULL) {
printf("Error open to write\n");
} printf("Open to write\n");
for (i = 0; i < n; i++) {
fprintf(fil, "%.1f", a[i]);
} mprintf("Write OK");
i = fclose(fil);
if (i!=0) {
printf("Error to close\n");
} printf("\nClosed OK\n");
}
Вариант А4
Написать программу, вычисляющую количество ненулевых элементов в двумерном массиве. Размер массива и исходные данные должны находиться в файле input.txt, результат работы записан в файл output.txt. Массив создавать динамически.
Формат файла input.txt:
3 4
1 3 6 3
3 0 2 4
4 2 0 0
Формат файла output.txt:
9
Спойлер
// Вариант А4
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define f_path "../input.txt"
#define f_read_path "../output.txt"
int main() {
int **a, i, j = 0, b = 0;
int n, m;
FILE* fil;
fil = fopen(f_path, "r");
if (fil == NULL) {
printf("Error open file\n");
return -1;
}
printf("File open to read\n");
fscanf(fil, "%i %i", &n, &m);
a = new int*[n];
for (i = 0; i < n; i++) {
a[i] = new int[m];
}
for (i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
fscanf(fil, "%i", &a[i][j]);
}
}
i = fclose(fil);
if (i != 0) {
printf("Error close file\n");
return -1;
}
printf("File closed\n");
}
