By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,955 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,955 IT Pros & Developers. It's quick & easy.

Need Help Please

P: 13
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(nums)
<<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.
Nov 5 '08 #1
Share this Question
Share on Google+
5 Replies


boxfish
Expert 100+
P: 469
What do you mean by converting to pointer notation? Do you want to use dynamic arrays instead of static arrays?
Nov 5 '08 #2

P: 13
Correct. I understand how to do it for one dimensional arrays, but don't have a clue for two dimensional arrays.
Nov 5 '08 #3

Expert 10K+
P: 11,448
FYI: I chopped off the hijacker; feel free to continue in your thread.

kind regards,

Jos (moderator)
Nov 5 '08 #4

boxfish
Expert 100+
P: 469
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:
Expand|Select|Wrap|Line Numbers
  1. int **nums = new int*[row];
Then use a loop to allocate space for each of the one dimensional arrays separately:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < row; i++)
  2.     nums[i] = new int[col];
Deleting is similar to allocating; first delete the one dimensional arrays, then delete the array of arrays:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < row; i++)
  2.     delete[] nums[i];
Then:
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 5 '08 #5

P: 13
Thank you so much, that explained exactly what I needed. Also thank you for the embedded code information.
Nov 5 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.