Like Tree1Likes
  • 1 Post By failure67

random number generator array

This is a discussion on random number generator array within the C Programming forums, part of the General Programming Boards category; hey i having problems with solving this question: Write a program that generates an array of 20 random numbers between ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    4

    Exclamation random number generator array

    hey i having problems with solving this question:
    Write a program that generates an array of 20 random numbers between 1 and 100. Use a loop to
    output the numbers on the screen with a field width of 4 characters. Write code that will reverse the
    numbers in the array and then output the numbers to the screen with a copy of the same output loop
    used with the original numbers.

    Please help

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    What kind of errors are you getting?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    array of 20
    Code:
    int arr [20] ;

    20 random numbers
    rand () and srand ()


    Use a loop
    Code:
    int i ;
    for (i = 0; i < 20; ++i) {
    ...
    }

    output the numbers on the screen with a field width of 4 characters
    printf () formatting


    reverse the numbers
    A swap () function

    Or copy them backwards into a second array.


    Please help
    Homework help
    Salem likes this.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    i have filled the array with random numbers and got it to print with a feild width of 4 characters i just dont know figure out how to print the array data in reverse. this is what i have:
    Code:
    int main()
    {
              int array[20], i;
              for(i = 0; i < 20; i++)
              {
                      array[i] = (rand() % 100)+1;
                      printf("%4d\n", array[i]);
           }
        return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,824
    Write a second loop that starts from the last element to print them.
    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

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    Quote Originally Posted by laserlight View Post
    Write a second loop that starts from the last element to print them.
    hey laserlight
    so say the first 3 integers in the array are:
    45
    100
    20
    how would i switch all numbers in the array to:
    54
    001
    02
    (opposite)?
    thankyou for your help

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    333
    It's easy enough to print numbers in reverse.

    Just go

    Code:
    for(i=N-1;i>=0;i--)
      printf("%d\n", array[i]);
    But you're not allowed to do this. You have to reverse the array. So you have to step throuhg half of it, swapping very ith element with the N-i-1th element. It's a bit tricky to get all the cases right.

    To generate random number on 0 to N-1, the cheap and nasty way is rand() % N. This doesn't give a very good distribution, because rand() isn't truly random and the lower bits are less random than the whole.
    So the best way to to say

    Code:
    /* random number on 0-1 minus a tiny epsilon */
    #define uniform() (rand()/RAND_MAX+1.0))
    
    /* random number on 0-N */
    #define rnd(N) (int) (uniform() * (N))
    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.
    http://www.malcolmmclean.site11.com/www

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by werty2480 View Post
    so say the first 3 integers in the array are:
    45
    100
    20
    how would i switch all numbers in the array to:
    54
    001
    02
    (opposite)?
    If you need to reverse the order of the digits in a number you could use a combination of modulo (%) and divisior.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Random number generator
    By jbone0881 in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 10:20 PM
  3. Random number generator
    By bond_006 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2010, 11:28 AM
  4. Random number generator between 0 and 1
    By Lord CyKill in forum C# Programming
    Replies: 1
    Last Post: 03-29-2007, 08:03 AM
  5. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 06:34 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21