-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_style_struct.c
More file actions
134 lines (98 loc) · 3.55 KB
/
Copy pathclass_style_struct.c
File metadata and controls
134 lines (98 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct DataFrame {
double ** _data_frame;
int n_rows;
int n_cols;
int row_ptr;
int col_ptr;
};
struct DataFrame create_dataframe(int n_rows, int n_cols) {
struct DataFrame df;
df.n_rows = n_rows;
df.n_cols = n_cols;
// Allocate memory for the 2d array
df._data_frame = (double**)malloc(df.n_rows * sizeof(double*));
// Allocate memory for the the columns
for (int i=0; i < df.n_cols; i++) {
*(df._data_frame + i) = (double*)malloc(df.n_cols * sizeof(double));
}
// Set the position pointers
df.row_ptr = 0;
df.col_ptr = 0;
return df;
}
void insert_data(struct DataFrame * df, double value) {
if (df->col_ptr <= df->n_cols-1 && df->row_ptr <= df->n_rows-1) {
// if there is still space in the current row & column
df->_data_frame[df->row_ptr][df->col_ptr] = value;
df->col_ptr++;
} else if (df->col_ptr == df->n_cols && df->row_ptr <= df->n_rows-1) {
// If the current column is filled but not the rows, move to the next row
df->row_ptr++;
df->col_ptr = 0;
df->_data_frame[df->row_ptr][df->col_ptr] = value;
df->col_ptr++;
} else if (df->col_ptr <= df->n_cols-1 && df->row_ptr == df->n_rows) {
// If we are in the last row but there's still room in the column
df->_data_frame[df->row_ptr][df->col_ptr] = value;
df->col_ptr++;
} else if (df->col_ptr == df->n_cols && df->row_ptr == df->n_rows) {
printf("DataFrame IS FILLED TO CAPACITY");
// Create a temp 2d array with the new size, copy over and delete the old array
int temp_n_rows = df->n_rows + 1;
// Allocate the new memory
double **temp = (double**)malloc(temp_n_rows * sizeof(double*));
for (int i=0; i < df->n_cols; i++) {
*(temp + i) = (double*)malloc(df->n_cols * sizeof(double));
}
// Write the data into temp array
for (int i=0; i < df->n_rows; i++) {
for (int j=0; j < df->n_cols; j++) {
*(*(temp + i) + j) = *(*(df->_data_frame + i) + j);
}
}
df->_data_frame = temp;
// deallocate temp memory
// for (int i=0; i < temp_n_rows; i++) {
// free(temp[i]);
// }
// free(temp);
// Set new row size
df->n_rows = temp_n_rows;
df->row_ptr++;
df->col_ptr = 0;
*(*(df->_data_frame + df->row_ptr) + df->col_ptr) = value;
df->col_ptr++;
}
else{
printf("DataFrame IS FILLED TO CAPACITY");
}
}
int main() {
int row = 4, col = 4;
struct DataFrame df_1 = create_dataframe(row, col);
insert_data(&df_1, 10000.99);
insert_data(&df_1, 500000.99);
insert_data(&df_1, 700000.99);
insert_data(&df_1, 800000.99);
insert_data(&df_1, 900000.99);
insert_data(&df_1, 80.99);
insert_data(&df_1, 1.99);
insert_data(&df_1, 840.99);
insert_data(&df_1, .99);
insert_data(&df_1, 129.99);
insert_data(&df_1, 1.99);
insert_data(&df_1, 4.99);
insert_data(&df_1, 11.99);
insert_data(&df_1, 8444.99);
insert_data(&df_1, 23.99);
insert_data(&df_1, 1000000000.99);
// insert_data(&df_1, 0.959);
printf("df_1[3][0] = %f\n", *(*(df_1._data_frame + 3) + 3));
printf("Get row pointer: %d\n", df_1.row_ptr);
printf("Get col pointer: %d\n", df_1.col_ptr);
printf("Hello, World!\n");
return 0;
}