mercredi 30 mars 2022

C: Shuffle String elements in 2d array

I am trying to shuffle the string elements in a 2d array, and I'm quite confused whether I should just shuffle the columns or all of them. Here's what I've done so far.

int main(int argc, char const *argv[]){

  char words[4][20] = {"abc", "dec", "dsz", "dfas"};
  for (int i=0;i<4;i++){
    srand(time(NULL));
    int r = rand() % 4;
    int temp = i;
    strcpy(words[i], words[r]);
    strcpy(words[r],words[temp]);
  }
  for(int j=0;j<4;j++){
    printf("Elements: %s\n",words[j]);
  };
  return 0;
}

However, this gives me a Trace/BPT trap: 5 error. Any helps would be appreciated.




Aucun commentaire:

Enregistrer un commentaire