I need to make a function which can accept one-dimensional arrays and two-dimensional arrays. I also need to do that with single pointer as argument of this function.
If someone will ask, I solved my problem with double pointer as argument on function matrix
, but now I have to do this in other way.
I was thinking about passing array of pointers and in my function assign every pointer (of row) to temporary one-dimensional array. I'm not familiar with C language, so maybe I did some stupid mistake, but I believe tmpTab = &tab[i];
this line of code should work, but it doesn't, because I get junk on output.
void matrix(double* tab, int n, int l) {
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
int i, j, isCol = 1, isRow = 1;
double *tmpTab;
if (l >= n || l < 0) { isRow = 0; }
if (l >= n || l < 0) { isCol = 0; }
for (i = 0; i < n; i++) {
tmpTab = &tab[i];
for (j = 0; j < n; j++) {
printf("\n%i: %lf", j, tab[j]);
if (tab[j] < S || tab[j] > E) count1++;
if (isRow && i == l && tab[j] >= S && tab[j] <= E) count2++;
if (isCol && j == l && tab[j] >= S && tab[j] <= E) count3++;
if (i == j && tab[j] >= S && tab[j] <= E) count4++;
if (i > j && tab[j] >= S && tab[j] <= E) count5++;
if (i + j == n - 1 && tab[j] >= S && tab[j] <= E) count6++;
}
}
}
Function to allocate my array:
double **allocate_array(int n) {
double **tab;
tab = (double **)malloc(sizeof(double) * n);
for (int i = 0; i < n; i++) {
tab[i] = (double *)malloc(sizeof(double) * n);
}
return tab;
}
Main:
int main()
{
double **tab;
tab = read_array("dane.txt", &N);
print_array(tab, N);
matrix((double *)tab, N, 2);
}
Read from file works properly, print_array also. This is output of my program for research.
3.30 2.30 2.10 3.30 3.40 3.30 2.30 2.10 3.30 3.40 3.30 2.30 2.10 3.30 3.40 3.30 2.30 2.10 3.30 3.40 3.60 2.60 2.60 3.60 3.50 0: 0.000000 1: 0.000000 2: -6277435991103237715486889255272272650483169294881725067238215516160.000000 3: -6277438562204192487878988888393020692503707483087375482269988814848.000000 4: -6277438562204192487878988888393020692503707483087375482269988814848.000000 0: 0.000000 1: 0.000000 2: -6277435991103237715486889255272272650483169294881725067238215516160.000000 3: -6277438562204192487878988888393020692503707483087375482269988814848.000000 4: -6277438562204192487878988888393020692503707483087375482269988814848.000000 0: 0.000000 1: 0.000000 2: -6277435991103237715486889255272272650483169294881725067238215516160.000000 3: -6277438562204192487878988888393020692503707483087375482269988814848.000000 4: -6277438562204192487878988888393020692503707483087375482269988814848.000000 0: 0.000000 1: 0.000000 2: -6277435991103237715486889255272272650483169294881725067238215516160.000000 3: -6277438562204192487878988888393020692503707483087375482269988814848.000000 4: -6277438562204192487878988888393020692503707483087375482269988814848.000000 0: 0.000000 1: 0.000000 2: -6277435991103237715486889255272272650483169294881725067238215516160.000000 3: -6277438562204192487878988888393020692503707483087375482269988814848.000000 4: -6277438562204192487878988888393020692503707483087375482269988814848.000000