7 Replies - 430 Views - Last Post: 25 October 2013 - 08:39 PM Rate Topic: -----

#1 s.waqas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 10-October 13

Returning a randomized string array without repeating in C?

Posted 22 October 2013 - 08:20 PM

Hi,

I want this to be in C, so please answer in the C terminology, because I dontknow C++ yet.

I am confused!!! I have an array pointer of strings (line 29) and I want to randomize that array and save it to another using recursion and return that randomized without the strings repeating. I don't have much experience with pointers =[. You can see my code so far.

The problems I am facing:
1. The randomized string has repeats and you can look at those if you made the fn. void.
2. I cannot get the randomized fn to be returned.
3. I cannot save the randomized array to a different array inside my main so that I can use that randomized array elsewhere.


This is my code:
     1 #include <stdio.h>
  2 #include <time.h>
  3 #include <stdlib.h>
  4 
  5 int i = 0;
  6 int j;
  7 char *rand_arr(char *arr[], char *buff[])
  8 {
  9         if(i<8)
 10         {
 11                 buff[i] = arr[rand() % 7];
 12                 i++;
 13                 rand_arr(arr, buff);
 14         }
 15 //      printf("after rand\n");
 16         if (i==8)
 17         {
 18         
 19                 for(j=0; j<8; j++) // for debugging
 20                         printf("  %s", buff[j]);
 21         }
 22         return *buff;
 23 }
 24 
 25 int main(void)
 26 {
 27         srand(time(NULL));
 28         printf("\n"); 
 29         char *arr[] = {"cat", "dog", "lion", "frog", "bear", "beaver", "snake", "eagle"};
 30         char *buff[8]; // initializing to NULL
 31         
 32         char *rand_arr[8];
 33 
 34 //      rand_arr(arr, buff);
 35         rand_arr = rand_arr(arr, buff);
 36         
 37         return 0;
 38 }                       



This is my errors:

re_rand_pointer.c:35:21: error: called object type 'char **' is not a function or function pointer
        rand_arr = rand_arr(arr, buff);
                   ~~~~~~~~^
1 error generated.




Please answer in simple terms that a NEWBIE can understand.

Is This A Good Question/Topic? 0
  • +

Replies To: Returning a randomized string array without repeating in C?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3451
  • View blog
  • Posts: 10,651
  • Joined: 05-May 12

Re: Returning a randomized string array without repeating in C?

Posted 22 October 2013 - 08:30 PM

On line 32, your declared a variable rand_arr. You cannot call that variable like function on line 35.

Out of curiosity, why do you need to use recursion? Seems like a lot of contortions for very little gain. As a hint, you'll need an extra parameter to your recursive function which indicates how many more items are left to pick from. And if the source and destination sizes are not well known, the size of the source/destination as well.
Was This Post Helpful? 0
  • +
  • -

#3 s.waqas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 10-October 13

Re: Returning a randomized string array without repeating in C?

Posted 22 October 2013 - 09:03 PM

Okay I have changed the name of the new array. I have also added a counter variable to my fn's parameter which should go 8 times. And it does go 8 times now.

But I am still having two problems:
1. Some of the strings are repeating.
2. It is saying at line 35, that arr is not assignable.


