Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int count_min(int *a, unsigned int n);
- // Подсчет количества чисел, равных минимальному
- int count_min(int *a, unsigned int n)
- {
- int min = 2147483647;
- unsigned int i;
- unsigned int count;// Счетчик минимальных
- for(i = 0; i<n; i++)
- {
- if(a[i]==min)
- {
- ++count;
- } else
- if(a[i]<min)
- {
- min = a[i];
- count = 1;
- }
- }
- return count;
- }
- int main(void)
- {
- FILE *f;
- int *a;// Это будет массив, когда выделю память
- unsigned int n;// Размер массива
- char fn[256];
- unsigned int i;
- printf("Write file name: ");
- if(scanf("%s", fn)<=0)
- {
- printf("Input from console error!\n");
- return 1;
- }
- f = fopen(fn, "r");
- if(f==NULL)
- {
- printf("Open file error!\n");
- return 2;
- }
- if(fscanf(f, "%d", &n)<=0)// Первое считываемое число - кол-во элементов в массиве
- {
- printf("Input from file error!\n");
- return 3;
- }
- if(n==0)
- {
- printf("Result: no elements!\n");
- return 0;
- }
- a = (int*)malloc(n*sizeof(int));
- for(i = 0; i<n; i++)
- {
- if(fscanf(f, "%d", &a[i])<=0)
- {
- printf("Input from file error!\n");
- free(a);
- return 3;
- }
- }
- // Массив считан. Поздравляю! Но это не конец...
- printf("Result: %d\n", count_min(a, n));
- free(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement