Thread: Calling the Same Array from main(): Specified and Random

  1. #1
    Registered User
    Join Date
    Apr 2019

    Calling the Same Array from main(): Specified and Random

    Good morning!

    I am attempting to do some C practice in preparation from an exam. In one of these prompts, the instructions are asking me to write an array that will be populated by random integers. This step was added onto the a previous step and are to be connected. In the previous step, I created an array with five values. There are two prototypes that were asked to be written and they are called from the main. The only issue that I am really having here that when I call the functions, they both have the same numbers. Why are the random integers being given to both sets of arrays?

    #include <stdio.h>#include <stddef.h>
    #include <stdlib.h>
    void display(int testArray[]);
    void randPop(int testArray[]);
    int main()
        int testArray [5] = {22, 3, 5, 707, 1};
        return 0;
    void display(int testArray[])
        int i;
        for (size_t i = 0; i < 5; ++i) {
            printf("%7zu%13d\n", i, testArray[i]);
    void randPop(int testArray[])
        int j;
        for (size_t j = 0; j < 5; ++j) {
            testArray[j] = rand() % 100 + 1;
            printf("%7zu%13d\n", j, testArray[j]);
    Is there a way to supply randPop with random integers and leave display with the original specified integers when called from the same array? I want to pass testArray to both randPop and display.

    Thank you for all help in advance! It is grealy appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    That's expected: you randomly populated the array and then you displayed it. If you wanted to print the array with the original hardcoded values, then you should call display before calling randPop.

    Also, you need to seed the pseudorandom number generator by calling srand near the start of the main function with a seed that differs on each run of the program. If not, it'll be as if you wrote srand(1) at the start of main, i.e., the same seed is always used so you always get the same sequence of pseudorandom numbers. This can be done by #include <time.h> and writing:
    srand((unsigned int)time(NULL));
    By the way, don't do this:
    int i;
    for (size_t i = 0; i < 5; ++i) {
    What you're doing is declaring a variable named i of type int, then immediately shadowing it in the scope of the for loop by declaring another variable named i of type size_t. Get rid of the first, unused, declaration of i.
    Last edited by laserlight; 04-29-2019 at 11:16 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Thank you so much for your help! Your suggestion helped perfectly and I even learned a little bit more that I likely wouldn't have got on my own. I really appreciate it!
    Best regards!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2016, 05:34 AM
  2. Calling two functions in main? ... new to C
    By Ershat Hamid in forum C Programming
    Replies: 4
    Last Post: 08-02-2011, 06:32 AM
  3. Calling this function in my main.
    By filadon in forum C Programming
    Replies: 3
    Last Post: 02-15-2011, 02:58 PM
  4. Need help calling outside of main with arrays
    By Invertalon in forum C Programming
    Replies: 1
    Last Post: 03-25-2008, 07:12 PM
  5. Calling main() from within it... is it possible?
    By pritin in forum C++ Programming
    Replies: 6
    Last Post: 08-31-2005, 09:51 AM

Tags for this Thread