![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Feb 2004
Posts: 1
| Sorting Multiple Arrays with complications of 2D arrays and strings. I am having trouble with an assignment at college. Now, I don't want anyone to write the program for me. I just want to understand the concepts. Problem Specification: I have to get a list of names and data to go with those names from an infile, sort them and then print them to the screen. The program works until I get to the actual sorting. It is returning weird values and not sorting anything. If someone could tell me what's wrong with the sort function and explain the concepts I don't have right? The code in question: Code: /*datalength is the number of entries from the datafile*/
void SortData (int datalength, char full [50][40], char last [50][20], char first [50][20], double fall [], double winter [], double spring [], double summer [], double total [])
{
int i;
int j;
char chartemp [40];
double doubletemp;
char comma [2] = {","};
/*Combines the last and first names into one array: full, and inserts a comma between them / Works*/
for (i = 0; i < datalength; i++)
{
strcpy (full [i], last [i]);
strcat (full [i], comma);
strcat (full [i], first [i]);
}
/*The part that doesn't work, the lower variable value is giving weird or wrong answers*/
for (i = 0; i < datalength; i++)
{
lowest = i;
for (j = i + 1; j < datalength; j++)
{
if (strcmp (full [j], full [lowest]) < 0);
lowest = j;
if (i != lowest)
{
strcpy (chartemp, full [j]);
strcpy (full [j], full [i]);
strcpy (full [i], chartemp);
doubletemp = fall [j];
fall [j] = fall [i];
fall [i] = doubletemp;
doubletemp = winter [j];
winter [j] = winter [i];
winter [i] = doubletemp;
doubletemp = spring [j];
spring [j] = spring [i];
spring [i] = doubletemp;
doubletemp = summer [j];
summer [j] = summer [i];
summer [i] = doubletemp;
doubletemp = total [j];
total [j] = total [i];
total [i] = doubletemp;
}
}
}
}
Code: Hill Justin 2785.00 2282.00 5720.00 6330.00 Ortego Taylor 4715.00 1676.00 6067.00 929.00 Stoilova Nadica 1253.00 1495.00 2884.00 4173.00 Please help! |
| compiler151 is offline | |
| | #2 |
| Just Lurking Join Date: Oct 2002
Posts: 5,005
| [Note: cross-posted. Replies have already been posted here.]
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #3 |
| Registered User Join Date: Feb 2004
Posts: 79
| Standard C already has a great sorting function called qsort. Code: #include <stdlib.h>
void qsort( void* base
, size_t nelem
, size_t size
, int (*cmp)(const void* e1, const void* e2)
);
Hope that helps. |
| c99 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Separate long string into multiple arrays | cashmerelc | C Programming | 6 | 11-27-2007 02:57 AM |
| strings or arrays of characters? | Callith | C++ Programming | 13 | 12-26-2004 11:28 AM |
| Storing strings in 2d char arrays problem | rainmanddw | C++ Programming | 5 | 10-22-2003 05:41 PM |
| strings or character arrays | Shadow12345 | C++ Programming | 2 | 07-21-2002 10:55 AM |
| Help With arrays and Sorting Strings | Unregistered | C Programming | 0 | 03-23-2002 01:09 AM |