Advertisement
ArtisOracle

Untitled

Oct 14th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. //
  3. //  main.c
  4. //  StretchVerb
  5. //
  6. //  Created by Beau Wright on 10/14/12.
  7. //  Copyright (c) 2012 Beau Wright. All rights reserved.
  8. //
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <string.h>
  14.  
  15. void generateArray(int a[], int size);
  16. int generateRandomNumber();
  17.  
  18. int main(int argc, const char ** argv)
  19. {
  20.     int size;
  21.     printf("How many random numbers do you want to generate? ");
  22.     scanf("%d", &size);
  23.     int arrayOfRandomNumbers[size];   // Declares an array of 'size' integers. Currently just empty values.
  24.     generateArray(arrayOfRandomNumbers, size);    // Assign numbers to each of the 'size' elements. arrayOfNumbers will have values after
  25.                                             // this function executes and returns
  26.    
  27.     // Now arrayOfRandomNumbers has size number of elements with different random values in each slot
  28.    
  29.     // Print each value out to the screen
  30.     for (int i = 0; i < size; i++) {
  31.         printf("Value at number %d : %d\n", i, arrayOfRandomNumbers[i]);    // Print the value at index i, where i is determined by the loop counter value
  32.                                                 // Print from slot 0 until we hit the max size of the array
  33.     }
  34. }
  35.  
  36. int generateRandomNumber()
  37. {
  38.     int randomNumber, min, max;
  39.     min = 1;
  40.     max = 10;
  41.     // Get a random number using the C standard library function rand()
  42.     randomNumber = (rand() % (max - min + 1)) + min;
  43.     return randomNumber;
  44. }
  45.  
  46. // Pass the array to populate into the function, but also pass the size.
  47. // There is a way to get the size out, but that's complicated, and
  48. // this is easier for now.
  49. void generateArray(int a[], int size)
  50. {
  51.     int x;  // Declare an integer number (currently not assigned a value, just empty)
  52.     for (int i = 0; i < size; i++)
  53.     {
  54.         // Grab a random number
  55.         x = generateRandomNumber();
  56.         // Assign that number at the ith position in the array
  57.         a[i] = x;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement