По какой-то причине мой внешний цикл for, похоже, ничего не делает, я проверил весь парантез, и все выглядит нормально, но он все еще не зацикливается.
С помощью этой программы я хочу суммировать 50 случайных чисел (числа могут быть 1 или -1 ... это для задачи вычислительной физики) и вывести величину, которую делает программа. НО я хочу пойти дальше и сделать это 10 раз, и рассчитать среднюю величину.
Я знаю, что мне нужно сделать, у меня просто проблема с этим циклом.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h> //Neeed this to seed the random generator with time, or else it will always generate the same numbers.
//This is a program to calculate the magnitude of the displacement of a particle after random collisions.
#define RAND_MAX 1
int main()
{
//using ints because the particle can only move one unit on x-axis at a time.
int i, x, displacement, sum = 0, avg;
int total_disp=0, mag_disp;
srand(time(NULL));
//Each collision causes the particle to advance or retreat by one unit on the x axis.
for(i=0; i<10; i++)
{
for (i=0; i<50; i++) //for 50 collisions
{
x = rand() % 2; //generates random numbers between 0 and 1.
if (x==0) //if x is 0 then it was a displacement in the minus x direction
{
displacement = -1;
}
else { //if x is 1 it is a displacement in the minus x direction
displacement = 1;
}
printf("the disp is : %d\n", displacement);
total_disp = total_disp + displacement; //sum the displacements
}
if (total_disp < 0) {
mag_disp = total_disp * -1;
}
else{ mag_disp = total_disp; }
printf("The total displacement is: %d\n", mag_disp);
sum = sum + mag_disp; //sum of the displacement magnitudes, there should be ten of them in this case
avg = sum / i; //average displacement is the sum of all the magnitudes divded by the number of times this is performed.
printf("The average displacement for i = %d particles is: %d", i, avg);
}
return 0;
}