Thread: Help with circular array of structures

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Help with circular array of structures

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct variables {
    
        int time[100];
        int brakefluid[100];
        int diskpressure[100];
        int driverpressure[100];
        int power[100];
    
        //x[i%100]
    
    };
    
    
    int main()
    {
    
    FILE *fout, *finrecorded;
    fout=fopen("variables.txt", "w");
    finrecorded=fopen("variables.txt", "r");
    
    
        int count = 0;
        int i = 0;
    
        //int population[1000];
    
        for (i=0; i<1000; i++)
        {
    
            variables.time[i] = i;
            brakefluid[i] = rand()%100;
            diskpressure[i] = rand()%100;
            driverpressure[i] = rand()%100;
            power[i] = rand()%100;
    
            count++;
        }
    
    
    
            while(!feof(finrecorded)) {
            fscanf(finrecorded, "%d ", &population[i]); // Populates the array with floating points from the file
            i++;
            count++;
    
        }
    
    
    
        printf("Time  Brake Fluid  Disk Pressure  Driver Pressure  Power to unit");
    
        for(i = 1; i < 100; i++) { // start at 1 because -1 was still getting input into the array because I was using the count instead of a size variable
    
        fprintf(fout, "%d ", population[i]);
    
        finrecorded=fopen("variables.txt", "r");
    
        printf(" %d        %d           %d              %d               %d ", population[i]);
        printf("\n");
        }
    
    fclose(fout);
    fclose(finrecorded);
    
        return 0;
    }
    Obviously there are a lot of problems with this code, but the only thing I really need help with is how to appropriately set up the structure I am using (struct variables) and fill these values. This is supposed to store the last 100 values for these 5 variables in a 1000 count loop. I don't know how to create the circular loop I have been assigned that will continue to overwrite itself until 1000 loops have occurred.

    Thanks in advance for any help with this problem. Please don't try and point out other flaws in this code it will be fixed once I have my circular loop properly filling this structure.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    still stuck

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct variables {
    
        int time;
        int brakefluid;
        int diskpressure;
        int driverpressure;
        int power;
    
    };
    
    
    int main()
    {
    
    FILE *fout, *finrecorded;
    fout=fopen("variables.txt", "w");
    finrecorded=fopen("variables.txt", "r");
    
    
        int count = 0;
        int i = 0;
    
        struct variables crash[1000];
    
        for (i=0; i<1000; i++)
        {
    
            crash[i].time = i;
            crash[i].brakefluid = rand()%100;
            crash[i].diskpressure = rand()%100;
            crash[i].driverpressure = rand()%100;
            crash[i].power = rand()%100;
    
            count++;
        }
    
    
    
            //while(!feof(finrecorded)) {
            //fscanf(finrecorded, "%d ", &population[i]); // Populates the array with floating points from the file
            //i++;
            //count++;
    
        //}
    
    
    
        printf("Time  Brake Fluid  Disk Pressure  Driver Pressure  Power to unit");
    
        for(i = 1; i < 1000; i++) { // start at 1 because -1 was still getting input into the array because I was using the count instead of a size variable
    
        fprintf(fout, " %d %d %d %d %d ", crash[i].time, crash[i].brakefluid, crash[i].diskpressure, crash[i].driverpressure, crash[i].power);
    
        finrecorded=fopen("variables.txt", "r");
    
        printf(" %d %d %d %d %d ", crash[i].time, crash[i].brakefluid, crash[i].diskpressure, crash[i].driverpressure, crash[i].power);
        printf("\n");
        }
    
    fclose(fout);
    fclose(finrecorded);
    
        return 0;
    }
    Okay so I now have the correct setup for my structure with the five variables. Everything is now working the way it should except that I still don't understand how to get the loop to behave in a circular fashion replacing the variables with only the latest 100 iterations of the 1000 iteration loop.
    Any help would be greatly appreciated.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    To keep only the last 100 events...

    Code:
    struct variables crash[100];
    
     crash[i % 100 ].time = i; 
     // etc.
    The catch is that you have to keep track of where the last event was written... if the last event was #49, to get them into "linear time" you have to begin reading at #50 to 99 then 0 to 49....

    Code:
    current = i +1;
    
    while ( current != i )
       {
         printf("Time : %d " crash[current].time;);
         // etc.
        current = (current + 1) % 100; 
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue with a circular array... nitpick !
    By manasij7479 in forum C++ Programming
    Replies: 18
    Last Post: 10-02-2011, 08:16 PM
  2. Circular Array Implementation
    By jturner38 in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2010, 11:44 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Circular array
    By Opel_Corsa in forum C++ Programming
    Replies: 5
    Last Post: 02-16-2006, 04:15 PM
  5. Circular array list
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2004, 02:28 PM