473,473 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to pass a 2D array to a function C++

11 New Member
hiii
i want to pass a 2D array to a function .. the size of the array is not fixed .. i have tried using pointer to the array ..but it is giving error ..
what i wrote was :
Expand|Select|Wrap|Line Numbers
  1.        void fun(int *,int,int);
  2. int main()
  3. {
  4.  
  5. int ht,wd;
  6. cin>>ht>>wd;
  7. int arr[ht][wd];
  8. int fun(&arr,ht,wd);
  9. for(int k=0;k<ht;k++)
  10. {
  11.  
  12.     for(int l=0;l<wd;l++)
  13.     {
  14.     cout<<arr[k][l];
  15.     }
  16. }
  17.  
  18. }
  19.  
  20. void fun(int*a,int h,int w)
  21. {
  22. for(int i=0;i<h;i++)
  23. {
  24.     for(int j=0;j<w;j++)
  25.     {
  26.         cout<<"roe"<<i<<"  ";
  27.         cin>>*(a+i*w+j);
  28.     }
  29. }
  30. }
  31.  
can some1 help me wid dis .. i m fairly new to c++
Jun 13 '07 #1
5 4185
Extremist
94 New Member
Please remember CODE tags

I'm not really seeing what you are asking, or what your code is doing
I would suggest that you get to know pointers first
You are asking how to pass a 2D array to a function C++
Wel, here is some code for that

First declare a 2D array
Expand|Select|Wrap|Line Numbers
  1. int ht=0, wd=0;
  2.     cout << "Please enter the size of the array:  " << endl;
  3.     cin >> ht;
  4.     cin >> wd;
  5.  
  6.     int **array;               // Declaration, however, your declaration is also correct
  7.  
  8.     *array = new int[ht]; // Allocate some memory
  9.     for(int i = 0; i < ht; i ++)
  10.     {
  11.         array[i] = new int[wd];
  12.         for(int j = 0; j < wd; j ++)
  13.         {
  14.             array[i][j] = 0;
  15.         }
  16.  
  17.     }
  18.  
Passing that as an argument to a function would be :
Expand|Select|Wrap|Line Numbers
  1. void fun(int ** array, int ht, int wd)
  2. {
  3.     for(int i = 0; i < ht; i ++)
  4.     {
  5.         for(int j = 0; j < wd; j ++)
  6.         {
  7.             cout << array[i][j] << "\t";
  8.         }
  9.         cout << endl;
  10.     }
  11. }
calling that in the main function :
Expand|Select|Wrap|Line Numbers
  1.   fun(array, ht, wd);
Jun 13 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
This is not correct:

I'm not really seeing what you are asking, or what your code is doing
I would suggest that you get to know pointers first
You are asking how to pass a 2D array to a function C++
Wel, here is some code for that

First declare a 2D array

Code: ( text )
int ht=0, wd=0;
cout << "Please enter the size of the array: " << endl;
cin >> ht;
cin >> wd;

int **array; // Declaration, however, your declaration is also correct

*array = new int[ht]; // Allocate some memory
for(int i = 0; i < ht; i ++)
{
array[i] = new int[wd];
for(int j = 0; j < wd; j ++)
{
array[i][j] = 0;
}

}

Passing that as an argument to a function would be :

Code: ( text )
void fun(int ** array, int ht, int wd)
{
for(int i = 0; i < ht; i ++)
{
for(int j = 0; j < wd; j ++)
{
cout << array[i][j] << "\t";
}
cout << endl;
}
}

calling that in the main function :

Code: ( text )
fun(array, ht, wd);
An int** is a pointer-to-a-pointer-to-a-single-int. It is not a pointer to a 2D array.

For example, this array:
Expand|Select|Wrap|Line Numbers
  1. int arr[3][4];
  2.  
has three elements. Each element is an array of 4 int. Now, the name of the array (arr) is the address of element 0. Therefore,the name of the array is trhe address of an array of 4 int. The address of an array of 4 int is declared as:

Expand|Select|Wrap|Line Numbers
  1. int (*ptr)[4];
  2.  
That is what you use in your function calls. You will need a second argument for the number of these arrays in the array used on the call:
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int (*ptr)[4], int num)
  2. {
  3.  
  4. }
  5.  
  6. int main()
  7. {
  8.     {
  9.         int arr[3][4];
  10.         MyFunction(arr, 3);
  11.  
  12.     }
  13. }
  14.  
