![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #16 |
| Registered User Join Date: Sep 2006
Posts: 2,504
| What you want to do is a simple, multi-key sort - which computers have been doing since day 1 after they were sorting. All your "Rube Goldberg" machinations, are just stopping/slowing your effort. Try and strip the work down, into it's smallest necessary parts. It's all you need, and it's also all you should want. |
| Adak is offline | |
| | #17 | |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Quote:
Code: for (i = (b - 1); i >= a; i--)
{
for (j = (a + 1); j <= i; j++)
...
| |
| itCbitC is offline | |
| | #18 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| Quote:
There's nothing really weird or wrong about using a variable as a loop counter multiple times. 'i' can be always "the current loop's index" in a function, and it's usage is perfectly clear. It would certainly be bad to use the same variable for completely different purposes in different places in the same function, of course. You wouldn't use the same variable as a loop counter, then an intermediate calculation result, then a flag, then to hold a dynamically allocated array length etc... (register allocation paranoia, perhaps) But declaring a separate variable for each loop counter, up front, is almost as bad, when you name them like that, and this bug shows one of the reasons why, firsthand.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger | |
| iMalc is offline | |
| | #19 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| Just wondering... Why have you used a structure of arrays rather than an array of structures? Switching to an array of structures would reduce the length of that program by at least 30 lines of code on its own.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is offline | |
| | #20 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Consider moving the bubblesort code snippet into its own separate function. It'll improve clarity as you won't need three or four i's and reduce the code size. |
| itCbitC is offline | |
| | #21 | |
| Registered User Join Date: Mar 2009
Posts: 12
| Quote:
-------------------------- Here's my code at this point: Code: #include <stdlib.h>
#include <stdio.h>
struct record
{
double *x;
double *y;
double *z;
double *t;
};
int compare( double z1, double time1, double z2, double time2 )
{
if( z1 < z2 ) return -1;
if( z1 > z2 ) return 1;
if( time1 < time2 ) return -1;
if( time1 > time2 ) return 1;
return 0;
}
int main(int argc, char *argv[])
{
FILE *ifp, *ofp;
ifp = fopen(argv[1], "r"); // Input data file
ofp = fopen(argv[2], "w"); // Output data file
int c; // counter
int a, b; // to store the number in the bins
/* Used for sorting the data */
int i, j;
double temp;
double temps;
double temp3;
/* z sorting */
double s1;
double s2;
double s3;
/* 1st bin*/
double ss1;
double ss2;
double ss3;
/* 2nd bin*/
double sss1;
double sss2;
double sss3;
/* Structure */
struct record tmp;
/* file formats to be read in */
double *x;
double *y;
double *z;
double *t;
const int max_el = 40; // Max array size
int al = 0; // Actual array length
int ap = 0; // array pointer
/* Memory Allocation */
tmp.x = malloc(max_el * sizeof(double));
tmp.y = malloc(max_el * sizeof(double));
tmp.z = malloc(max_el * sizeof(double));
tmp.t = malloc(max_el * sizeof(double));
while(!feof(ifp)) {
al = al + 1;
fscanf(ifp, "%lf,%lf,%lf,%lf\n",
&tmp.x[ap], &tmp.y[ap], &tmp.z[ap],
&tmp.t[ap]);
ap = ap + 1;
}
fclose(ifp);
a = 0; /* Initialize the counters */
b = 0;
/* z bin counting */
for (c = 0; c < al; c++) {
if ((tmp.z[c] >= 0.0)&&(tmp.z[c] <= 100))
a += 1;
else if (tmp.z[c] > 100)
b += 1;
}
/***********************************************/
/* Bubble Sort to sort the data by z */
for (i = (al - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (tmp.z[j-1] > tmp.z[j])
{
temp = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = temp;
s1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = s1;
s2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = s2;
s3 = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = s3;
}
}
}
/***************************************/
/* first bin */
for (i = (a - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temp3 = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temp3;
ss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = ss1;
ss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = ss2;
ss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = ss3;
}
}
}
/* second bin */
for (i = (b - 1); i >= a; i--)
{
for (j = (a + 1); j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temps = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temps;
sss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = sss1;
sss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = sss2;
sss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = sss3;
}
}
}
printf("Sorting complete \n");
fprintf(ofp,"Data Sorted\n");
fprintf(ofp,"The amount in each z bin:\n");
fprintf(ofp,"%d, %d\n", a, b);
fprintf(ofp,"time, z, y, x \n");
for (i = 0; i < a; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
fprintf(ofp,"***************\n");
for (i = a; i < al; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
free(tmp.x);
free(tmp.y);
free(tmp.z);
free(tmp.t);
fclose(ofp);
return 0;
}
Code: time, z, y, x 1.000000, 1.000000, 2.000000, 15.000000 5.000000, 1.000000, 2.000000, 5.000000 6.000000, 11.000000, 22.000000, 5.000000 10.000000, 11.000000, 52.000000, 5.000000 3.000000, 13.000000, 2.000000, 45.000000 7.000000, 15.000000, 42.000000, 5.000000 8.000000, 51.000000, 2.000000, 5.000000 *************** 4.000000, 111.000000, 32.000000, 5.000000 2.000000, 122.000000, 2.000000, 25.000000 9.000000, 221.000000, 2.000000, 5.000000 ![]() Pretty confident right now the problem lies in the condition statements for the 2nd bins sorting: IE Code: /* second bin */
for (i = (b - 1); i >= a; i--)
{
for (j = (a + 1); j <= i; j++)
{
Looking to see if there's way to say, directly for the array records, that after the 7 array record resort again for t? Last edited by badtwistofate; 06-11-2009 at 10:31 AM. | |
| badtwistofate is offline | |
| | #22 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Code: if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1) Code: if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1) |
| tabstop is offline | |
| | #23 |
| Registered User Join Date: Mar 2009
Posts: 12
| THINK I FIXED IT!! Code:
/* first bin */
for (i = (a - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temp3 = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temp3;
ss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = ss1;
ss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = ss2;
ss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = ss3;
}
}
}
/* second bin */
for (i = (a + b); i >= b; i--)
{
for (j = (a + 1); j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temps = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temps;
sss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = sss1;
sss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = sss2;
sss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = sss3;
}
}
}
printf("Sorting complete \n");
fprintf(ofp,"Data Sorted\n");
fprintf(ofp,"The amount in each z bin:\n");
fprintf(ofp,"%d, %d\n", a, b);
fprintf(ofp,"time, z, y, x \n");
for (i = 0; i < a; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
fprintf(ofp,"***************\n");
for (i = a + 1; i <= al; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
Code: for (i = (b - 1); i >= b; i--)
{
for (j = (a + 1); j <= i; j++)
so by changing it so: Code: for (i = (a + b); i >= b; i--)
{
for (j = (a + 1); j <= i; j++)
BUT... Now i tested that if the 1st bin has less amount then the 2nd bin it fails... YUCK Last edited by badtwistofate; 06-11-2009 at 10:43 AM. |
| badtwistofate is offline | |
| | #24 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Perhaps this Code: /* second bin */
for (i = (a+b-1); i >= a; i--)
{
for (j = (a + 1); j <= i; j++)
{
...
Last edited by itCbitC; 06-11-2009 at 11:29 AM. Reason: Tags |
| itCbitC is offline | |
| | #25 |
| Registered User Join Date: Mar 2009
Posts: 12
| |
| badtwistofate is offline | |
| | #26 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | ||
| MK27 is offline | |
| | #27 | |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Quote:
Code: /* first bin */
for (i = (a - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (tmp.t[j-1] > tmp.t[j])
{
...
/* second bin */
for (i = (a + b - 1); i >= a; i--)
{
for (j = (a + 1); j <= i; j++)
{
if (tmp.t[j-1] > tmp.t[j])
{
...
| |
| itCbitC is offline | |
![]() |
| Tags |
| array, sort, struct |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with arrays structures sorting. | pitifulworm | C Programming | 42 | 02-09-2009 12:31 PM |
| Homework problem...structures or arrays? | tortan | C++ Programming | 21 | 08-30-2006 01:26 AM |
| problem with arrays and structures | gell10 | C++ Programming | 7 | 11-03-2003 12:02 AM |
| pointers to arrays of structures | terryrmcgowan | C Programming | 1 | 06-25-2003 09:04 AM |
| Methods for Sorting Structures by Element... | Sebastiani | C Programming | 9 | 09-14-2001 12:59 PM |