Connecting Tech Pros Worldwide Forums | Help | Site Map

Need Help Please

Newbie
 
Join Date: Nov 2008
Posts: 13
#1: Nov 5 '08
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.

boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#2: Nov 5 '08

re: Need Help Please


What do you mean by converting to pointer notation? Do you want to use dynamic arrays instead of static arrays?
Newbie
 
Join Date: Nov 2008
Posts: 13
#3: Nov 5 '08

re: Need Help Please


Correct. I understand how to do it for one dimensional arrays, but don't have a clue for two dimensional arrays.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 5 '08

re: Need Help Please


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

kind regards,

Jos (moderator)
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#5: Nov 5 '08

re: Need Help Please


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.
Newbie
 
Join Date: Nov 2008
Posts: 13
#6: Nov 5 '08

re: Need Help Please


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


Similar C / C++ bytes