fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <inttypes.h>
  4.  
  5.  
  6. //////////////////////////////////////
  7.  
  8. typedef struct {
  9.  
  10. int group[8];
  11. uint64_t points;
  12.  
  13. } BestGroup;
  14.  
  15. //////////////////////////////////////
  16.  
  17. typedef struct {
  18. BestGroup *array;
  19. size_t used;
  20. size_t size;
  21. } Array;
  22.  
  23. void initArray(Array *a, size_t initialSize) {
  24. a->array = (BestGroup *)malloc(initialSize * sizeof(BestGroup));
  25. a->used = 0;
  26. a->size = initialSize;
  27. }
  28.  
  29. void insertArray(Array *a, int *group_add, uint64_t points_add) {
  30. // a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed.
  31. // Therefore a->used can go up to a->size
  32. if (a->used == a->size) {
  33. a->size *= 2;
  34. a->array = (BestGroup *)realloc(a->array, a->size * sizeof(BestGroup));
  35. }
  36.  
  37. int i;
  38.  
  39. for (i = 0; i < 8; i++)
  40. {
  41. a->array[a->used].group[i] = group_add[i];
  42. }
  43. a->array[a->used].points = points_add;
  44. a->used++;
  45. }
  46.  
  47. void freeArray(Array *a) {
  48. free(a->array);
  49. a->array = NULL;
  50. a->used = a->size = 0;
  51. }
  52.  
  53. ///////////////////////////////////////////////////////////////////////////////
  54.  
  55. int main()
  56.  
  57. {
  58. Array a;
  59. int i;
  60. int list[8] = {0, 1, 2, 2, 4, 5, 6, 7};
  61.  
  62. initArray(&a, 5); // initially 5 elements
  63. for (i = 0; i < 100; i++)
  64. insertArray(&a, list, i); // automatically resizes as necessary
  65. printf("%d\n", a.array->group[1]); // print 2nd element
  66. printf("%lu\n", a.used); // print number of elements
  67. freeArray(&a);
  68. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
1
100