473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(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
5 1287
boxfish
469 Expert 256MB
What do you mean by converting to pointer notation? Do you want to use dynamic arrays instead of static arrays?
Nov 5 '08 #2
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
JosAH
11,448 Expert 8TB
FYI: I chopped off the hijacker; feel free to continue in your thread.

kind regards,

Jos (moderator)
Nov 5 '08 #4
boxfish
469 Expert 256MB
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
Thank you so much, that explained exactly what I needed. Also thank you for the embedded code information.
Nov 5 '08 #6

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

Similar topics

8
by: Nathan Pinno | last post by:
Hi all, I need help figuring out how to fix my code. I'm using Python 2.2.3, and it keeps telling me invalid syntax in the if name == "Nathan" line. Here is the code if you need it. #This...
17
by: Marcin Borkowski | last post by:
Hello I need some small help in the Borland C++ Builder. I need to do something with string in format like "var1|var2|var3|var4|" and add it to some ComboBox (Items -> Add("var1"), Items ->...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
3
by: Bob.Henkel | last post by:
I write this to tell you why we won't use postgresql even though we wish we could at a large company. Don't get me wrong I love postgresql in many ways and for many reasons , but fact is fact. If...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
8
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it....
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.