fork download
  1. // cs162_422.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // dummy Random
  6. double Random(double m, double s)
  7. {
  8. return 123.456 * (double) rand() / ((double) RAND_MAX + 1.0);
  9. }
  10.  
  11. // main
  12. int main(void)
  13. {
  14. int i, n;
  15. unsigned seed;
  16. double mu, siguma, *array;
  17.  
  18. int j, length;
  19. double max, min;
  20. const double max_width = 57.0; // 一番長い所の桁数
  21. const char mark = '*'; // ヒストグラムに使う文字
  22.  
  23. // input & setting
  24. printf("乱数系列(x): ");
  25. scanf("%u", &seed);
  26. printf("個数: ");
  27. scanf("%d", &n);
  28. printf("平均(μ): ");
  29. scanf("%lf", &mu);
  30. printf("分散(σ): ");
  31. scanf("%lf", &siguma);
  32. srand(seed);
  33. if ((array = (double *) malloc(sizeof(double) * n)) == NULL) {
  34. exit(1);
  35. }
  36.  
  37. // show
  38. printf("\n乱数表\n\n");
  39. for (i = 0; i < n; i++) {
  40. array[i] = Random(mu, siguma);
  41. if (i == 0) {
  42. max = min = array[0];
  43. } else {
  44. if (max < array[i]) {
  45. max = array[i];
  46. }
  47. if (min > array[i]) {
  48. min = array[i];
  49. }
  50. }
  51. }
  52. for (i = 0; i < n; i++) {
  53. printf("[%3d] %10.5f ", i + 1, array[i]);
  54. length = (int) (max_width * array[i] / max);
  55. for (j = 0; j < length; j++) {
  56. printf("%c", mark);
  57. }
  58. for (; j < max_width; j++) {
  59. printf(" ");
  60. }
  61. printf("|%s\n", max == array[i] ? " MAX" : (min == array[i] ? " min" : ""));
  62. }
  63.  
  64. // end
  65. if (array) {
  66. free(array);
  67. array = NULL;
  68. }
  69. return 0;
  70. }
Success #stdin #stdout 0.02s 1812KB
stdin
12
34
5.6
7.8
stdout
乱数系列(x): 個数: 平均(μ): 分散(σ): 
乱数表

[  1]   96.98707 ***********************************************          |
[  2]   54.34258 **************************                               |
[  3]   14.21211 ******                                                   |
[  4]  101.67143 *************************************************        |
[  5]   84.98640 *****************************************                |
[  6]   16.04008 *******                                                  |
[  7]   38.41519 ******************                                       |
[  8]   63.76730 *******************************                          |
[  9]   79.25974 **************************************                   |
[ 10]   60.62121 *****************************                            |
[ 11]   10.53393 *****                                                    |
[ 12]   51.13318 ************************                                 |
[ 13]   35.30929 *****************                                        |
[ 14]   10.82589 *****                                                    |
[ 15]   45.06610 *********************                                    |
[ 16]   59.30257 ****************************                             |
[ 17]   78.94647 **************************************                   |
[ 18]   16.07715 *******                                                  |
[ 19]   95.94763 **********************************************           |
[ 20]  110.48860 *****************************************************    |
[ 21]  101.14007 *************************************************        |
[ 22]   74.29380 ************************************                     |
[ 23]   46.66570 **********************                                   |
[ 24]  110.44611 *****************************************************    |
[ 25]   24.32167 ***********                                              |
[ 26]   22.91278 ***********                                              |
[ 27]  116.87629 *********************************************************| MAX
[ 28]   95.88910 **********************************************           |
[ 29]    3.94487 *                                                        | min
[ 30]   84.14983 *****************************************                |
[ 31]   99.92911 ************************************************         |
[ 32]  100.93194 *************************************************        |
[ 33]   15.03641 *******                                                  |
[ 34]  114.14122 *******************************************************  |