在这个程序中,我们添加了 0 到 100 之间生成的随机数。
每次运行后,随机数之和的结果都是不同的,即,我们得到不同的结果每次执行。
我们用来计算 0 到 100 之间的随机数之和的逻辑是 –
for(i = 0; i首先,我们计算随机数的总和并将该总和存储在文件中。对于 write open 中打开的文件,并使用 fprintf 将总和附加到数组文件。
fprintf(fptr, "Total sum of the array is %d", sum);//appending sum to the array file.
登录后复制
示例
<!--
现场演示
-->
#include#include#include#define max 100// Declaring the main function in the main header.int main(void){ srand(time(0)); int i; int sum = 0, num[max]; FILE *fptr; // Declaring the loop to generate 100 random numbers for(i = 0; i", sum); // appending sum to the array file. fclose(fptr); // closing the file pointer}
登录后复制
输出
Run 1: Total sum of the array is 5224Run 2: Total sum of the array is 5555Note: after executing a text file is created in the same folder with number.txtWe have to open it; there we can see the sum of random numbers.登录后复制
以上就是如何使用C编程中的文件计算0到100之间随机数的总和?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2585511.html