Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 24th, 2006, 9:27 PM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
sort and swap

Hi,
I want to write a function in which I have an array list that contains random permutation of integers from 0 to 51.
Then, we use a ascending selection sort selsort to sort list- but with each swap in the sort, also swap the corresponding cards (rows) in the deck.
deck is of type arrays of strings. its declaration is like

char deck[5][20]={"two of clubs","three of clubs","four of clubs",
"five of clubs","six of clubs","seven of clubs",
"eight of clubs","nine of clubs","ten of clubs",
"jack of clubs","queen of clubs","king of clubs",
"ace of clubs","two of spades",.........


the code for the ascending sort is:
(NOTE: I modified this code by adding the red instructions to solve the problem)
void selsort(int x[ ],int n)
/* purpose: to sort a numerical vector in ascending order using
            the selection sort algorithm 
parameters: x -- the array to be sorted/input&output
            n -- the number of elements in the array */
{ int min; /*position of minimum*/
  int i,j; /*counters for loops */
  float t; /*temporary hold for interchange*/
  char deck[5][20];
/*outer loop to find min of what is left */
  for(i=0;i<n-1;i++)
{ min = i; /*assume smallest in current position*/
  for(j=i+1;j<n;j++) /*look thru rest*/
  {
  if(x[j]<x[min])  /*see if less than current min */
    min = j;  /*yes, so note its position */
  }
    t = x[i]; /*found min so move it into position */
    x[i]=x[min];
    x[min] = t;
    swap(deck,i,j);
    } /*end of outer loop*/
 } /*end of function */

My swap function is:
void swap(char terms[][20],int i,int j)
{
     char temp[20];
     strcpy(temp,terms[i]);
     strcpy(terms[i], terms[j]);
     strcpy( terms[j],temp);
}

When I execute selsort, I get a wrong sorting, and when I try to print deck, it just give me one card.(when I removed the red instructions,the function selsort is OK)
I really dont know why that happened.
Please,can someone tell me (suggestions) why I have those two problems?
How to get at the same time a good sorting( from 0 to 51) and be able to print the shuffled deck?
Or my approach is wrong.....
B.
brad sue is offline   Reply With Quote
Old Mar 25th, 2006, 6:33 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
    swap(deck,i,j);
should be
    swap(deck,i,min);
If you are swapping i and min in the "x" array, you probably want to swap i and min in the deck array. j's value is too large for the array at this point as well.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC