// Q.3
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define DATA_MAX 50
void random_data(int data[],int num)
{
int i, n;
for (i = 0; i < num; i++) {
data[i] = -1;
}
for (i = 0; i < num; ) {
n
= (int)((double)num
*rand()/(1.0+RAND_MAX
)); if (data[n] != -1) continue;
data[n] = i;
i++;
}
}
void output_data(int data[], int num, const char *filename)
{
FILE *pfile;
int i;
for (i = 0; i < num; i++) {
printf("%d %d\n", i
, data
[i
]); }
pfile
= fopen(filename
, "wt"); if (!pfile) return;
for (i = 0; i < num; i++) {
fprintf(pfile
, "%d %d\n", i
, data
[i
]); }
}
void bubble(int data[],int num)
{
int i, j, t;
for (i = num - 1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (data[j] > data[j+1]) {
t = data[j];
data[j] = data[j+1];
data[j+1] = t;
}
}
}
}
int main()
{
int data[DATA_MAX];
int num;
printf("データの数(MAX:%d)>", DATA_MAX
); random_data(data, num);
output_data(data, num, "q3-1.log");
bubble(data, num);
output_data(data, num, "q3-2.log");
return 0;
}
Ly8gUS4zCiNkZWZpbmUgX0NSVF9TRUNVUkVfTk9fV0FSTklOR1MKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgREFUQV9NQVggNTAKCnZvaWQgcmFuZG9tX2RhdGEoaW50IGRhdGFbXSxpbnQgbnVtKQp7CglpbnQgaSwgbjsKCglmb3IgKGkgPSAwOyBpIDwgbnVtOyBpKyspIHsKCQlkYXRhW2ldID0gLTE7Cgl9Cglmb3IgKGkgPSAwOyBpIDwgbnVtOyApIHsKCQluID0gKGludCkoKGRvdWJsZSludW0qcmFuZCgpLygxLjArUkFORF9NQVgpKTsKCQlpZiAoZGF0YVtuXSAhPSAtMSkgY29udGludWU7CgkJZGF0YVtuXSA9IGk7CgkJaSsrOwoJfQp9Cgp2b2lkIG91dHB1dF9kYXRhKGludCBkYXRhW10sIGludCBudW0sIGNvbnN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