If your array is allocated on the heap using the new operator, the address returned will be the address of an array of 4 int:

Expand|Select|Wrap|Line Numbers
  1. int (*arr)[4]   = new int[3][4];
  2.  
Jun 13 '07 #3
emaghero
85 New Member
To pass a 2D array to a function proceed as follows

1. Declare memory for the array

Expand|Select|Wrap|Line Numbers
  1. int row,column;//Declare variables to hold the size of your array
  2.  
  3. cout<<"Input the size of your array\n";
  4. cout<<"\n";
  5. cout<<"Input the number of rows\n";
  6. cin>>row;
  7. cout<<"Input the number of columns\n";
  8. cin>>column;
  9.  
  10. double *arr=new(double [row*column]);
  11. //This declares memory for a dynamically allocated array of doubles
  12.  
  13. //It is always a good idea to initialise the array to zero before you use it
  14. for(int i=0;i<row;i++){
  15.     for(int j=0;j<column;j++){
  16.          *(arr+i*column+j)=0.0;
  17.     }
  18. }
  19.  
  20.  
2. Define the function to which the array is to be passed

Expand|Select|Wrap|Line Numbers
  1. void function(double *array,int number_of_rows, int number_of_columns)
  2. {
  3.          //This function will print the array to the screen
  4.           for(int i=0;i<number_of_rows;i++){
  5.               for(int j=0;j<number_of_columns;j++)
  6.                    cout<<*(array+i*number_of_columns+j)<<" ";
  7.                    cout<<"\n";
  8.           }
  9. }
  10.  
3. Pass the array to the function by calling it

Expand|Select|Wrap|Line Numbers
  1. //Perform some operation on the array here
  2.  
  3. //Call the function which passes the array
  4. function(arr,row,column);
  5.  
  6.  
Jun 14 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
To pass a 2D array to a function proceed as follows

1. Declare memory for the array


Code: ( text )
int row,column;//Declare variables to hold the size of your array

cout<<"Input the size of your array\n";
cout<<"\n";
cout<<"Input the number of rows\n";
cin>>row;
cout<<"Input the number of columns\n";
cin>>column;

double *arr=new(double [row*column]); //This declares memory for a dynamically allocated array of doubles

//It is always a good idea to initialise the array to zero before you use it
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
*(arr+i*column+j)=0.0;
}
}
This is not allocating a 2D array. It is allocating a one-dimensional array.

Read my previous post.
Jun 14 '07 #5
sarada purkait
11 New Member
To pass a 2D array to a function proceed as follows

1. Declare memory for the array

Expand|Select|Wrap|Line Numbers
  1. int row,column;//Declare variables to hold the size of your array
  2.  
  3. cout<<"Input the size of your array\n";
  4. cout<<"\n";
  5. cout<<"Input the number of rows\n";
  6. cin>>row;
  7. cout<<"Input the number of columns\n";
  8. cin>>column;
  9.  
  10. double *arr=new(double [row*column]);
  11. //This declares memory for a dynamically allocated array of doubles
  12.  
  13. //It is always a good idea to initialise the array to zero before you use it
  14. for(int i=0;i<row;i++){
  15.     for(int j=0;j<column;j++){
  16.          *(arr+i*column+j)=0.0;
  17.     }
  18. }
  19.  
  20.  
2. Define the function to which the array is to be passed

Expand|Select|Wrap|Line Numbers
  1. void function(double *array,int number_of_rows, int number_of_columns)
  2. {
  3.          //This function will print the array to the screen
  4.           for(int i=0;i<number_of_rows;i++){
  5.               for(int j=0;j<number_of_columns;j++)
  6.                    cout<<*(array+i*number_of_columns+j)<<" ";
  7.                    cout<<"\n";
  8.           }
  9. }
  10.  
3. Pass the array to the function by calling it

Expand|Select|Wrap|Line Numbers
  1. //Perform some operation on the array here
  2.  
  3. //Call the function which passes the array
  4. function(arr,row,column);
  5.  
  6.  
thankyou .. i can do it nw
Jun 15 '07 #6

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

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
6
by: Kenny | last post by:
Hello, can anyone tell me how to pass an array to a function ? I have this function , part of my class. It works if I do not put in int a everywhere , but obviously , I need to add an array so I...
2
by: Te-Jung Lo | last post by:
the following is the original code i wrote #include <stdio.h> #include <stdlib.h> //--- Prototype ---// void bfs(int ); void main()
1
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
3
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.