This is the modified code:

  1 #include <stdio.h>
  2 #include <time.h>
  3 #include <stdlib.h>
  4 
  5 int i = 0;
  6 int j;
  7 char *rand_arr(char *arr[], char *buff[], int n) // n is the number of times we want to loop using     recursion
  8 {
  9         if(n !=0)
 10         {
 11                 buff[i] = arr[rand() % 7];
 12                 i++;
 13                 rand_arr(arr, buff, n-1);
 14         }
 15 //      printf("after rand\n");
 16         if (i==8)
 17         {
 18 
 19                 for(j=0; j<8; j++) // for debugging
 20                         printf("  %s", buff[j]);
 21         }
 22         return *buff;
 23 }
 24 
 25 int main(void)
 26 {
 27         srand(time(NULL));
 28         printf("\n");
 29         char *arr[] = {"cat", "dog", "lion", "frog", "bear", "beaver", "snake", "eagle"};
 30         char *buff[8]; // initializing to NULL
 31 
 32         char *randomized_arr[8];
 33 
 34 //      rand_arr(arr, buff);
 35         randomized_arr = rand_arr(arr, buff, 8);
 36 



What it shows when I compile:
re_rand_pointer.c:35:17: error: array type 'char *[8]' is not assignable
        randomized_arr = rand_arr(arr, buff, 8);
        ~~~~~~~~~~~~~~ ^



Quote

Out of curiosity, why do you need to use recursion? Seems like a lot of contortions for very little gain.


That is because in my CS class we started learning recursions, and I want to have a grasp of it. Also for our next project, we have to do something similar but on a higher and more difficult level, so I want to break it down and learn piece-by-piece, I don't want to fall behind as this is one of my first CS courses and I think it is the foundation, so if I don't grind my "brain" and try all different ways myself (and ofcourse, ask you guys if I need to be guided in the right direction, in terms of coding) then I will barely learn...=|
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3451
  • View blog
  • Posts: 10,651
  • Joined: 05-May 12

Re: Returning a randomized string array without repeating in C?

Posted 23 October 2013 - 06:29 AM

Right now, you are always picking a random number between 0 and 6 based on your line 11. What if in each recursive call you first pick a random number between 0 and 7, and then on the next call between 0 and 6, and then 0 and 5, etc. Each time you pick a random number, you not only copy that word into your output array, but you also take out that word from the pool of possible words.
Was This Post Helpful? 0
  • +
  • -

#5 s.waqas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 10-October 13

Re: Returning a randomized string array without repeating in C?

Posted 24 October 2013 - 07:08 PM

View PostSkydiver, on 23 October 2013 - 06:29 AM, said:

Right now, you are always picking a random number between 0 and 6 based on your line 11. What if in each recursive call you first pick a random number between 0 and 7, and then on the next call between 0 and 6, and then 0 and 5, etc. Each time you pick a random number, you not only copy that word into your output array, but you also take out that word from the pool of possible words.


Thank you!!! I was gonna try that, but two HUGE SPEED BREAKERS came to me, I still don't know how to weed out the ones already used with recursion, and how to return a pointer function because I am not efficient in C yet. So I picked a different way of solving the problem:

A different way:

Because what I want is a function that takes in an array and randomizes it without repeating so that I can use my array inside my other functions.

My code:

int i = 0;
void randomize_animals(char *animals[], int n)
{
        if(n !=0) 
        {
        int j; 
        char *tmp;
 
        j = rand() % 8;
        tmp = animals[i];
        animals[i] = animals[j];
        animals[j] = tmp;
         
                i++;
 
                randomize_animals(animals, n-1);
        }
}
int main(int argc, char *argv[])
{
 
    srand(time(NULL));
 
    char *animals[] = {"frog", "cat", "dog", "horse", "snake", "eagle", "bear", "weasel"}; // original array
     
    randomize_animals(animals, 8); // randomizing the array
     
    printf("\n");
 
    story(animals, 16); // here the randomized array will be used
 
    printf("...and the child fell asleep.\n"); // printing out the last line of the story.
     
    return 0;
}



When I call the story function in my main, the animals array it is using is a randomized version and you can check that by printing out the result with a for loop for example.


Thanks everyone!!!!!!!!!!!!!!!!!!!!!!!!!
And sorry for the late reply, I wanted to post this earlier but didn't have the chance!
Was This Post Helpful? 1
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,485
  • Joined: 19-February 09

Re: Returning a randomized string array without repeating in C?

Posted 25 October 2013 - 07:23 PM

Hi, instead of i you could use n-1 to give an index.

Is the value 16 correct here?

30	    story(animals, 16); // here the randomized array will be used

Was This Post Helpful? 0
  • +
  • -

#7 s.waqas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 10-October 13

Re: Returning a randomized string array without repeating in C?

Posted 25 October 2013 - 08:13 PM

View Post#define, on 25 October 2013 - 07:23 PM, said:

Hi, instead of i you could use n-1 to give an index.

Is the value 16 correct here?

30	    story(animals, 16); // here the randomized array will be used


Yes the value of 16 is correct, that's because I wanted to print every animal twice recursively.
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,485
  • Joined: 19-February 09

Re: Returning a randomized string array without repeating in C?

Posted 25 October 2013 - 08:39 PM

View Posts.waqas, on 26 October 2013 - 04:13 AM, said:

Yes the value of 16 is correct, that's because I wanted to print every animal twice recursively.


I see, thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1