Thread: Arrays

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Arrays

    i want to load arrays and display arrays that will have numbers 1-15 in this format

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15

    i have got 2 functions
    i will load arrays n second will display

    [code]

    void loadArray()
    {
    int r, c, cnt=1;

    for( r=0; r<5; r++ )
    for( c=0; c<5; c++ )
    data[r][c] = cnt++;
    }

    void displayArray( )
    {
    int r, c, temp;

    cout << "The 5 x 5 2D array:\n\n";

    for (r=0;r<5;r++)
    {
    for(c=0;c<5;c++)
    {
    cout << " " << data[r][c];
    }
    }

    displayArray();
    getchar();

    pause();
    }
    i want to display arrays when ever i call func DisplayArrays();
    Last edited by dayknight; 05-04-2002 at 01:32 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    That is only a 5 x 3 Matrix. Try changing some of the loops accordingly.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    i meant 5x5 array that will count upto 25
    Last edited by dayknight; 05-04-2002 at 04:00 PM.

  4. #4
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    people help!
    Last edited by dayknight; 05-04-2002 at 07:33 PM.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    This is how you may want to write it. If you want them on separate lines simply put an cout << endl; after the end of the outter for loop when displaying. Looking at my code you should be able to see your mistakes. Need anymore help let me know.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    void loadArray( int data[5][5] )
    { 
         int r, c, cnt = 1;
         for( r=0; r<5; r++ )
         {
             for( c=0; c<5; c++ )
             {
                  data[r][c] = cnt++;
             }
         }
    } 
    
    void displayArray( int data[5][5] )
    { 
       int r, c, temp;
    
       cout << "The 5 x 5 2D array:\n\n";
    
       for (r=0;r<5;r++)
       {
          for(c=0;c<5;c++)
          {
             cout << " " << data[r][c];
          }
       }
    }
    
    int main( void )
    {
      int Array[5][5];
    
      loadArray( Array );
      displayArray( Array );
    
      system("pause");
    
      return( 0 );
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM