Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //
- // main.c
- // StretchVerb
- //
- // Created by Beau Wright on 10/14/12.
- // Copyright (c) 2012 Beau Wright. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- void generateArray(int a[], int size);
- int generateRandomNumber();
- int main(int argc, const char ** argv)
- {
- int size;
- printf("How many random numbers do you want to generate? ");
- scanf("%d", &size);
- int arrayOfRandomNumbers[size]; // Declares an array of 'size' integers. Currently just empty values.
- generateArray(arrayOfRandomNumbers, size); // Assign numbers to each of the 'size' elements. arrayOfNumbers will have values after
- // this function executes and returns
- // Now arrayOfRandomNumbers has size number of elements with different random values in each slot
- // Print each value out to the screen
- for (int i = 0; i < size; i++) {
- 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
- // Print from slot 0 until we hit the max size of the array
- }
- }
- int generateRandomNumber()
- {
- int randomNumber, min, max;
- min = 1;
- max = 10;
- // Get a random number using the C standard library function rand()
- randomNumber = (rand() % (max - min + 1)) + min;
- return randomNumber;
- }
- // Pass the array to populate into the function, but also pass the size.
- // There is a way to get the size out, but that's complicated, and
- // this is easier for now.
- void generateArray(int a[], int size)
- {
- int x; // Declare an integer number (currently not assigned a value, just empty)
- for (int i = 0; i < size; i++)
- {
- // Grab a random number
- x = generateRandomNumber();
- // Assign that number at the ith position in the array
- a[i] = x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement