多维数组和qsort的大小 [英] size of multi-dimensional array and qsort
问题描述
有谁能告诉我如何计算以下
4维数组的大小,现在用qsort进行排序在这个阵列上?
double sp [3] = {4.0,5.0,6.0};
double spa [3] [2] = {
{4.0,2.0},
{5.0,8.0},
{6.0,6.0},
};
double spb [3] [2] [2] = {
{{1.0,2.0},{3.0,4.0}},>
{{5.0,6.0},{7.0,8.0}},
{{9.0,10.0},{11.0,12.0}},
} ;
// spc(时间,X,Y,Z)
double spc [3] [1] [1] [1] = {
{{{1.0}}},
{{{5.0}}},
{{{9.0}}},>
};
for(int t = 0; t< max_time; t ++)
for(int x = 0; x< max_x; x ++)
for(int y = 0; y< max_y; y ++)
for(int z = 0; z< max_z; z ++)
qsort (...);
谢谢
D
bsder写道:
任何人都可以告诉我如何计算以下
4维数组的大小,现在用qsort对这个数组进行排序?
double sp [3] = {4.0,5.0,6.0};
double spa [3] [2] = {
{4.0,2.0},
{ 5.0,8.0},
{6.0,6.0},
};
double spb [3] [2] [2] = {
{{1.0, 2.0},{3.0,4.0}},
{{5.0,6.0},{7.0,8.0}},
{{9.0,10.0},{11.0,12.0}},
} /
// spc(时间,X,Y,Z)
双spc [3] [1] [1] [1] = {
{{{1.0 }},
{{{5.0}}},
{{{9.0}}},
};
尺寸:
sizeof sp
sizeof spa
sizeof spb
sizeof spc
如果你想要每个第一维的元素数量,你可以使用
sizeof sp / sizeof sp [0]。
(int t = 0; t< max_time; t ++)
for(int x = 0; x< max_x; x ++)
for(int y = 0; y< max_y; y ++)
for(int z = 0; z< max_z; z ++)
qsort(...);
问题是:你想怎么排序?
你似乎想要沿着spc的最高维度排序:
如果是,传递spc [t] [x] [y]到qsort()以及比较
函数为double *来实现它。
如果不是,请定义order。在一个n维数组上,所有维度都是
但是一个固定或提供一维数组的转换和/或
以及相应的比较函数。
>
干杯
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
On Thu,2005年7月14日05:48:23 +0000,bsder写道:
任何人都可以告诉我如何计算以下
4维数组的大小,现在使用qsort对这个数组进行排序?
你需要通过排序来解释你的意思。一个4维数组。
排序本质上是一维过程,没有一种方法可以将4D
数组视为已排序。
double sp [3] = {4.0,5.0,6.0};
double spa [3] [2] = {
{4.0,2.0},
{5.0, 8.0},
{6.0,6.0},
};
double spb [3] [2] [2] = {
{{1.0,2.0} ,{3.0,4.0}},
{{5.0,6.0},{7.0,8.0}},
{{9.0,10.0},{11.0,12.0}},
};
// spc(时间,X,Y,Z)
double spc [3] [1] [1] [1] = {
{{{1.0}} },
{{{5.0}}},
{{{{{{{{}}},
};
case是相当明显的,因为数组是退化的:只有
一个维度的大小不是1.这里你可以写一下
qsort(spc,sizeof spc / sizeof * spc,sizeof * spc,compare);
当其他尺寸大于1,只要您只需将数组视为行的一维数组即可。每一行都发生在哪里?
本身就是一个数组。比较函数需要排序
,详细说明如何以有效的方式比较2个完整的行。
劳伦斯
Michael Mair写道:bsder写道:
任何人都可以告诉我如何计算以下四维数组的大小,现在用qsort对这个数组进行排序?双sp [3] = {4.0,5.0,6.0};
双人水疗[3] [2] = {
{4.0,2.0},
{5.0,8.0},
{6.0,6.0},
};
double spb [3] [2] [2] = {
{{1.0,2.0},{3.0,4.0}},
{{5.0,6.0}, {7.0,8.0}},
{{9.0,10.0},{11.0,12.0}},
};
// spc(时间,X,Y,Z )
double spc [3] [1] [1] [1] = {
{{{1.0}}},
{{{5.0}}},
{ {{9.0}}},
};
S. izes:
sizeof sp
sizeof spa
sizeof spb
sizeof spc
如果你想要每个第一维的元素数量,你可以使用
sizeof sp / sizeof sp [0]。
for(int t = 0; T< MAX_TIME;对于(int y = 0; y< max_y; y ++)
for(int z = 0; z< max_z) ; z ++)
qsort(...);
问题是:你想如何排序?
你似乎想要沿着最高排序spc的维度:
如果是,将spc [t] [x] [y]传递给qsort()以及double *的比较函数来实现它。
如果不是,则定义"顺序"在具有所有尺寸的n维阵列上,但是一个固定或提供与一维阵列的转换以及适当的比较功能。
干杯
迈克尔
我写了以下版本的传递4维数组到
函数进行打印,但传递
函数中的4维数组。
这是代码:
#include< stdio.h>
#include< stdlib.h>
#include< math.h>
void prn_sorted_distance(double * spc)
{
// qsort(spc,sizeof spc / sizeof * spc,sizeof * spc,compare);
}
void prn_distance(double * spb)
{
寄存器int a,b,c,t,x,y,z;
x = y = z = 0;
double tmp_x,tmp_y,tmp_z,distance,total;
int size_T = sizeof spb / sizeof spb [0];
int size_X = sizeof spb [0] / sizeof spb [0] [0];
int size_Y = sizeof spb [0] [0] / sizeof spb [0] [0] [0];
int size_Z = sizeof spb [ 0] [0] [0] / sizeof spb [0] [0] [0] [0];
int x_co_ord,y_co_ord,z_co_ord;
for(t = 0; t< SIZE_T; t ++){
for(x = 0; x< size_X; x ++){
tmp_x = 100.00-spb [t] [x] [y] [z] ;
// printf(" x:%6.1lf \ n",spb [t] [x] [y] [z]);
for( y = 0; y< size_Y; y ++){
tmp_y = 100.00-spb [t] [x] [y] [z];
// printf( " y:%6.1lf \ n",spb [t] [x] [y] [z]);
for(z = 0; z< size_Z; z ++){
printf(" z:%6.1lf;",spb [t] [x] [y] [z]);
tmp_z = 100.00-spb [t ] [x] [y] [z];
a = tmp_x * tmp_x;
b = tmp_y * tmp_y;
c = tmp_z * tmp_z;
总计= a + b + c;
距离= sqrt(总计);
printf(" distance:%6.1lf \ n",distance);
}
}
}
}
}
int main()
{
double spb [3] [1] [1] [1] = {
{{{1.0}}},
{{{5.0}}},
{{{9.0}}},
};
prn_distance(& spb);
// prn_sort_distance( spb);
返回1;
}
我没有实现比较功能。根据两点的距离比较将是
。但我需要首先通过引用解决传递
的数组。
谢谢
D
Hi,
Can anyone please tell me how to calculate the size of the following
4-dimensional array, and now to use qsort for sorting on this array?
double sp[3] = { 4.0, 5.0, 6.0 };
double spa[3][2] = {
{ 4.0, 2.0 },
{ 5.0, 8.0 },
{ 6.0, 6.0 },
};
double spb[3][2][2] = {
{ {1.0, 2.0}, {3.0, 4.0} },
{ {5.0, 6.0}, {7.0, 8.0} },
{ {9.0, 10.0 }, {11.0, 12.0} },
};
// spc(Time, X, Y, Z)
double spc[3][1][1][1] = {
{ {{1.0}} },
{ {{5.0}} },
{ {{9.0}} },
};
for (int t=0; t<max_time; t++)
for (int x=0; x<max_x; x++)
for (int y=0; y<max_y; y++)
for (int z=0; z<max_z; z++)
qsort(...);
Thanks
D
bsder wrote:Hi,
Can anyone please tell me how to calculate the size of the following
4-dimensional array, and now to use qsort for sorting on this array?
double sp[3] = { 4.0, 5.0, 6.0 };
double spa[3][2] = {
{ 4.0, 2.0 },
{ 5.0, 8.0 },
{ 6.0, 6.0 },
};
double spb[3][2][2] = {
{ {1.0, 2.0}, {3.0, 4.0} },
{ {5.0, 6.0}, {7.0, 8.0} },
{ {9.0, 10.0 }, {11.0, 12.0} },
};
// spc(Time, X, Y, Z)
double spc[3][1][1][1] = {
{ {{1.0}} },
{ {{5.0}} },
{ {{9.0}} },
};
Sizes:
sizeof sp
sizeof spa
sizeof spb
sizeof spc
If you want the number of elements per first dimension, you can use
sizeof sp/sizeof sp[0].
for (int t=0; t<max_time; t++)
for (int x=0; x<max_x; x++)
for (int y=0; y<max_y; y++)
for (int z=0; z<max_z; z++)
qsort(...);
The problem is: How do you want to sort?
You seem to want to sort along the highest dimension of spc:
If yes, pass spc[t][x][y] to qsort() along with a comparison
function for double* to achieve it.
If no, define "order" on a n-dimensional array with all dimensions
but one fixed or provide transformations to and from a 1D array
along with appropriate comparison functions.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
On Thu, 14 Jul 2005 05:48:23 +0000, bsder wrote:
Hi,
Can anyone please tell me how to calculate the size of the following
4-dimensional array, and now to use qsort for sorting on this array?
You''ll need to explain what you mean by "sorting" a 4-dimensional array.
Sorting is inherently a 1D process, there is no single way in which a 4D
array might be considered "sorted".
double sp[3] = { 4.0, 5.0, 6.0 };
double spa[3][2] = {
{ 4.0, 2.0 },
{ 5.0, 8.0 },
{ 6.0, 6.0 },
};
double spb[3][2][2] = {
{ {1.0, 2.0}, {3.0, 4.0} },
{ {5.0, 6.0}, {7.0, 8.0} },
{ {9.0, 10.0 }, {11.0, 12.0} },
};
// spc(Time, X, Y, Z)
double spc[3][1][1][1] = {
{ {{1.0}} },
{ {{5.0}} },
{ {{9.0}} },
};
In this case it is fairly obvious because the array is degenerate: only
one dimension has a size other then 1. Here you could write
qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
This works when other dimensions are greater than 1, as long as you are
just viewing the array as a 1D array of "rows" where each row happens to
be an array in its own right. The comparison function will need to sort
out the details of how to compare 2 complete rows in a valid way.
Lawrence
Michael Mair wrote:bsder wrote:Hi,
Can anyone please tell me how to calculate the size of the following
4-dimensional array, and now to use qsort for sorting on this array?
double sp[3] = { 4.0, 5.0, 6.0 };
double spa[3][2] = {
{ 4.0, 2.0 },
{ 5.0, 8.0 },
{ 6.0, 6.0 },
};
double spb[3][2][2] = {
{ {1.0, 2.0}, {3.0, 4.0} },
{ {5.0, 6.0}, {7.0, 8.0} },
{ {9.0, 10.0 }, {11.0, 12.0} },
};
// spc(Time, X, Y, Z)
double spc[3][1][1][1] = {
{ {{1.0}} },
{ {{5.0}} },
{ {{9.0}} },
};
Sizes:
sizeof sp
sizeof spa
sizeof spb
sizeof spc
If you want the number of elements per first dimension, you can use
sizeof sp/sizeof sp[0].
for (int t=0; t<max_time; t++)
for (int x=0; x<max_x; x++)
for (int y=0; y<max_y; y++)
for (int z=0; z<max_z; z++)
qsort(...);
The problem is: How do you want to sort?
You seem to want to sort along the highest dimension of spc:
If yes, pass spc[t][x][y] to qsort() along with a comparison
function for double* to achieve it.
If no, define "order" on a n-dimensional array with all dimensions
but one fixed or provide transformations to and from a 1D array
along with appropriate comparison functions.
Cheers
Michael
Hi, I wrote the following version of passing 4-dimensional array in to a
function for printing, but there is an error when passing a
4-dimensional array in to the function.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void prn_sorted_distance(double *spc)
{
//qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
}
void prn_distance(double *spb)
{
register int a,b,c, t,x,y,z;
x=y=z=0;
double tmp_x, tmp_y, tmp_z, distance, total;
int size_T = sizeof spb / sizeof spb[0];
int size_X = sizeof spb[0] / sizeof spb[0][0];
int size_Y = sizeof spb[0][0] / sizeof spb[0][0][0];
int size_Z = sizeof spb[0][0][0] / sizeof spb[0][0][0][0];
int x_co_ord, y_co_ord, z_co_ord;
for ( t = 0; t < size_T; t++ ) {
for ( x = 0; x < size_X; x++ ) {
tmp_x = 100.00-spb[t][x][y][z];
//printf("x: %6.1lf\n", spb[t][x][y][z]);
for ( y = 0; y < size_Y; y++ ) {
tmp_y = 100.00-spb[t][x][y][z];
//printf("y: %6.1lf\n", spb[t][x][y][z]);
for ( z = 0; z < size_Z; z++ ) {
printf("z: %6.1lf; ", spb[t][x][y][z]);
tmp_z = 100.00-spb[t][x][y][z];
a = tmp_x * tmp_x;
b = tmp_y * tmp_y;
c = tmp_z * tmp_z;
total = a + b + c;
distance = sqrt(total);
printf("distance: %6.1lf\n", distance);
}
}
}
}
}
int main()
{
double spb[3][1][1][1] = {
{ {{1.0}} },
{ {{5.0}} },
{ {{9.0}} },
};
prn_distance(&spb);
//prn_sort_distance(spb);
return 1;
}
I haven''t implement the function for comparison. The comparison will be
based on distance of two points. But I need to solve the array passing
by reference first.
Thanks
D
这篇关于多维数组和qsort的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!