Thread: Dynamic arrays

  1. #1
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    Dynamic arrays

    The assignment is this;

    Write a complete 'c' program to populate a eleven element array with a random listing of eleven whole numbers:
    Use functions to determine the following:
    1) The largest value & the index number of that element
    2) The smallest value and the index number of that element
    3) The mid-range value and the index number of that element
    **He want use to use dynamic arrays.
    To me a dynamic array sounds like a multidimesional array, without the function to allocate the memory. I am not sure if that is even the right line to draw between the two. I am really embarresed of the code I have written for it up to now but here it is. I am just trying to fill up the array with 11 random whole numbers and print it out, but I can seem to get that right either.

    [code]

    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    int main(void)
    {
    int wNums[];
    int i;
    clrscr();

    randomize();

    for(i=0; i<11; i++)
    scanf(" %d", wNums[i]);
    printf("%d\n", rand());
    getch();
    return 0;
    }
    [\code]

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A dynamic array is an array that you allocate the memory for yourself with either malloc, calloc, or realloc:
    Code:
    int *array;
    array = malloc ( 11 * sizeof *array );
    if ( array != NULL ) {
      /* Fill and work with the new array */
    }
    To create a random number which is passable for this program, include both time.h and stdlib.h. You want to use the srand function like so to seed the random number generator with every running of the program:

    srand ( (unsigned)time ( NULL ) );

    And then call rand and assign the returned value of it to each element of your array. For large numbers, simply use:

    array[x] = rand();

    Here's one solution to simply creating the array and filling it with random numbers with a decent range:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ( void )
    {
      int x;
      int *array;
      srand ( (unsigned)time ( NULL ) );
      if ( ( array = malloc ( 11 * sizeof *array ) ) != NULL ) {
        for ( x = 0; x < 11; x++ ) {
          array[x] = ( rand() * 500 ) / RAND_MAX;
          printf ( "%d\n", array[x] );
        }
        free ( array );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    Thanks Prelude

    The help is much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM