Другие языки программирования и технологии

Нужна программа на Си Которая из трех рандомных чисел, выбирает два наименьших числа

Серж !!!
Серж !!!
93
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
typedef struct { unsigned short a, b; } two;
typedef struct { unsigned short a, b, c; } three;
three random();
void print_3(three x);
void print_2(two x);
two f(three x);
int main(void) {
srand((unsigned)time(NULL));
do {
three x = random();
print_3(x);
two y = f(x);
print_2(y);
system("pause");
} while (true);
return 0;
}
two f(three x) {
two y;
y.a = x.a;
y.b = x.b;
if (y.a > y.b) {
y.a = x.b;
y.b = x.a;
}
if (y.a > x.c) {
y.b = y.a;
y.a = x.c;
}
else if (y.b > x.c) {
y.b = x.c;
}
return y;
}
three random() {
three x;
x.a = rand();
x.b = rand();
x.c = rand();
return x;
}
void print_3(three x) {
printf(" %d %d %d\n", x.a, x.b, x.c);
}
void print_2(two x) {
printf(" %d %d\n", x.a, x.b);
}
Gregory Altukhov
Gregory Altukhov
87 967
Лучший ответ
Александр Сапонов Николай, что это было? :))
Всё как-то гораздо проще делается:

$ cat rand3.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main () {
  int a, b, c;
  srand(time(NULL));
  a = rand() % 100; b = rand() % 100; c = rand() % 100;
  printf("%d %d %d\n", a, b, c);
  printf("%d %d\n", (a < b) ? a : b, (a < c) ? a : c);
  return 0;
}

Сборка:

$ clang rand3.c -o rand3

Проверка:

$ ./rand3
89 46 90
46 89

Похожие вопросы