Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

Cプログラミングのファイルを使用して0〜100の乱数の合計を計算するにはどうすればよいですか?


このプログラムでは、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];
}

まず、乱数の合計を計算し、その合計をファイルに保存します。その開いているファイルを書き込みで開き、fprintfを使用して合計を配列ファイルに追加します。

fprintf(fptr, "Total sum of the array is %d\n", sum);
//appending sum to the array file.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#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 <=99; i++){
      // Storing random numbers in an array.
      num[i] = rand() % 100 + 1;
      // calculating the sum of the random numbers.
      sum+= num[i];
   }
   // intializing the file node with the right node.
   fptr = fopen("numbers.txt", "w");
   // cheching if the file pointer is null, check if we are going to exit or not.
   if(fptr == NULL){
      printf("Error!");
      exit(1);
   }
   fprintf(fptr, "Total sum of the array is %d\n", sum); // appending sum to the array file.
   fclose(fptr); // closing the file pointer
}

出力

Run 1: Total sum of the array is 5224
Run 2: Total sum of the array is 5555
Note: after executing a text file is created in the same folder with number.txt
We have to open it; there we can see the sum of random numbers.

  1. Excelで2つの数値の差の割合をどのように計算しますか

    パーセンテージの差は通常、2つの数値のパーセンテージの差を知りたいときに計算されます。 Microsoft Office Excelは、使い始めるのに役立つ優れたツールです。この投稿では、パーセンテージの差を見つける方法を紹介します。 Excelの2つの数字の間。 Excelで2つの数値の差のパーセンテージを計算する パーセンテージの変化またはパーセンテージの差は、時間の経過とともに測定する任意の量に適用できます。これは、時間の経過に伴う変化の程度を表す単純な数学的概念です。簡単な数式を使用すると、2つの数値の差のパーセンテージを見つけるのに役立ちます。方法は次のとおりです! 1]値を入力

  2. SHAREit を使用してラップトップ間でデータ/ファイルを転送する方法

    コンピュータ間のデータ共有が退屈な作業であった時代がありました。ほとんどの場合、データ転送を行うために、USB フラッシュ ドライブ、ハードディスク、または LAN ケーブルを持ち運ぶ必要がありました。 しかし、スマートな世界では、コンピューター間でデータを共有できる無料のアプリケーションが数多くあり、SHAREit はその 1 つです。このアプリを使用すると、Bluetooth の 200 倍の速さでデータを共有できます。このアプリを使用するために、データ プランや WiFi ネットワークは必要ありません。 この記事では、SHAREit プログラムを使用して、あるラップトップから別のラッ