Thread: Need help with rand(), random array

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    3

    Need help with rand(), random array

    I'm trying to write a function with any random integer that the user will enter with scanf (randomNumber)

    Code:
    int randomArray(int arr1, int randomNumber) 
    {
        int i, x = rand() % randomNumber;
        for (i = 0; i < 10; i++){
            arr1[&i] = x;
        printf("Enter arr[%d]: %d", i, x);
    }
        return randomArray;
    but i keep getting errors in the middle of the program in ''arr1[&i] = x;'' and it forces it to end.
    so what am i doing wrong or what do i need to add? thank you.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    394
    Quote Originally Posted by avenged View Post
    I'm trying to write a function with any random integer that the user will enter with scanf (randomNumber)

    Code:
    int randomArray(int arr1, int randomNumber) 
    {
        int i, x = rand() % randomNumber;
        for (i = 0; i < 10; i++){
            arr1[&i] = x;
        printf("Enter arr[%d]: %d", i, x);
    }
        return randomArray;
    This code is strange and it's hard to see what you are trying to
    achieve. The unequivocal mistake is that "&" is the address
    of operator in C. So you are saying "take the address of i and
    use it as an index into arr1". arr1 is not even an array, it is
    an integer. So the compiler throws a fit at this point.

    You also seem to be trying to return a function. This is the idiom
    in some programming languages, but not C. C functions are
    compiled into machine code. You can take their addresses, but
    you cannot manipulate the function itself at runtime.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    3
    I'm trying to create a new array, with random numbers, the numbers are limited from 0 to a random number that the user decides while running it. i'm pretty sure that the problem is with "rand() % randomNumber".
    I first wrote it as " arr1[i] = x;", I only added the "&" because visual studio gave me an error there and wouldn't let me run the program without it.
    Last edited by avenged; 05-26-2020 at 07:04 AM.

  4. #4
    Registered User
    Join Date
    May 2020
    Posts
    3
    ok it's solved you can close it, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() function is not really random
    By edsoneicc in forum C Programming
    Replies: 4
    Last Post: 02-25-2012, 11:14 AM
  2. Random rand() question
    By Nathan the noob in forum C++ Programming
    Replies: 13
    Last Post: 01-27-2009, 01:52 PM
  3. Rand() not bieng very random
    By Necrofear in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2007, 08:12 PM
  4. rand() not so random....
    By Kewley in forum C++ Programming
    Replies: 11
    Last Post: 01-23-2007, 08:19 AM
  5. Rand() won't be random
    By EvilPickles in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2006, 01:47 AM

Tags for this Thread