Thread: dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    dynamic memory allocation

    this is the question; Write a function that builds a two-dimensional multiplication table with arbitrary sizes for the
    two dimensions.

    This is what I have done. I have allowed the user to input whatever size table they want by arbitrarily choosing what value they can input. However I cannot get the board to have blank squares. I thought the char would do it. Any suggestions? thanks

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    char SQAURE_CHAR = {' '};
    
    
    const int Board_Size = 14;
    
    
    int main()
    {
    
    
        int **p_p_multtable;
    
    
        p_p_multtable = new int* [Board_Size];
    
    
        for (int i = 0; i < Board_Size; i++)
        {
            p_p_multtable[i] = new int [Board_Size];
        }
    
    
        for (int i = 0; i < Board_Size; i++)
        {
            for (int j = 0; j < Board_Size; j++)
            {
              p_p_multtable[i][j] = SQAURE_CHAR;
            }
        }
    
    
        for (int i = 0; i < Board_Size; i++)
        {
            delete [] p_p_multtable[i];
        }
    
    
        for (int i = 0; i < Board_Size; i++)
        {
            for (int j = 0; j < Board_Size; j++)
            {
              cout << p_p_multtable[i][j];
              if (j!= Board_Size - 1)
              {
                  cout << " | ";
              }
            }
            cout << endl;
            if (i != Board_Size - 1)
            {
                cout << "----------------------------" << endl;
            }
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    sorry noticed some mistakes changed the int to char but still not getting a blank board. below is rectified code.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    char SQAURE_CHAR = {' '};
    
    
    const int Board_Size = 14;
    
    
    int main()
    {
    
    
        char **p_p_multtable;
    
    
        p_p_multtable = new char* [Board_Size];
    
    
        for (char i = 0; i < Board_Size; i++)
        {
            p_p_multtable[i] = new char [Board_Size];
        }
    
    
        for (char i = 0; i < Board_Size; i++)
        {
            for (char j = 0; j < Board_Size; j++)
            {
              p_p_multtable[i][j] = SQAURE_CHAR;
            }
        }
    
    
        for (char i = 0; i < Board_Size; i++)
        {
            delete [] p_p_multtable[i];
        }
    
    
        for (char i = 0; i < Board_Size; i++)
        {
            for (char j = 0; j < Board_Size; j++)
            {
              cout << p_p_multtable[i][j];
              if (j!= Board_Size - 1)
              {
                  cout << " | ";
              }
            }
            cout << endl;
            if (i != Board_Size - 1)
            {
                cout << "----------------------------" << endl;
            }
        }
        return 0;
    }

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why are you (partially) deleting the array before you print it???

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You change all this maddening and ugly code...

    Code:
        char **p_p_multtable;
     
     
        p_p_multtable = new char* [Board_Size];
     
     
        for (char i = 0; i < Board_Size; i++)
        {
            p_p_multtable[i] = new char [Board_Size];
        }
     
     
        for (char i = 0; i < Board_Size; i++)
        {
            for (char j = 0; j < Board_Size; j++)
            {
              p_p_multtable[i][j] = SQAURE_CHAR;
            }
        }
     
     
        for (char i = 0; i < Board_Size; i++)
        {
            delete [] p_p_multtable[i];
        }
    ...to...

    Code:
    std::vector<std::vector<char>> multtable(Board_Size, std::vector<char>(SQUARE_CHAR));
    The delete is an error, btw. Don't free anything before you've finished using it. But with vector, you don't have to. It's automatic.
    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. Dynamic Memory Allocation
    By tictac232434 in forum C Programming
    Replies: 2
    Last Post: 11-20-2012, 12:48 PM
  2. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  3. Dynamic Memory Allocation
    By JamesD in forum C Programming
    Replies: 10
    Last Post: 03-12-2011, 12:08 AM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. dynamic memory allocation - Please help
    By Space_Cowboy in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2002, 05:20 PM

Tags for this Thread