-
sorting problem
Hi,
I am trying to get my program to sort a list alphabetically and numerically.
This is the list to sort:
trapezoid 45
triangle 36
circle 10
square 67
oval 56
rectangle 25
This is what I want to print out:
(the list in alphab)
circle 10
oval 56
rectangle 25
square 67
trapezoid 45
triangle 36
(the list numer.)
circle 10
rectangle 25
triangle 36
trapezoid 45
oval 56
square 67
To do this, I stored the data in two arrays, one for the character strings and the other for the numbers. I need to convert the character strings to all caps first to be able to sort the list. I am able to compile my program, but then it does not print out the list out correctly. Is my logic wrong? Can anyone help me?
Here are my functions that I wrote to call in my main program:
Code:
void uppercase(char *shape [25])
{
/*convert all to caps*/
int toupper(char shape);
}
void sortalphabetically(char *shape [25], int numbers [], int size)
{
/*declaration of variables*/
char temp[50];
int pass, i;
/*sort alphabetically*/
for (pass=0; pass<size; pass++)
for (i=pass+1; i<size; i++)
{
if (strcmp (shape[numbers[pass]], shape[numbers[i]]) >0 )
/*swap*/
{
strcpy(temp, shape[numbers[pass]]);
strcpy(shape[numbers[pass]], shape[numbers[i]]);
strcpy(shape[numbers[i]], temp);
}
}
}
/*using selection sort method*/
void sortnumerically(char *shape [25], int numbers [], int size)
{
/*declaration of variables*/
int i, j, smallestnum, temporary;
/*initialization*/
for (i=0; i<size; i++)
{
smallestnum=i;
for (j=i; j<size; j++)
{
if ( numbers[smallestnum] > numbers[j] )
{
smallestnum=j;
}
/*swap*/
temporary=numbers[i];
numbers[i]=numbers[smallestnum];
numbers[smallestnum]=temporary;
}
}
/*print contents in the array*/
void printarray(char *shape [25], int numbers [], int size)
{
int i;
for (i=0; i<size; i++)
printf("The sorted list of shapes: %d\n", shape[numbers[i]]);
}
-
Can you post the main code?
How did you define shapes and numbers?
shape[numbers[pass]] ????
You should declare a structure containing the number and the shape. This way you'll only have to swap the structures when sorting.
-
Try this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ASIZE 6
typedef struct {
char *name;
int num;
} db_st;
int sort_by_name ( const void *a, const void *b ) {
const db_st *pa = a;
const db_st *pb = b;
return strcmp( pa->name, pb->name );
}
int sort_by_number ( const void *a, const void *b ) {
const db_st *pa = a;
const db_st *pb = b;
if ( pa->num < pb->num ) return -1;
if ( pa->num > pb->num ) return +1;
return 0;
}
void print_arr ( db_st *arr ) {
int i;
for ( i = 0 ; i < ASIZE ; i++ ) {
printf( "%s %d\n", arr[i].name, arr[i].num );
}
printf( "\n" );
}
int main ( ) {
db_st data[] = {
{ "trapezoid", 45 },
{ "triangle", 36 },
{ "circle", 10 },
{ "square", 67 },
{ "oval", 56 },
{ "rectangle", 25 },
};
print_arr( data );
qsort( data, ASIZE, sizeof(db_st), sort_by_name );
print_arr( data );
qsort( data, ASIZE, sizeof(db_st), sort_by_number );
print_arr( data );
return 0;
}
-
Thanks Salem, but we haven't learned about structures and I am just a beginner, so I don't quite understand all of your code.
-
what salem did is create a struct with both the name and the number in it, so they would be forever bonded. when one is sorted, the other stays with it. you can do the same thing without structs, but you need to keep the number and string together by moving both in the same swap.
-
Thanks for the explanation ygfperson. But in my assignment we have to separate them into two arrays. In my main code I call a function that reads in the data and then separates them into a character array and an array to hold the numbers. This read file then returns the number of shapes/number pairs that were successfully read from the file. I know that the readfile works, it is just the sorting and making the character string in caps that don't work. That is why I have an array in an array, because I don't know how to keep the shape and number together. Is there a way to do that without using structures?
-
When you set your arrays up, make sure the position of the number and the type of shape are the same. eg:
Code:
int shapenum[6];
char *shapetype[6];
shapetype[0] = "circle";
shapenum[0] = 10;
//etc. etc