array functions

This is a discussion on array functions within the C++ Programming forums, part of the General Programming Boards category; I wish to define a random integer to an array or part of an array, then display it, but this ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    23

    array functions

    I wish to define a random integer to an array or part of an array, then display it, but this does nothing.

    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    int fill_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2,int low_random,int high_random);
    int display_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2);
    int main()
    {
    time_t seconds;
    time(&seconds);
    srand((unsigned int)seconds);
    int fg[5][5];
    fill_array(fg,1,5,1,5,1,6);
    display_array(fg,1,5,1,5);
    system ("pause");
    return 0;
    }
    int fill_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2,int low_random,int high_random)
    {
    int dimension1a=low_dimension1+1,dimension2a=low_dimen sion2+1;
    for(dimension1a;dimension1a<high_dimension1;dimens ion1a++)
    {
    for(dimension2a;dimension2a<high_dimension2;dimens ion2a++)
    {
    array[dimension1a][dimension2a]=rand()%(high_random-low_random+1)+low_random;
    }
    }
    return 0;
    }
    int display_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2)
    {
    int dimension1b=low_dimension1+1,dimension2b=low_dimen sion2+1;
    for(dimension1b;dimension1b<high_dimension1;dimens ion1b++)
    {
    for(dimension2b;dimension2b<high_dimension2;dimens ion2b++)
    {
    cout<<"["<<array[dimension1b][dimension2b]<<"]";
    }
    cout<<"\n";
    }
    return 0;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    first off, useCode Tags

    secondly, it might help a bit to use shorter variable names; i can hardly read it (again code tags might also help)

    fix that and you're more likely to get responses...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int fill_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2,int low_random,int high_random)

    This should be:
    int fill_array(int array[][5],int low_dimension1,int high_dimension1,int low_dimension2,int high_dimension2,int low_random,int high_random)

    >int display_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2)

    This should be:
    int display_array(int array[][5],int low_dimension1,int high_dimension1,int low_dimension2,int high_dimension2)

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    23
    This now works:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    int fill_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2,int low_random,int high_random);
    int display_array(int array[][5],int low_dimension1,int low_dimension2,int high_dimension1,int high_dimension2);
    int main()
    {
     time_t seconds;
     time(&seconds);
     srand((unsigned int)seconds);
     int fg[5][5];
     fill_array(fg,0,5,0,5,1,6);
     display_array(fg,0,5,0,5);
     system ("pause");
     return 0;
    }
    int fill_array(int array[][5],int low_dimension1,int high_dimension1,int low_dimension2,int high_dimension2,int low_random,int high_random)
    {
     int dimension1a,dimension2a;
     for(dimension1a=low_dimension1;dimension1a<high_dimension1;dimension1a++)
     {
      for(dimension2a=low_dimension2;dimension2a<high_dimension2;dimension2a++)
      {
       array[dimension1a][dimension2a]=rand()%(high_random-low_random+1)+low_random;
      }
     }
     return 0;
    }
    int display_array(int array[][5],int low_dimension1,int high_dimension1,int low_dimension2,int high_dimension2)
    {
     int dimension1b,dimension2b;
     for(dimension1b=low_dimension1;dimension1b<high_dimension1;dimension1b++)
     {
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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