8 Replies - 458 Views - Last Post: 03 September 2013 - 09:11 PM Rate Topic: -----

#1 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Scrambling strings in an array.

Posted 03 September 2013 - 04:52 PM

I'm writing a function that scrambles(shuffles) words(strings) in a 2D array. The problem I'm having is that it is scrambling the characters. Not the actual words. I've used trial and error but I can't find what wrong.

void Scramble( char dest[208][13])
  {
    for (int i = 0 ; i < 108 ; i++)
     {
       for (int j = 0 ; j < 2 ; j++)
        {   
          int m = rand() % 108;
          int n = rand() % 4;
	
	 char t = dest[i][j];
	 dest[i][j] = dest[m][n];
	 dest[m][n] = t;
	 cout << dest[i] << " ";
       }
    }
   
 }



Is This A Good Question/Topic? 0
  • +

Replies To: Scrambling strings in an array.

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,941
  • Joined: 05-May 12

Re: Scrambling strings in an array.

Posted 03 September 2013 - 05:10 PM

Your lines 10-13 are only swapping 1 character at a time. Since your for loop on lines 5-14 randomizes a new m and n, you are not consistently swapping the letters of the same word.

Remember that that for any given x word, the letters of that word as sitting in dest[x][0] through dest[x][12].
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Scrambling strings in an array.

Posted 03 September 2013 - 06:02 PM

You have the right idea, but it needs a tweak.

void Scramble( char dest[208][13])
{
   for (int i = 0 ; i < 108 ; i++)
   {
      /* since you want to shuffle WORDS, (not chars in the
         the words, you don't want this inner for loop, at all
         Delete this:
      for (int j = 0 ; j < 2 ; j++)
      {  
         int m = rand() % 108;
         int n = rand() % 4;
         char t = dest[i][j];
         dest[i][j] = dest[m][n];
         dest[m][n] = t;
         cout << dest[i] << " ";
      }
     */
        int m = rand() % 108; 
        strcpy(temp,dist[m]);
        strcpy(dist[m],dist[i]);
        strcpy(dist[i],temp);
   }
}

//NOW print out the sorted list of words, OUTSIDE the shuffle block of code.


Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,941
  • Joined: 05-May 12

Re: Scrambling strings in an array.

Posted 03 September 2013 - 06:20 PM

LOL! Now you've done it. :)/> defjamvan is not allowed to use strcpy(). I bet you've just thrown him into apoplexy.

On the plus side, though, you've shown him how he can apply his own strcpy() when he rolls his own.
Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Scrambling strings in an array.

Posted 03 September 2013 - 07:31 PM

< LOL > B)

This post has been edited by Adak: 03 September 2013 - 07:32 PM

Was This Post Helpful? 0
  • +
  • -

#6 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Scrambling strings in an array.

Posted 03 September 2013 - 09:00 PM

LOL! Thanks guys
Was This Post Helpful? 0
  • +
  • -

#7 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Scrambling strings in an array.

Posted 03 September 2013 - 09:06 PM

Wait @Adak What is temp? char temp[208]
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,941
  • Joined: 05-May 12

Re: Scrambling strings in an array.

Posted 03 September 2013 - 09:10 PM

An array of 13 should be sufficient since the source strings cannot take up more than 13.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: Scrambling strings in an array.

Posted 03 September 2013 - 09:11 PM

It's a temporary variable. A place holder. Let's say you want to swap A and B. If you do:
A = B
B = A
You'll end up with both being the original value of B, so you do:
temp = A
A = B
B = temp
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1