-
Sorting Problems
Ok I dont know what I am doing wrong, I read through about 30 threads on here and tried googling for something that might help me out.
I found one with a problem pretty close to mine and I tried out using what worked for that person (strcmp) but that makes the output even worse then what it is for what i have now.
If I comment out this function the program works ok, it calculates the summary fine, prints everything out fine but as soon as I compile and run with this in it ... I get some strange outputs and only the first few records are in order. I am really lost as to what I have done wrong here.
I know there are tons of questions already posted about bubble sorting and I apologize for having to ask yet another ;p
Code:
void SortArrs ()
{
for(i=0; i<RECORDCOUNT-1; i++)
for(j=0; j<RECORDCOUNT-1; j++)
if (FNAME[i]>FNAME[j])
{
strcpy(temp, FNAME[i]);
strcpy(FNAME[i], FNAME[j]);
strcpy(FNAME[j], temp);
strcpy(temp, LNAMEARR[i]);
strcpy(LNAMEARR[i], LNAMEARR[j]);
strcpy(LNAMEARR[j], temp);
strcpy(temp, CLASSARR[i]);
strcpy(CLASSARR[i], CLASSARR[j]);
strcpy(CLASSARR[j], temp);
temp[15] = CHARGEARR[i];
CHARGEARR[i]=CHARGEARR[j];
CHARGEARR[j]= temp[15];
temp[15]= GSTARR[i];
GSTARR[i]=GSTARR[j];
GSTARR[j]= temp[15];
temp[15]= PSTARR[i];
PSTARR[i]=PSTARR[j];
PSTARR[j]= temp[15];
temp[15]= TOTALARR[i];
TOTALARR[i]=TOTALARR[j];
TOTALARR[j]= temp[15];
}
}
-
To me it looks like each record should be a struct. If so, then I'd swap using structure assignment. And then only the difference from sorting ints would be the comparison.
-
We havent covered structures at all in my course, I asked the teacher about them the other day and he said not to worry about them as we arent going to cover them at all :/
The Problem we were given asks us to use seperate parallel arrays for all the information we read in from a file and then sort them in alphabetical order in "last name, first name" format.
I have everything working but this bubble sort and its giving me a real headache.
Hey I need to start paying you for answering all my questions ;p
-
>The Problem we were given asks us to use seperate parallel arrays for all the information
Could you post the variable definitions?
>Hey I need to start paying you for answering all my questions ;p
I've got no problem with that. ;)
-
Code:
int FCOUNT, BCOUNT, ECOUNT, RECORDCOUNT, TOT500, ERROR, NUMBER, i, j, PCOUNT;
char NAME[20], LNAME[20], *CLASS, CODE, BUFFER,
*REASON, FNAME[100][25], LNAMEARR[100][25], CLASSARR[100][15], temp[15];
float CHARGE, CHARGEARR[100],GST,GSTARR[100], PST,
PSTARR[100], TOTAL, TOTALARR[100], TOTTOTAL, AVGTOT, HOURS;
No doubt you wouldnt lol
-
You ought to make temp big enough to swap FNAME and LNAMEARR. You need to use a temporary of type float to swap floats, rather than trying with a single char (that is one beyond its array bounds). And use strcmp to compare strings.
-
ok that fixed my one problem but it still isnt sorting right. The output is nowhere near sorted alphabetically. It also prints out one record of blank information (all zeros and just " , " for the name). Maybe I am doing something wrong with strcmp? As far as I can tell I have done the same as pretty much every instance I have seen it used in. :/
Code:
void SortArrs ()
{
for(i=0; i<RECORDCOUNT-1; i++)
for(j=0; j<RECORDCOUNT-1; j++)
if (strcmp(FNAME[i], FNAME[j])>0)
{
strcpy(temp, FNAME[i]);
strcpy(FNAME[i], FNAME[j]);
strcpy(FNAME[j], temp);
strcpy(temp, LNAMEARR[i]);
strcpy(LNAMEARR[i], LNAMEARR[j]);
strcpy(LNAMEARR[j], temp);
strcpy(temp, CLASSARR[i]);
strcpy(CLASSARR[i], CLASSARR[j]);
strcpy(CLASSARR[j], temp);
tempf[15] = CHARGEARR[i];
CHARGEARR[i]=CHARGEARR[j];
CHARGEARR[j]= tempf[15];
tempf[15]= GSTARR[i];
GSTARR[i]=GSTARR[j];
GSTARR[j]= tempf[15];
tempf[15]= PSTARR[i];
PSTARR[i]=PSTARR[j];
PSTARR[j]= tempf[15];
tempf[15]= TOTALARR[i];
TOTALARR[i]=TOTALARR[j];
TOTALARR[j]= tempf[15];
}
}
-
I figured it out finally. I was sorting it by firstname instead of lastname for some reason and thats why it looked wrong. There were a few other problems so thanks for your help
-
-
Is that because tempf is a float array so it just is big enough to fit in the floats that are going into it? ... Sorry I like to know the reasons behind doing things like that. :cool:
-
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
float a = 10.0F, b = 20.0F, swap;
printf("a = %g, b= %g\n", a, b);
swap = a;
a = b;
b = swap;
printf("a = %g, b= %g\n", a, b);
return 0;
}
/* my output
a = 10, b= 20
a = 20, b= 10
*/
An array, saymay contains the elements a[0] ... a[14]. An a[15] is off the end of the array -- attempting to use it is undefined behavior.