c multi dimensional arrays with programming examples


Multidimensional Arrays
They simulate the same architectural definition as one - dimensional arrays,
except that a separate pair of square brackets is required for each subscript.
Syntax:
<data_type> <array –name> [exp1] [exp2] [exp3] … [exp n];
The RIGHT MOST SUBSCRIPT INCREASES most RAPIDLY.
The LEFT MOST SUBSCRIPT INCREASES least RAPIDLY.
The elements of a two dimensional array will be assigned by rows.
The natural order in which the initial values are assigned can be altered by
framing groups of initial values enclosed in braces.
col1 col2 col3 col n-2 col n-1 col n
row1 …………
A[0][0] A[0][1] A[0][2] A[0][n-3] A[0][n-2] A[0][n-1]
Accept a two – dimensional matrix and printing the sum of the elements
Program
#include <stdio.h>
# define R 2
# define C 2
void main ( )
{
int j, k, A[R][C], sum = 0;
printf("\nEnter The Elements For Matrix A : ");
for (j = 0; j < R; j++)
{
for(k = 0; k < C; k++)
{
printf("\nEnter A[%d][%d] : ", j, k);
scanf("%d", &A[j][k]);
}
}
printf("\nProcessing The Matrix :\n\n\n");
for(j = 0; j < R; j++)
{
for(k = 0; k < C; k++)
{
printf("\t%4d", A[j][k]);
sum = sum + A[j][k];
}
printf("\n\n\n");
}
printf("\nThe Sum of All Elements in the Matrix is : %d", sum);
}

Program to Find Sum of two Matrices
Program
#include <stdio.h>
#define R 3
#define C 3
void main()
{
int j, k, A[R][C], B[R][C], D[R][C];
printf("\nEnter Elements For Matrix : " );
for(j = 0; j < R; j++)
{
for(k = 0; k < C; k++)
{
printf("\nEnter Elements For A[%d][%d] : ", j, k);
scanf("%d", &A[j][k]);
}
}
for(j = 0; j < R; j++)
{
for(k = 0; k < C; k++)
{
printf("\nEnter Elements For B[%d][%d] : ", j, k);
scanf("%d", &B[j][k]);
}
}
printf ("\nProcessing The Matrix :\n\n");
for(j = 0; j < R; j++)
{
for(k = 0; k < C; k++)
{
D[j][k] = A[j][k] + B[j][k];
printf("\t%4d", D[j][k]);
}
printf ("\n\n\n");
}
}








Program to Transpose a Matrix:
Program
#include <stdio.h>
#define R 3
#define C 3
void main ( )
{
int A[R][C], B[R][C], j, k;
printf("\nEnter Elements For Matrix :");
for(j = 0; j < R; j++)
{
for(k = 0; k < C; k++)
{
printf("\nEnter Element For A[%d][%d] ", j, k);
scanf("%d", &A[j][k]);
}
}
printf("\nProcessing The Matrix : \n\n");
for(j = 0; j < R; j++)
{
for (k = 0; k < C; k++)
B[j][k] = A[k][j];
}
printf("\nPrinting The Transpose : \n\n");
for(j = 0; j < R; j++)
{
for(k = 0; k < C; k++)
printf ("\t%4d", B[j][k]);
printf ("\n\n");
}
}
Passing Arrays as Arguments To Functions
An array can be passed to a function as an argument in a manner that is
similar to passing ordinary variables.
To pass an array to a called function, it is sufficient to list the names of the array, without any subscripts and the size of the array as arguments
The called function with array as parameters contains minimum two parameters, the array name and the size of the array.
Syntax:
<ReturnType> <function-name>(<Datatype> <arrayname>[array_size]);
When we pass an array as an argument to a function we need not specify
the array size in the arguments declaration part of the function.
The array name is followed by empty square bracket.

Things to Note:
When an array is passed as an argument to a function...
Remember one main distinction from passing ordinary variables, if a
function changes the value of an array elements, the changes will be made
to the original array that was passed to the function.
When the arrayname is passed the entire Array is Passed as an argument to
the Definition of the Function. The contents of the array are not copied into
the formal parameters, instead information about the memory addresses of
the array elements are passed to the function.
Any changes introduced to the array elements are truly reflected in the
original array in the calling function.
If we pass only one element of the array as argument to a function, then
this element is treated as ordinary variable, and a copy of it is made and is
passed to the array.
Any changes made to the array element passed as variable in the called
function, is not reflected back in the calling function.
To Accept and Display element using Arrays and Functions.
Program
#include <stdio.h>
void main ( )
{
int A[10], num;
void get_array(int a[ ], int) ;
void print_array(int a[ ], int);
printf("\nEnter the Size of the Array not > 10 : "); scanf("%d", &num);
get_array(A, num);
print_array(A, num);
}
void get_array(int A[ ], int n)
{
int k;
for (k = 0; k < n; k++)
{
printf("\nEnter A[%d] Element : ", k);
scanf("%d", &A[k]);
}
return;
}
void print_array(int A[ ], int n)
{
int k;
for (k = 0; k < n; k++)
printf("\nThe Element in A[%d] is %d.", k, A[k]);
return;
}

