Unix Programming - comparison in a 2D array

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2005 > comparison in a 2D array





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author comparison in a 2D array
bsder

2005-07-14, 7:48 am

Hi,

I feel abit trouble creating a "compare" function for the comparison of
each row in an 2D arrays. Is there any simple example I can follow?
Here is what I got now:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void prn_sorted_distance(double spc[][4], int n)
{
//qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
printf("size_spc*: %d\n", sizeof *spc);
qsort(spc, n, sizeof *spc, compare);
}

void prn_distance(double spb[][4], int n)
{
register int x,y,z, t,coor=0;
double distance, total;
int size_coor = sizeof spb[0] / sizeof spb[0][0];
double tmp[size_coor];
if (n == 0 || size_coor < 3)
return;

for ( t = 0; t < n; t++ ) {
for ( coor = 0; coor < size_coor; coor++ ) {
printf("point: %6.1lf; ", spb[t][coor]);
tmp[coor] = 100.00-spb[t][coor];
}
x = tmp[0] * tmp[0];
y = tmp[1] * tmp[1];
z = tmp[2] * tmp[2];
distance = sqrt(x+y+z);
printf("distance: %6.1lf\n", distance);
}
}

int main()
{
double spb[3][4] = {
{1.0, 2.0, 1.0, 3.0},
{8.0, 3.0, 12.0, 8.0},
{4.0, 7.0, 2.0, 5.0}
};

int size_elem = sizeof spb / sizeof spb[0];
prn_distance(spb, size_elem);
printf("--------------------------\n");
printf("size_spc: %d\n", sizeof spb/sizeof *spb);
printf("size_spc*: %d\n", sizeof *spb);
prn_sorted_distance(spb, size_elem);

return 1;
}

Thanks
Maxim Yegorushkin

2005-07-15, 7:52 am

On Thu, 14 Jul 2005 17:03:11 +0400, bsder <bsder@bsder.com> wrote:

> I feel abit trouble creating a "compare" function for the comparison of
> each row in an 2D arrays. Is there any simple example I can follow?


Try another newsgroup like comp.lang.c or comp.lang.c++. This group is
related to unix specific programming.

--
Maxim Yegorushkin
<firstname.lastname@gmail.com>
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com