Memory Allocations:
Static Memory Allocation - Static Memory is allocated for declared variables by the compiler during compile time.
Dynamic Memory Allocation - Memory allocation is done at the run time is known as dynamic memory allocation. For Dynamic memory allocation we use malloc function.
Creating a 1D array dynamically:
We first declare a pointer and then use malloc function to create an array in heap. We then assign the values to the dynamic array. After that we can display the dynamic array using loops.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p;
int i;
p = (int *)malloc(5 * sizeof(int));
p[0] = 1;
p[1] = 2;
p[2] = 3;
p[3] = 4;
page-7
p[4] = 5;
printf("\n");
for (i = 0; i <=6; i++)
printf("%d ", p[i]);
return 0;
}
Creating a 2D array dynamically:
We first declare a pointer array that is created in the stack and then use malloc function to create 3 different arrays in the heap. After that we can display the dynamic array using loops.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *A[3];
int i, j;
A[0] = (int *)malloc(4 * sizeof(int));
A[1] = (int *)malloc(4 * sizeof(int));
A[2] = (int *)malloc(4 * sizeof(int));
return 0;
}
Increase array size using pointers:
First we declare two pointers a and b. Then we dynamically create an array of size 5 using malloc function and assign to pointer a. After that we assign values to the array. Now we want to increase the size of the array. So we make another dynamic array of size 10 and assign it to b. Now we make the pointer point to b. Then we free a and make b null.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a, *b;
int i;
a = (int *)malloc(5 * sizeof(int));
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
b = (int *)malloc(10 * sizeof(int));
for (i = 0; i < 5; i++)
b[i] = a[i];
free(a);
a = b;
b = NULL;
for (i = 0; i < 5; i++)
printf("%d \n", a[i]);
return 0;
}
1D Array Representation:
Int A[5] = {1,2,3,4,5};
1
2
3
4
5
Add:200 0 1 2 3 4
Address(A[2]) = 200+2*2=204
Address(A[i]) = B + i * w
Here,
B=Base Address
i=Index
w=size of data-type
2D Array Representation:
Row Major representation -
Int A[3][4];
a00 | a01 | a02 | a03 |
a10 | a11 | a12 | a13 |
a20 | a21 | a22 | a23 |
Converting to row-major :
|
|
|
|
|
|
|
|
|
|
|
|
Address(A[i][j]) = B+ [(i * n) + j] * w
Here,
B=Base Address
m=number of rows
n=number of columns
i=row index
j=column index
w=size of data-type
Column Major representation -
Int A[3][4];
|
|
|
|
|
|
|
|
|
|
|
|
Converting to column major :
|
|
|
|
|
|
|
|
|
|
|
|
Address(A[i][j]) = B+ [(j * m) + i] * w
Here,
B=Base Address
m=number of rows
n=number of columns
i=row index
j=column index
w=size of data-type
nD Array Representation:
Row Major representation -
Int A[d1][d2][d3][d4]
Address(A[i1][i2][i3][i4]) =
B + [ (i1*d2*d3*d4) + (i2*d3*d4) + (i3*d4) + i4] * w
Column Major representation -
Int A[d1][d2][d3][d4]
Address(A[i1][i2][i3][i4]) =
B + [ (i4*d3*d2*d1) + (i3*d2*d1) + (i2*d1) + i1] * w
Question: Given an array A[15][20] with base address 3020 and requires 4 bytes of space. Locate A[12][13] in row and column major order.
Solution :
B=3020 , m=15 , n=20 , i=12 , j=13
Row Major Address (A[12][13]) = B+ [(i * n) + j] * w
= 3020+(12*20+13)*4
= 4032
Column Major Address (A[12][13]) = B+ [(j * m) + i] * w
= 3020+(13*15+12)*4
= 3848
Problem 1: Write a C program to transpose matrices.
#include<iostream>
using namespace std;
int main(){
int A[2][3],B[3][2];
for(int i{0};i<2;i++)
{
for(int j{0};j<3;j++)
{
cout<<"Enter "<<i<<" and "<<j<<" : ";
cin>>A[i][j];
}
for(int i{0};i<2;i++)
{
for(int j{0};j<3;j++)
{
B[j][i]=A[i][j];
}
cout<<endl;
}
for(int i{0};i<3;i++)
{
for(int j{0};j<2;j++)
{
cout<<B[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Problem 2: Write a C program to multiply two matrices.
#include<iostream>
using namespace std;
int main(){
int r1,r2,c1,c2;
cout<<"Enter number of rows and columns of matrix A : ";
cin>>r1>>c1;
cout<<"------------------------"<<endl;
cout<<"Enter number of rows and columns of matrix B : ";
cin>>r2>>c2;
cout<<"------------------------"<<endl;
if(r2!=c1)
{
cout<<"Multiplication not possible"<<endl;
return 0;
}
int A[r1][c1];
int B[r2][c2];
//storing matrix A
for(int i{0};i<r1;i++)
{
for(int j{0};j<c1;j++)
{
cout<<"Enter matrix A element of row "<<i+1<<" and column "<<j+1<<" : ";
cin>>A[i][j];
}
}
cout<<"------------------------"<<endl;
//storing matrix B
for(int i{0};i<r2;i++)
{
for(int j{0};j<c2;j++)
{
cout<<"Enter matrix B element of row "<<i+1<<" and column "<<j+1<<" : ";
cin>>B[i][j];
}
}
int C[r1][c2];
for(int i{0};i<r1;i++)
{
for(int j{0};j<c2;j++)
{
C[i][j]=0;
for(int k{0};k<c1;k++)
{
C[i][j]+=(A[i][k]*B[k][j]);
}
}
}
cout<<"Matrix multiplication is : "<<endl;
cout<<" ---------------------- "<<endl;
for(int i{0};i<r1;i++)
{
for(int j{0};j<c2;j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Problem 3: Write a C program to reverse the rows of a matrix.
#include<stdio.h>
#define MAX 20
int main(void)
{
int a[MAX][MAX],i,j,m,n,temp,p,q;
printf("Enter the number of rows : ");
scanf("%d",&m);
printf("Enter the number of columns : ");
scanf("%d",&n);
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
printf("\n");
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("%4d",a[i][j]);
printf("\n");
}
for(p=0,q=m-1; p<q; p++,q--)
/*Interchange rows p and q*/
for(j=0; j<n; j++)
{
temp=a[p][j];
a[p][j]=a[q][j];
a[q][j]=temp;
}
printf("\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("%4d",a[i][j]);
printf("\n");
}
return 0;
}
Problem 4: Write a C program to print matrices spiral form.
#include <stdio.h>
#define MAX 20
int main(void)
{
int a[MAX][MAX],i,j,k,m,n,rStart,cStart,rEnd,cEnd;
printf("Enter value of m (rows): ");
scanf("%d", &m);
printf("Enter value of n (columns): ");
scanf("%d", &n);
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
printf("\n");
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("%4d", a[i][j]);
printf("\n");
}
printf("\n\n");
for(rStart=0,cStart=0,rEnd=m-1,cEnd=n-1; rStart<=rEnd && cStart<=cEnd; rStart++,cStart++,rEnd--,cEnd--)
{
for(k=cStart; k<=cEnd; k++)
printf("%d* ",a[rStart][k]);
for(k=rStart+1; k<=rEnd; k++)
printf("%d$ ",a[k][cEnd]);
if(rStart<rEnd)
for(k=cEnd-1; k>=cStart; k--)
printf("%d# ",a[rEnd][k]);
if(cStart<cEnd)
for(k=rEnd-1; k>=rStart+1; k--)
printf("%d& ",a[k][cStart]);
}
return 0;
}
Problem 5: Write a C program to check if the matrix is symmetric or skew symmetric or not.
#include<stdio.h>
int main(){
int A[3][3]{0,1,-2,-1,0,3,2,-3,0};
int B[3][3];
int x=0,y=0;
for(int i{0};i<3;i++)
{
for(int j{0};j<3;j++)
{
B[j][i]=A[i][j];
}
}
for(int i{0};i<3;i++)
{
for(int j{0};j<3;j++)
{
int count=0;
if(A[i][j]!=B[i][j])
x=1;
if (A[i][j] != -B[i][j])
y=1;
}
}
if(x==0)
printf("symmetric");
if(y==0)
printf("skew symmetric");
else
printf("no");
return 0;
}
In the next tutorial we will learn about array abstract data types where we will insert, delete, reverse , merge and do various operations on arrays.