fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define MAX_ROWS 5000
  7. #define MAX_COLUMNS 5000
  8.  
  9. struct array
  10. {
  11. int rows;
  12. int columns;
  13. int order;
  14. char *base_pointer; /* pointer to array of bytes. In this array, float numbers will be stored (4 bytes each) */
  15. };
  16. struct array* initialize(int rows, int columns, int order)
  17. {
  18. int i,
  19. size = columns * rows * sizeof(float);
  20. /* Allocate the required memory for the 2D array to store float values (Ex: 1.45) with "rows" and "columns" */
  21.  
  22. // If you want to allocate both the structure and the internal storage in one malloc:
  23. /*
  24.   struct array* array = (struct array*) malloc(sizeof(struct array) + size);
  25.   array->base_pointer = &((char*)array)[sizeof(struct array)];
  26.   */
  27.  
  28. struct array* array = malloc(sizeof(struct array));
  29. if(!array) {
  30. return 0; // error
  31. }
  32. array->base_pointer = malloc(size);
  33. if(!array->base_pointer) {
  34. return 0; // error
  35. }
  36.  
  37. for(i = 0; i < size; i++) {
  38. array->base_pointer[i] = 0;
  39. }
  40. /* Make sure the size is within 1 to MAX_ROWS and 1 to MAX_COLUMNS specified in main.c. If not return null pointer */
  41. array->rows = rows;
  42. array->columns = columns;
  43. array->order = order;
  44. /* Initialize the 2D array to the all zeroes (0.0) */
  45. /* Assign suitable values to all the elements of the structure and return the struct pointer */
  46. return array;
  47. }
  48.  
  49. void insert(struct array* arr, int row, int column, float value) {
  50. float* floatArr = (float*)arr->base_pointer;
  51. floatArr[(column * arr->rows) + row] = value;
  52. }
  53.  
  54. float retrieve(struct array* arr, int row, int column) {
  55. float* floatArr = (float*)arr->base_pointer;
  56. return floatArr[(column * arr->rows) + row];
  57. }
  58.  
  59. int main() {
  60. struct array* arr = initialize(3, 4, 42);
  61.  
  62.  
  63. int i, row, column;
  64. float f = 0.0;
  65. for(column = 0; column < arr->columns; column++) {
  66. for(row = 0; row < arr->rows; row++) {
  67. insert(arr, row, column, f);
  68. f = f + 1.0;
  69. }
  70. }
  71.  
  72. // Check the array's fields
  73. printf("arr->rows = %i\narr->columns = %i\narr->order = %i\n", arr->rows, arr->columns, arr->order);
  74. // print the array linearly
  75. for(i = 0; i < arr->columns*arr->rows; i++) {
  76. printf("((float*)arr->base_pointer)[%i] = %f\n", i ,((float*)arr->base_pointer)[i]);
  77. }
  78.  
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
arr->rows = 3
arr->columns = 4
arr->order = 42
((float*)arr->base_pointer)[0] = 0.000000
((float*)arr->base_pointer)[1] = 1.000000
((float*)arr->base_pointer)[2] = 2.000000
((float*)arr->base_pointer)[3] = 3.000000
((float*)arr->base_pointer)[4] = 4.000000
((float*)arr->base_pointer)[5] = 5.000000
((float*)arr->base_pointer)[6] = 6.000000
((float*)arr->base_pointer)[7] = 7.000000
((float*)arr->base_pointer)[8] = 8.000000
((float*)arr->base_pointer)[9] = 9.000000
((float*)arr->base_pointer)[10] = 10.000000
((float*)arr->base_pointer)[11] = 11.000000