Dynamic Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • achilleas11
    New Member
    • Nov 2006
    • 1

    Dynamic Array

    ///////////// start code /////////////
    #include<iostre am>
    #include<iomani p>

    using namespace std;

    int ReadSquareSize( )
    {
    int x;
    cout<<"Plhktrol oghse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
    while(1)
    {
    cin>>x;
    if(x>=3 && x%2==1)
    {
    return x;
    }
    else
    {
    cout<<"Plhktrol oghse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
    }
    }
    cout<<endl;
    }

    int main()
    {
    int n = ReadSquareSize( );
    int square[n][n]; //----- error here -----
    }

    ///////////////// end code /////////////////

    This is a part from my code. What i cannot understand is why i get 6 errors in the error line. I have declared n from the previous line. Malloc is better to use? If so, can u post the corrected code lines?
  • Colloid Snake
    New Member
    • Nov 2006
    • 144

    #2
    You use 'int main' and you don't use any return statement.

    Try putting return 0; below your array declaration.

    Also, the error messages might help.

    Comment

    • Tomassus
      New Member
      • Nov 2006
      • 5

      #3
      Hi there,

      You can't daclare something like:

      int x;
      cin >> x;
      int a[x];

      It won't compile because x is unknown in the time of compilation. Values in brackets while declaring arrays have to be daclered before compilation.

      U can try to declare 2-dimension array using new. Problem is that in C++ u can't declare 2-dimensional array directly.
      Example u can remake for your code:

      int **a = new int*[x];
      for (int i = 0; i < x; ++i)
      a[i] = new int[y];

      I encourage you to learn about very nice funcion from STL library -> vectors. U can find a use for them in your example.

      Comment

      • sircool
        New Member
        • Oct 2006
        • 12

        #4
        Originally posted by achilleas11
        ///////////// start code /////////////
        #include<iostre am>
        #include<iomani p>

        using namespace std;

        int ReadSquareSize( )
        {
        int x;
        cout<<"Plhktrol oghse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
        while(1)
        {
        cin>>x;
        if(x>=3 && x%2==1)
        {
        return x;
        }
        else
        {
        cout<<"Plhktrol oghse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
        }
        }
        cout<<endl;
        }

        int main()
        {
        int n = ReadSquareSize( );
        int square[n][n]; //----- error here -----
        }

        ///////////////// end code /////////////////

        This is a part from my code. What i cannot understand is why i get 6 errors in the error line. I have declared n from the previous line. Malloc is better to use? If so, can u post the corrected code lines?

        if you use like that definitation
        int square[n][n] that means you made a array static.So n must be const that does not matter you return a n value from any function.
        i will make a dynamic aray declaration you can use

        int **arary,row,col omn; // two asterix means you make two dimentional aray
        than you will request memory (in C malloc, in C++ you can use new function)

        array=new int*[row];
        for(int a=0;a<row;a++) //row=n in your code
        array[a]=new int[colomn]; //request a colomn*sizeof(i nt) memory for each row

        merory management is like that and dynamic arrays can be like that

        keep in touch

        Comment

        Working...