
在这个程序中,我们添加了 0 到 100 之间生成的随机数。
每次运行后,随机数之和的结果都是不同的,即,我们得到不同的结果每次执行。
我们用来计算 0 到 100 之间的随机数之和的逻辑是 –
for(i = 0; i <=99; i++){ // Storing random numbers in an array. num[i] = rand() % 100 + 1; // calculating the sum of the random numbers. sum+= num[i];}
首先,我们计算随机数的总和并将该总和存储在文件中。对于 write open 中打开的文件,并使用 fprintf 将总和附加到数组文件。
fprintf(fptr, "Total sum of the array is %d", sum);//appending sum to the array file.
示例
<!–
现场演示
–>
#include#include#include
输出
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之间随机数的总和?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1443963.html
微信扫一扫
支付宝扫一扫