我正在独自学习一本书,而我一生都无法弄清楚如何解决该练习。我显然是在用错误的方式看待它。以下是说明。
下面列出了一些功能,而底部列出了主要功能。编译该程序以生成一定数量的随机数,并确定随机数的最小值和最大值。如果复制并粘贴此代码,您将看到其工作方式。无论如何,一个练习要求我转到函数“ prn_random_numbers()”并将for循环从“ for(i = 1; i 总而言之,编写“ prn_random_numbers()”函数以打印出5个随机数,然后再移至下一行。因此,“ i%5” if语句。现在,由于某种原因,当您对for循环进行细微调整时(如上面的练习所要求的那样),它将导致第一行仅打印4个数字,然后移至下一行。我已经尝试了很多方法,包括试图强迫它打印第5个数字,但是它只重复了一个随机数。我什至尝试使用“ i%4”来查看是否每行打印4个数字,但第一行只打印3个数字,而不是4个!因此,它总是在第一行上打印比预期少的数字。我不知道为什么要这么做,而这本书没有做练习。你有什么主意吗? 如果您认为这是一个愚蠢的问题,请和我一起忍受。我只是在独自学习,我想确保自己有良好的基础,并在继续学习之前了解所学的一切。感谢您的帮助或建议!prn_random_numbers(k) /* print k random numbers */
int k;
{
int i, r, smallest, biggest;
r = smallest = biggest = rand();
printf("\n%12d", r);
for (i = 1; i < k; ++i)
{
if (i % 5 == 0)
printf("\n");
r = rand();
smallest = min(r, smallest);
biggest = max(r, biggest);
printf("%12d", r);
}
printf("\n\n%d random numbers printed.\n", k);
printf("Minimum:%12d\nMaximum:%12d\n", smallest, biggest);
}
int main()
{
int n;
printf("Some random numbers are to be printed.\n");
printf("How many would you like to see? ");
scanf("%d", &n);
while (n < 1)
{
printf("ERROR! Please enter a positive integer.\n");
printf("How many would you like to see? ");
scanf("%d", &n);
}
prn_random_numbers(n);
return (EXIT_SUCCESS);
}
答案 0 :(得分:1)
以下建议的代码:
max()
和min()
现在是建议的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void prn_random_numbers(int k)
{
int count = 1;
int r;
int smallest;
int biggest;
r = smallest = biggest = rand();
printf("\n%12d", r);
for ( int i = 2; i <= k; i++, count++)
{
if (count % 5 == 0)
{
count = 0;
printf("\n");
}
r = rand();
smallest = (r < smallest)? r : smallest;
biggest = (r > biggest)? r : biggest;
printf("%12d", r);
}
printf("\n\n%d random numbers printed.\n", k);
printf("Minimum:%12d\nMaximum:%12d\n", smallest, biggest);
}
int main( void )
{
int n;
srand( (unsigned)time( NULL ) );
do
{
printf("Please enter a positive integer, greater than 0.\n");
printf("How many would you like to see? ");
if( scanf("%d", &n) != 1 )
{
fprintf( stderr, "scanf for number of random numbers failed\n" );
exit( EXIT_FAILURE );
}
} while( n < 1 );
prn_random_numbers(n);
// in modern C, if the returned value from `main()` is 0 then no `return 0;` statement needed
}
一个典型的运行,没有输入问题是
Please enter a positive integer, greater than 0.
How many would you like to see? 20
98697066 2110217332 1247184349 421403769 1643589269
1440322693 985220171 1915371488 1920726601 1637143133
2070012356 541419813 1708523311 1237437366 1058236022
926434075 1422865093 2113527574 626328197 1618571881
20 random numbers printed.
Minimum: 98697066
Maximum: 2113527574
答案 1 :(得分:0)
尝试使用调试器解决您的问题,它易于使用,并且非常有用:)
解决方案:
您的i
变量不计算数量,因为它的初始值是1(在for语句中),因此您需要声明一个新变量以正确计数。
如果仍然有问题:
void prn_random_numbers(int k)
{
int count = 1;
int i, r, smallest, biggest;
r = smallest = biggest = rand();
printf("\n%12d", r);
for (i = 2; i <= k; i++, count++) {
if (count % 5 == 0) {
count = 0;
printf("\n");
}
r = rand();
smallest = min(r, smallest);
biggest = max(r, biggest);
printf("%12d", r);
}
printf("\n\n%d random numbers printed.\n", k);
printf("Minimum:%12d\nMaximum:%12d\n", smallest, biggest);
}