To Sort an Array using BUBBLE-SORT NETGID
Program
#include<stdio.h>
#include<conio.h>
void main( )
{
int A[10], num;
void get_array(int A[ ], int);
void print_array(int A[ ], int);
void bubble_sort(int A[],int);
printf ("\nEnter the Size of the Array not > 10 : "); scanf ("%d", &num);
printf("\n\nAccepting the Data for Sorting...Please Wait.\n");
get_array (A, num);
printf("\n\nTo See the Original Order of the Given Data...Press Any Key.\n");
getch();
print_array(A, num);
printf("\nCalling the Sorting Routine Please Wait...\n");
bubble_sort (A, num);
printf("\nTo See the Data in Sorted Form...Press Any Key.\n");
getch();
print_array (A, num);
getch();
}
void get_array(int A[ ], int n)
{
int k;
for (k = 0; k < n; k++)
{
printf("\nEnter A[%d] Element : ", k);
scanf("%d", &A[k]);
}
return;
}
void print_array(int A[ ], int n)
{
int k;
printf("\n\nThe Present Order of the Elements are : ");
for (k = 0; k < n; k++)
printf("%d,", A[k]);
return;
}
void bubble_sort (int A[ ], int n)
{
int k, j, temp;
for(k = 0; k < n; k++)
{
for(j = k + 1; j < n; j++)
{
if (A[k] > A[j])
{
temp = A[k];
A[k] = A[j];
A[j] = temp;
}
}
}
return;
}

Search Methodologies Using Array
In order to access or retrieve data through an array searching is necessitated.
Searching enables only the required element of the array to be retrieved for processing purposes.
Search Methods:
Sequential or Linear Search.
Binary or Division Search.
Sequential or Linear Search:
Most elementary search algorithm in use. All elements of the array are accessed in a sequential manner starting from the first element and a comparison between the element of array and the search value is done.
The search continues in a sequential manner till the array element matches the searched value or the end of the array is reached.
Linear Search Implementation:
Program
#include <stdio.h>
void main()
{
int A[10], key, k;
printf ("\n\nEnter Data into The Array : ");
for (k = 0; k < 10; k++)
{
printf ("\nEnter A[%d] Index Element : ", k);
scanf ("%d", &A[k]);
}
printf ("\n\nEnter The Value to Find : ");
scanf ("%d", &key);
for (k = 0; k < 10; k++)
{
if (key == A[k])
break;
}
if (k > 9)
printf ("\nThe Key Value %d is Not Found.", key);
else
printf ("\nThe Key Value %d is Found in Index Number %d at a Positin of %d", key, k,
k+1);
}
Binary or Division Search:
In this method, a SORTED ARRAY is divided into two parts.
The search value is matched with the MIDDLE ELEMENT of the array.
If the search is successful, the operation is complete.
If the search value is less than the middle element …
The search value is in the FIRST UPPER HALF of the array
If the search value is greater than the middle element.
The search value is in the SECOND LOWER HALF of the array.
The process is updated using only that half of the array in which the search
value is expected to the found.
Binary search is fast, when the array to be searched is too large.
Binary search operation can be carried out only on a sorted array.
Binary Search Implementation:
Program
# include <stdio.h>
void main( )
{
int A[10], num, key;
void binary_search(int a[ ], int , int);
void get_array(int a[ ], int);
void print_array(int a[ ], int);
void sort(int a[], int);
printf("\nEnter The Size of The Array : ");
scanf("%d", &num);
get_array(A, num);
sort(A, num);
printf("\nEnter The Element to be Searched : ");
scanf("%d", &key);
binary_search(A, num, key);
print_array(A, num);
}
void binary_search(int A[ ], int n, int m)
{
int low = 0, high = n - 1, mid, flag = 1;
while(low <= high)
{
mid = (low + high) / 2;
if(m < A[mid])
high = mid - 1;
else
if(m > A[mid])
low = mid + 1;
else
if(m == A[mid])
{
printf("\nThe Search is Successful...Printing the Result.");
printf("\n\n%d Found At Location %d.\n", m, mid + 1);
flag = 0;
break;
}
}
if(flag)
printf("\nSearch is Completed but Unsuccessful.");
}
void get_array(int A[ ], int n)
{
int k;
for (k = 0; k < n; k++)
{
printf("\nEnter A[%d] Element : ", k);
scanf("%d", &A[k]);
}
return;
}
void print_array(int A[ ], int n)
{
int k;
printf("\n\nThe Present Order of the Elements are : ");
for (k = 0; k < n; k++)
printf("%d,", A[k]);
return;
}
void sort(int A[ ], int n)
{
int k, j, temp;
for(k = 0; k < n; k++)
{
for(j = k + 1; j < n; j++)
{
if (A[k] > A[j])
{
temp = A[k];
A[k] = A[j];
A[j] = temp;
}
}
}
return;
}


No comments: