Thread: Reverse a random number array

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    3

    Reverse a random number array

    I need to make an array of 10 cells using random numbers and then print it backwards and forwards. Also it can only have values 0 to 100.

    I still need correct the range and print it backwards.
    This is what I have so far. Help?Lab7.c
    Code:
     #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int main ()
    {
    
    
        int b[10];
    
    
        srand(0);
    
    
        int i;
        for (i = 0; i < 10; i++) {
            b[i] = rand();
        }
    
    
        printf("The value of my array is %d\n", b[i]);
    
    
    
    
        printf("The value of my array backwards is %d\n", b[i]);
    
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can start by putting for loops around both your printf calls.

    I mean, you managed to put a for loop around your rand calls to fill the array to begin with.

    Unless you didn't do that, and this is a "fill in the missing blanks" homework from your tutor's boilerplate code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    3
    Okay so I made the other for loop, but now it brinks the backwards one as 0
    And I still need the range to be 1 to 100

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int main ()
    {
        int b[10];
    
    
        srand(0);
    
    
        int i;
        for (i = 0; i < 10; i++) {
            b[i] = rand();
        }
        printf("The value of my array is %d\n", b[i]);
    
    
        for (i = 9; i >= 0; i--){
    
    
        }
        printf("The value of my array backwards is %d\n", b[i]);
    
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Print the data in the loops.

    To get an number on the interval 0-100 use modulus.
    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


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: How to Reverse an array of RANDOM numbers??
    By Kaylah Hubbard in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2016, 01:14 AM
  2. Array and random number
    By Silsilay in forum C++ Programming
    Replies: 5
    Last Post: 04-09-2012, 09:28 AM
  3. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04:44 PM
  4. fill array with random number
    By azsquall in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2008, 12:27 PM
  5. Random Number Array
    By thestrap in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 12:44 AM

Tags for this Thread