Hi everybody,
I have a problem and if you could help me as soon as possible I'll really appreciated
I have finished a C++ program but I've got a problem in a small part of the program ,I tried to solve it many times but so far I couldn't figure it out .
My problem was calling the two Dimensional array which is defined within main function.

To make it easier , Let's see the following :
_We have two dimensional array.
_User enters the two dimensions then we create 2d array inside the main function .
_ we want to write a function that takes such an array and its dimensions as its inputs and outputs many things such as maximum value in the array .

My problem as you know was you must define the other dimension in the function such as :
void display(int array [][3]){}
but since the user is the one who enters the number of rows and columns , then we call the function inside main function [ int main(){} ]
so, we don't know the dimensions until the user enters them .

Is there a solution for that ?
If yes , please give me an example (code),,,,,

#include<iostream>


using namespace std;

 
void function(int array[][],int r ,int c)
{   
	
	/*here we r going to perform many procedures such 
	as finding Maximum integer in the array*/
}

int main()
{  int ro,co;

	cout<<"enter rows"<<endl;
	cin>>ro;
	cout<<"enter columns"<<endl;
	cin>>co;
	int ar[ro][co];    
	


	
	return 0;
}

Recommended Answers

All 11 Replies

Hi,

you can use vector (from library of c++) :

vector<vector<int>> ar(ro);
for(int i = 0; i < ro; i++)
ar[ro].resize(co);

and if you don't, you must do this :

int **ar;

ar=new int*[ro]; 
for(int i=0; i<ro; ++i)
ar[i]=new int[co];

Arrays are EVIL. Use a vector of vectors.
They work similarly to arrays, but have resizing and a bunch of other things, like checking if you don't call for elements outside of the array.
Like this:

#include <vector>
#include <iostream>

using namespace std;

void display(vector<vector<int> > &arr){
	//	do your stuff here
}

int main(){
	vector<vector<int > > arr;
	int w,h;
	cout<<"Width: ";
	cin>>w;
	cout<<"Height: ";
	cin>>h;
	arr.resize(w);
	for(int i=0;i<w;i++)
		arr[i].resize(h);
	//	get the array from the user
	display(arr);
}

That was the preferred solution.
Another would be to make a 1-dimension array of size w*h, then refer to items like arr[y*cols+h] . But still, it's not recommended, as dynamic allocation is prone to erros, although unaviodable.

Actually, making it one dimension (instead of two) is desirable with vectors too, since with two-dimensional arrays (or array-like classes) there is no assurance that all the numbers will be in one block.

@Behi Jon:
The >> for template class declaration was added in c++ OX, there should be a space in there, like > >

@jaskij :
thanks for your attention,
but I always use it without space in visual c++ .
Is that a syntax error ?

The problem is that the assignment said :

write a function that takes a 2d array and its dimensions as its inputs and outputs many things such as maximum value in the array

so , I think using a vector will be considerd as a failure + I have never use vectors

so , if there is any other way , please tell me about it
otherwise if I must use pointers can you please see the code and show me how they are used because I'm not very good with pointers

#include<iostream>


using namespace std;


 
void function(int Array[][],int r ,int c)
{   int i,j;
	
		for (i=0;i<r;i++){ 
    	for (j=0;j<c;j++)
		cout<<ARR[i][j]<<"   ";}
		cout<<endl<<endl; }
		
	}

int main()
{  int ro,co;

	cout<<"enter rows"<<endl;
	cin>>ro;
	cout<<"enter columns"<<endl;
	cin>>co;
	int ARR[ro][co];    
	


	
	return 0;
}

Learn pointers. Just do it.
Loads of more advanced problems require them.

The solution provided by Behi John seems legit, so if you HAVE TO use a 2-d array, use that, but that's a stupid thing IMO.
Or you can represent the 2d array as a 1d one in the memory, see the code below.
Iterating over it may get a little tricky, but it's nothing you can't debug :)

#include <iostream>
#include <new>

void func(int *arr,int cols,int rows){
	for(int i=0;i<cols;i++)
		for(int j=0;j<rows;j++)
			cout<<arr[j*cols+i];
}

int main(){
	int rows,cols;
	cin>>rows;
	cin>>cols;
	int *arr=new int[rows*cols]
	func(arr);
}

@Behi John: either you somehow have the cutting edge of c++ (c++ 0x standard was approved 12th of August 2011) in your compiler or it's the non-standard Microsoft way, not sure which.

Can you please explain the followings in details :

arr[j*cols+i];
like why did say [j*cols+i]?
and
int *arr=new int[rows*cols]

Please use code or icode tags. arr[j*cols+i] is just addressing an element in the array.

Consider the example:

1 2 3
4 5 6
7 8 9

Becomes 1 2 3 4 5 6 7 8 9 in this representation. I suppose you can figure out how it works from here. int *arr=new int[rows*cols] just allocates the memory for the array. How else did you want to do it?

jaskij and Behi Jon

Thank you very much

last question , is it a must to create the array dynamically and why ?

int *arr=new int[rows*cols]

Dynamic allocation of memory here is a must, since the user determines the size of the arrays.

Theoretically you could approximate the maximum size of the array and statically declare it with that size, but that is not a good idea, since it wastes memory and such.

Thanks again

appreciated ur time and effort

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.