Почему мой код не тестирует более одного раза? Для петель в C - PullRequest
0 голосов
/ 16 апреля 2020

Вывод моей программы печатается только один раз. Я использую для l oop в начале моей программы, чтобы проверить его 100 раз, то есть numOfTests. Я могу появиться, чтобы проверить это только один раз.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int randomInt(int max);
int find_maximum(int[], int);
double average(int[], int);

int main() {
    int i, j, k, x, y, z, total = 0, numberOfHops = 0, test;
    const int L = 3;
    const int totalHops=1000;
    const int N = L*L*L; 
    const int numOfTests = 100;
    int measureOfHops[numOfTests] = {};

    for (test = 0; test<numOfTests; test++) {
        int sites[L][L][L] = {};
        for (i = 0; i<L; i++) { // each site has unique value from 1 to N // 
            for (j =0; j<L; j++) {
                for (k = 0; k<L; k++) {
                    total++;
                    sites[i][j][k] = total;
                }
            }
        } // printf("%d at (L,L,L)\n", sites[0][0][0]);
        int sitesVisited = 0;
        int tracker[totalHops] = {0};
        int memory[N] = {0};
        int seed = time(NULL);
        srandom(seed);
        //printf("The seed value for this random walk is : %d\n", seed);
        //printf("Initial location of the particle is at (%d,%d,%d)\n", x, y, z);
        x = 0, y = 0, z = 0; // initialising location on particle // 
        tracker[0] = sites[x][y][z]; // store first site visited //
        for (int i = 1; i <= totalHops; i++) // the random walk //
        {
            int direction = randomInt(6); // six possible directions to move //
            // along x //
            if (direction == 0) { // move in negative direction //
                x -= 1;
                if (x == -1)
                {
                    x = L-1;
                }
            }
            if (direction == 1) { // move in positive direction //
                x += 1;
                if (x == L) 
                {
                    x = 0;
                }
            }
            // along y //
            if (direction == 2) { // move in negative direction //
                y -= 1;
                if (y == -1)
                {
                    y = L-1;
                }
            }
            if (direction == 3) { // move in positive direction //
                y += 1;
                if (y == L) 
                {
                    y = 0;
                }
            }
            // along z //
            if (direction == 4) { // move in negative direction //
                z -= 1;
                if (z == -1)
                {
                    z = L-1;
                }
            }
            if (direction == 5) { // move in positive direction //
                z += 1;
                if (z == L) 
                {
                    z = 0;
                }
            }
            tracker[i] = sites[x][y][z]; // tracker stores all sites visited during walk //
            // printf("After %d hops, new location at (%d,%d,%d) site name %d\n", i, x, y, z, tracker[i]);
        }
        for (i = 1; i <= N; i++) // want to check that all sites from 1 to N are in tracker // 
        {
            k = 0;
            numberOfHops = 0; // initializing values for each i //
            do {
                if (i == tracker[k])
                {
                    sitesVisited +=1;
                    numberOfHops = k;
                    memory[i] = numberOfHops;
                    // printf("Number of hops: %d, for i = %d\n", memory[i], i);
                }
                if (k > totalHops) {
                    sitesVisited = N + 1;
                    break;
                } 
                k++;
            }
            while ( sitesVisited < i);
        }
        if (sitesVisited == N) {
            printf("Particle took %d hops to explore environment.\n", find_maximum(memory, N));
            measureOfHops[test] = find_maximum(memory, N);
            printf("Test %d, hops needed %d\n", test, measureOfHops[test]);
        }       
    }
}

// function definition //
int randomInt(int max) // generate a random int //
{
    return (random() % max);
}
int find_maximum(int a[], int n) { // find max value in an array //
  int c, max, index;
  max = a[0];
  index = 0;
  for (c = 1; c < n; c++) {
    if (a[c] > max) {
       index = c;
       max = a[c];
    }
  }
  return max;
}
double average(int a[], int n) // Function that return average of an array. 
{ 
    double sum = 0.0; 
    for (int i=0; i<n; i++) 
       sum += a[i]; 
    return sum/n; 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...