Need Help Please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MonkeyHater
    New Member
    • Nov 2008
    • 13

    Need Help Please

    Hey, I have been browsing the site for awhile and find it quite impressive. I couldn't find an article that quite helped me out. I was curious as how to change the following program into pointer notation. Replies are much appreciated.

    using namespace std;

    const int row = 4;
    const int col = 5;
    int findMax(int [row][col]);
    int findMaxC(int [row][col]);

    int main()
    {
    int nums[row][col] = {16, 22, 99, 4, 18, -258, 4, 101, 5, 98,
    105, 6, 15, 2, 45, 33, 88, 72, 16, 3};
    int rn, cn;
    for (rn = 0; rn < row; rn++)
    {
    for (cn = 0; cn < col; cn++)
    cout <<setw(4) <<nums[rn][cn];
    cout <<endl;
    }
    rn=0;
    cn=0;
    cout <<endl;
    findMaxC(nums);
    cout <<"\nThe max value in the array is: " <<findMax(num s)
    <<endl <<endl;

    system("pause") ;
    return 0;
    }

    int findMax(int n[row][col])
    {
    int i, j, max=0;
    int maxrow=0;
    for (i=0;i<4;i++)
    {
    for (j=0;j<5;j++)
    {
    if (n[i][j] > max)
    {
    max = n[i][j];
    }
    if (n[i][j] > maxrow)
    {
    maxrow = n[i][j];
    }
    }
    cout <<"Maximum number in Row number " <<i+1 <<" is " <<maxrow <<endl;
    maxrow=0;
    }
    return max;
    }
    int findMaxC(int n[row][col])
    {
    int i, j, maxcol=0;
    for (j=0;j<5;j++)
    {
    for (i=0;i<4;i++)
    {
    if (n[i][j] > maxcol)
    {
    maxcol = n[i][j];
    }
    }
    cout <<"Maximum number in Col number " <<j+1 <<" is " <<maxcol <<endl;
    maxcol=0;
    }
    cout <<endl;
    return 0;
    }


    Thanks again.
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    What do you mean by converting to pointer notation? Do you want to use dynamic arrays instead of static arrays?

    Comment

    • MonkeyHater
      New Member
      • Nov 2008
      • 13

      #3
      Correct. I understand how to do it for one dimensional arrays, but don't have a clue for two dimensional arrays.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        FYI: I chopped off the hijacker; feel free to continue in your thread.

        kind regards,

        Jos (moderator)

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          A two dimentional array is, of course, an array of one dimensional arrays. To declare a two dimensional dynamic array, use two asterisks instead of one, then allocate space for a lot of one dimensional arrays:
          Code:
          int **nums = new int*[row];
          Then use a loop to allocate space for each of the one dimensional arrays separately:
          Code:
          for (int i = 0; i < row; i++)
              nums[i] = new int[col];
          Deleting is similar to allocating; first delete the one dimensional arrays, then delete the array of arrays:
          Code:
          for (int i = 0; i < row; i++)
              delete[] nums[i];
          Then:
          Code:
          delete[] nums;
          Don't get these in the wrong order or you'll get a segmentation fault.
          By the way, it would be helpful if you used code tags if you are posting some of your code here again. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box like the above code and the indentation is preserved. Thanks.
          I hope this is somewhat helpful.

          Comment

          • MonkeyHater
            New Member
            • Nov 2008
            • 13

            #6
            Thank you so much, that explained exactly what I needed. Also thank you for the embedded code information.

            Comment

            Working...