0

I tried creating an array of ten random strings that would print directions randomly. Such as first time "up down right ... rot_x" and second time "forward rot_y up ... down" etc. I tried using a char* pc and allocating memory for it with memset but that didn't work so I tried the following code but I'm getting weird output. How can I fix this?

int main()
{
  int r_num;
  char r_arr[10][10];

  for (int i = 0; i < 10; i++)
  {
    r_num = rand() % 10;

    switch(r_num)
    {
      case 0: strcpy(r_arr[0], "up");
              break;
      case 1: strcpy(r_arr[1], "down");
              break;
      case 2: strcpy(r_arr[2], "left");
              break;
      case 3: strcpy(r_arr[3], "right");
              break;
      case 4: strcpy(r_arr[4], "rot_x");
              break;
      case 5: strcpy(r_arr[5], "rot_y");
              break;
      case 6: strcpy(r_arr[6], "rot_z");
              break;
      case 7: strcpy(r_arr[7], "forward");
              break;
      case 8: strcpy(r_arr[8], "back");
              break;
      case 9: strcpy(r_arr[9], "reset");
              break;
      default:
        fprintf(stderr, "Cannot process input/n");
    }
  }

  for (int i = 0; i < 10; i++)
  {
    printf("%s ", r_arr[i]);
  }

  return 0;

}

here's my output:

up ?V? left right rot_x ?V?  forward back reset
4
  • why are you putting each random choice at a different location? this would make more sense if each of the cases did the strcpy to r_arr[i]. Commented Jan 15, 2015 at 21:57
  • 1
    You're randomizing to which elements of r_arr you write. Some of them may never get written to. Commented Jan 15, 2015 at 21:57
  • @ArlieStephens right, I completely overlooked it, thanks that fixed it. Commented Jan 15, 2015 at 21:59
  • 1
    I can't see the point in using strcpy() here. Why not just create an array char *r_arr[10] and fill each value with a pointer to one of your ten strings? Commented Jan 15, 2015 at 22:02

6 Answers 6

1

A few problems with your code are:

  1. You aren't seeding rand(), so every run of your program will generate identical output. You need to use srand() first with a seed. Traditionally one uses time().
  2. Secondly despite the randomness you are unrandomly (is that a word?) filling r_arr. "up" will always be first, "down" will always be second etc.... Instead you should do something like

    for (int = 0; i< 10; i++) {
       r_num = rand() % 10;
       strcpy(r_arr[i], getDirection(r_num));
    }
    

where getDirection() will return a string based on an integer input (e.g.: via a case statement that associates 0 with "up").

  1. Your r_arr needs to be initialized. There is no guarantee in your current code that each entry in the array will be populated with chars before being accessed. If you implement suggestion 2 then you wont have a problem. Today however your code is accessing potentially uninitialized memory.
Sign up to request clarification or add additional context in comments.

Comments

1

As noted by others above, your issue is that you're not indexing your array with the iteration number of your loop. You had:

case 0: strcpy(r_arr[0], "up");

Whereas you should have had:

case 0: strcpy(r_arr[i], "up");

The additional thing that I wanted to point out is that rand() uses a linear equation (at least on many systems) so it will be impossible for you to ever get two even numbers in a row or two odd numbers in a row, which is not very random. Hence I suggest something like:

r_num = (rand() >> 8) % 10;

Comments

0

As the commenters pointed out, you are randomizing not what value you put in each position but which positions get filled with their preset value. Also, your use of a switch statement here is just odd. Try something like:

char value_arr[10][10]={"up", "down", "left", "right", "rot_x", "rot_y", "rot_z", "forward", "back", "reset"}

 for (int i = 0; i < 10; i++)
  {
    r_num = rand() % 10;
    strcpy(r_arr[i], value_arr[r_num]);
  }

Comments

0

Print the strings inside the switch instead of the for-loop at the end.

Maybe you'll also need something like:

srand (time(NULL));

Comments

0

here is a code that fits exactly to your need :

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h> 

int main()
{
  // we use this instruction to get a random result each time we run the program 
    srand(time(NULL));
    int r_num;
    char r_arr[10][10];
// the dictionary array will be used to take from it the possible strings 
    char dictionary[10][10]={"up","down","left","right","rot_x","rot_x","rot_x","forward","back","reset"};
    int i=0;
    for(i=0; i<10; i++)
    {
      // r_num will be assigned randomly an index from dictionary tab
      // the general equation is (rand()%(max-min+1))+min to get a random value 
      // between max and min inclusive
        r_num=(rand()%(9-0+1))+0;
      // we will put the random string chosen in the array r_num each time 
        strcpy(r_arr[i],dictionary[r_num]);
    }
     // this loop will print the result 
    for(i=0; i<10; i++)
    {
        printf("r_arr[%d]=%s \n",i,r_arr[i]);
    }

    return 0;
}

Comments

0

by looking at your output i noticed some strange values like ?v?, the problem is that not all numbers between 0 and 9 will be generated by the rand() function which mean that the corresponding array element(to those numbers) will not be initialized and therefor it contain garbage values from what ever was stored in that memory address.
i hope that explain why you are getting those strange values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.