Faculty CIM
Report
Labaratory Work nr.4
on Computer Programming realized in C
V-10
Accomplished : st.gr.FAF-081
Colun Mihai
Kulev Mihail
Chisinau 2008
LABWORK NR.4
Subject: Dynamic memory allocation for 2-dimensional array.
Use of function in a menu.
Objective : Studying the ways of pragramming in C of
algorithm for processing dynamic memory allocation for
2-dimensional arrays using functions.
Problem Create the menu of operation for an array n*m:
1. Allocate memory for array.
2. Create the array from keyboard.
3. Random create an array.
4. Sort odd columns in decrease using the Bubble Sort.
5. Type the array on screen.
6. Free memory for array.
7. Exit the program.
Work course: In this Labwork i have to create a menu which
will allow me to operate with a 2-dimensional array,the structure
of menu is show up.For such case i must use the switch
statement which allows me to choose the number of
operation.First of all i must create functions for each operation.
Function SORT:sort an 2-dimensional array,odd columns are sorted in
decrease.
Function ReadKey:creats from keyboard a 2-dimensional array.
Function ReadRand:creates an array with random elements between
-50 and 50.
Function Type:Display the current array on screen.
STOP
FOLWCHART
Function SORT
Entrance
j=0;j<m;j++
j
%2==
1
i=0;i<n-1;i+
+
k=0;k<(n-1i);k++
STOP
No
A[k][j]<
A[k+1][j]
Yes
t=A[k][j];
A[k][j]=A[k+1][j];
A[k+1][j]=t;
Function ReadKey
Entrance
insert elements:
i=0;i<n;i++
j=0;j<m;j++
A[i][j]
STOP
Function ReadRand
Entrance
i=0;i<n;i++
j=0;j<m;j++
A[i][j]=random(50)-random(50)
STOP
Function Type
Entrance
n*m==
0
Yes
No
Matrix is empty
j=0;j<m;j++
STOP
A[i][j]
j=0;j<m;j++
Matrix is empty
Function Main
Start
MENU:
1:Memory allocation;
2:Reading array from keyboard;
3:Reading array random;
4:Sorting odd columns in deacrease;
5:Display Array;
6:Free Memory;
7:Stop the program.
N, m
1
Yes
choose next operation:
L==
1
No
Yes
A=(int**)malloc(n*m*sizeof(int))
Yes
n*m==
0
Yes
No
ReadKey(A,n,m)
L==
2
No
Yes
L==
3
n*m==
0
Yes
n, m
No
ReadRand(A,n,m
)
No
L==
4
n, m
Yes
sort(A,n,m)
No
L==
5
Yes
No
Type(A,n,m)
free(A);
n=0;
m=0;
L==
6
Yes
No
L==
7
No
wrong operation
Yes
STOP
Code of Program:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
void sort(int**A,int n,int m)
{
int i,j,k,t;
for(j=0;j<m;j++)
if(j%2==1)
{
for(i=0;i<n-1;i++)
for(k=0;k<n-1-i;k++)
if(A[k][j]<A[k+1][j])
{
t=A[k][j];
A[k][j]=A[k+1][j];
A[k+1][j]=t;
}
}
}
void ReadKey(int** A,int n,int m)
{
int i,j;
printf("\ninsert elements:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
printf("\nA[%d][%d]=",i,j);
scanf("%d",&A[i][j]);
}
}
void ReadRand(int**A,int n,int m)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
A[i][j]=random(50)-random(50);
}
}
void Type(int**A,int n,int m)
{
int i,j;
case 4:sort(A,n,m);break;
case 5:Type(A,n,m);break;
case 6:{free(A);n=0;m=0;}break;
case 7: exit (1);
default:{printf("\nwrong operation\n");break;};
}
}
}
Results on screen:
Check:
In our case we will check this case in printscreen.On screen has
appearead the menu with operations,next we introduced the
dimensions of array,then allocate memory for it,then creats an
array with random variables,next display the initial array,next
sort it and display it again,then delete it and again display,we
can see the mesageMatrix is empty.Sorting is true.
Conclusion:after computing the program i can say that
allocation memory its an important thing in working with array.