two dimensional array

This is a discussion on two dimensional array within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that creates a magic square of two dimensions. The size of the array ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    22

    two dimensional array

    I am trying to write a program that creates a magic square of two dimensions. The size of the array needs to be decided by the user so it can't be predefined. I've come across a few different ways to allocate memory for a 2 dimensional array but none without a predetermined size. Is there a way to get this to work?

    Code:
    int main(void)
    {
    int order;
    double TwoDimArray[order][order];
    
    cout <<"This program will create a magic square of an odd order\n";
    cout <<"Please enter the order number you would like:\n";
    cin >> order;
    THANK YOU!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    std::vector<std::vector<double> TwoDimArray(order, std::vector<double>(order));

    Be sure to put it after you read in order.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    I added that after reading 'order' and added header #include <vector> but now it is saying template argument 1 and template argument 2 are invalid errors for that line?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Ah, I missed a >. Updated code:

    std::vector<std::vector<double>> TwoDimArray(order, std::vector<double>(order));
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    sorry but now I am recieving a conflicting declaration error for that line?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    What? Show your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    void MagicSquare ( double matrix, int n );
    void DisplaySquare ( int matrix, int n );
    
    int main(void)
    {
    int order;
    double TwoDimArray[order][order];
    
    cout <<"This program will create a magic square of an odd order\n";
    cout <<"Please enter the order number you would like:\n";
    cin >> order;
    std::vector<std::vector<double> > TwoDimArray(order, std::vector<double>(order));
    if (order%2==1)
        MagicSquare( TwoDimArray, order );
        else{
            while (order%2==0){
            cout <<"Please enter an odd number:\n";
            cin >> order;
            }
            MagicSquare( TwoDimArray, order );
    
        }
    }
    void MagicSquare ( double matrix, int n )
    {
    int max = n * n;
    int i=0;
    int j=n/2;     // start position
    
      for (int k=1; k<=max; ++k)
      {
        matrix[i][j] = k;
    
        i--;
        j++;
    
        if (k%n == 0)
        {
          i += 2;
          --j;
        }
        else
        {
          if (j==n)
            j -= n;
          else if (i<0)
            i += n;
        }
      }
    }
    void DisplaySquare( &matrix, int n)
    {
    int i;
    int j;
      for (int i=0; i<n; i++)
      {
        for (int j=0; j<n; j++)
          printf(" %3d", matrix[i][j]);
    
        printf("\n");
      }
    
      printf("\n\n");
    }
    do i need to change the type of 'order'?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Get rid off double TwoDimArray[order][order].
    Change MagicSquare to accept your new vector and get rid of the printfs (use std::cout).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two-dimensional array
    By BamaMarine06 in forum C Programming
    Replies: 8
    Last Post: 03-01-2010, 12:28 AM
  2. 3 Dimensional Array
    By devarishi in forum C Programming
    Replies: 10
    Last Post: 12-17-2008, 01:52 PM
  3. Replies: 24
    Last Post: 11-11-2008, 12:39 PM
  4. 2 dimensional array
    By chasesg in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 12:12 PM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

Tags for this Thread


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