0% found this document useful (0 votes)
10 views17 pages

Lecture-24 (21-11-23)

The document discusses dynamic arrays in computer programming, explaining how to redefine data types using typedef and the relationship between array and pointer variables. It covers the declaration, usage, and deletion of both 1-D and 2-D dynamic arrays, providing code examples for better understanding. The document emphasizes the importance of managing memory for dynamic arrays in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

Lecture-24 (21-11-23)

The document discusses dynamic arrays in computer programming, explaining how to redefine data types using typedef and the relationship between array and pointer variables. It covers the declaration, usage, and deletion of both 1-D and 2-D dynamic arrays, providing code examples for better understanding. The document emphasizes the importance of managing memory for dynamic arrays in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Problem Solving and

MA144:
Computer Programming

Lecture-24
Dynamic Arrays
Redefine dataTypes using typedef
• We can use typedef to define alias for any type name of definition.

Examples
• typedef int iece;
Now we can define a variable type int as iece a,b;
int a,b; and iece a,b; are equivalent.

• typedef double dece;


Now we can define a variable type double as dece c,d;
double c,d; and dece c,d; are equivalent.
typedef int* iptr;
Now we can define a pointer variable type int as iptr p,q;
int *p,*q; and iptr p,q are equivalent.
Array variables and pointer variables
When an array is declared,
• The compiler allocates sufficient amount of
storage to contain all the elements of the array in
contiguous memory locations
• The base address is the location of the first
element (index 0) of the array
• The compiler also defines the array name as a
constant pointer to the first element
int x[5];
• The array name x is the starting address of the array
• Both x and &x[0] have the same address
• If int *p is declared, then
p=x; and p = &x[0]; are equivalent
• x=p; is illegal,
because x is a constant pointer, so cannot be changed
• We can access successive values of x by using
p++ or p-- to move from one element to another
Relationship between p and x

In general, *(p+i) gives the value of x[i]


int main()
{
int n,i,*p;
cout<<"enter size of array: ";
cin>>n;
int a[n];
cout<<"enter array: "; cout<<"modified array: ";
for(i=0;i<n;i++) for(i=0;i<n;i++)
cin>>a[i]; cout<<*(p+i)<<" ";
p=a;
cout<<"\n modified array
for(i=0;i<n;i++) using a : ";
*(p+i)+=i;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
cout<<*(p++)<<" "; }
int* update(int [],int); // function prototype
int main()
{ int n,i,*p;
cout<<"enter an integer: ";
cin>>n; Program describing
int a[n]; function returning array
cout<<"enter array: ";
for(i=0;i<n;i++)
cin>>a[i];
p=update(a,n); // function call
cout<<"\n the updated array: ";
for(i=0;i<n;i++)
cout<<*(p+i)<<" ";
return 0;
}
//function definition
int* update(int a[],int n)
{ int i;
for(i=0;i<n;i++)
a[i]+=1;

return a;
}
1-D Dynamic Arrays
A dynamic array is an array whose size is not specified
when you write the program, but is determined while
the program is running.

• Declaration It can be variable


int *p;
p=new int [10];
It creates a dynamic array variable with ten array
elements of type int
• Eliminate the dynamic array as
delete [] p;
typedef int* iece;
iece p;
p=new int [10];
int main()
{
int n,i,*p;
cout<<"enter an integer:";
cin>>n;
p=new int [n];
cout<<"enter array: ";
for(i=0;i<n;i++) for(i=0;i<n;i++)
cin>>p[i]; *(p+i)+=i;
cout<<"modified array:";
for(i=0;i<n;i++)
cout<<*(p+i)<<" ";

delete [] p;
return 0;
}
2-D Dynamic Arrays
Pointer to a pointer variable
• Declaration
int **p;
p=new int *[m];
It is an array of m pointers.
Each p[i] can name a dynamic array of n ints as
for(i=0;i<m;i++)
p[i]=new int [n];
It creates a dynamic array p of size m-by-n
• Eliminate the 2-D dynamic array as
for(i=0;i<m;i++)
delete [] p[i];
delete [] p;
Note that elimination is reverse to declaration process
#include<iostream>
using namespace std;

int main()
{
int m, n,i,j,**p;
cout<<"\n enter no. of rows: ";
cin>>m;
cout<<"\ enter no. of columns: ";
cin>>n;
//declaring 2-D dynamic array
p=new int *[m];
for(i=0;i<m;i++)
p[i]=new int [n];
//reading array elements
cout<<"\n enter array:\n ";
for(i=0;i<m;i++)
{ cout<<"enter row "<<i<<" with "
<<n<<" elements: ";
for(j=0;j<n;j++)
cin>>p[i][j];
cout<<endl;
}
//accessing array elements
for(i=0;i<m;i++)
for(j=0;j<n;j++)
p[i][j]+=1;
//printing array
cout<<"\n modified array:\n ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<p[i][j]<<" ";
cout<<endl;
}
//eliminating dynamic arrays
for(i=0;i<m;i++)
delete [] p[i];
delete [] p;
return 0;
}

You might also like