C

Topic: Arrays

Explain about Variably Dimensioned Arrays ?

While dealing with Scientific or Engineering problems one is often required to make use of multi-dimensioned array. However, when it comes to passing multidimensional arrays to a function C is found wanting. This is because the C compiler wants to know the size of all but the first dimension of any array passed to a function. For instance, we can define a function compute ( int n, float x[] ), but not compute ( int n, x[][]).Thus, C can deal with variably dimensioned 1-D arrays, but when an array has more than one dimension, the C compiler has to know the size of the last dimensions expressed as a constant. This problem has long been recognized, and some of the solutions that are often used are:Declare the arrays in the functions to be big enough to tackle all possible situations. This can lead to a wastage of lot of precious memory in most cases. Another solution is to construct multiple-dimension array as an array of pointers. For example, a matrix (2-D array) of floats can be declared as a 1-D array of float pointers, with each element pointing to an array of floats. The problem with this method is that the calling function has to define all arrays in this fashion. This means that any other computations done on the arrays must take this special structure into account.Another easy solution, though seldom used, exists. This is based on the following method:Pass the array to the function as though it is a pointer to an array of floats (or the appropriate data type), no matter how many dimensions the array actually has, along with the dimensions of the array. Reference individual array elements as offsets from this pointer.Write your algorithm so that array elements are accessed in storage order. The following program for multiplying two matrices illustrates thisprocedure.# define M 3# define N 2# define P 4float a[M][N], b[N][P], c[M][P] ;void mulmat ( int, int, int, float*, float*, float* ) ;main( ){int i, j ;for ( i = 0 ; i < M ; i++ )for ( j = 0 ; j < N ; j++ )a[i][j] = i + j ;for ( i = 0 ; i < N ; i++ )for ( j = 0 ; j < P ; j++ )b[i][j] = i + j ;mulmat ( M, N, P, a, b, c ) ;for ( i = 0 ; i < M ; i++ ){printf ( "\n" ) ;for ( j = 0 ; j < P ; j++ )printf ( "%f\t", c[i][j] ) ;}}void mulmat ( int m, int n, int p, float *a, float *b, float *c ){float *ptrtob, *ptrtoc ;int i, j, k, nc ;/* set all elements of matrix c to 0 */for ( i = 0 ; i < m * p ; i++ )*( c + i ) = 0 ;for ( i = 0 ; i < m ; i++ ){ptrtob = b ;for ( k = 0 ; k < n ; k++ ){ptrtoc = c ;for ( j = 0 ; j < p ; j++ )*ptrtoc++ += *a * *ptrtob++ ;a++ ;}c += p ;}}We know that C stores array elements in a row-major order. Hence to ensure that the elements are accessed in the storage order the above program uses a variation of the normal matrix-multiplication procedure. The pseudo code for this is given below:for i = 1 to mfor j = 1 to pc[i][j] = 0endfor k = 1 to nfor j = 1 to pc[i][j] = c[i][j] + a[i][k] * b[k][j]endendend

Browse random answers:

Do array subscripts always start with zero?
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
What is the difference between null array and an empty array?
How to remove duplicate elements from an array?
Why is it necessary to give the size of an array in an array declaration?
Array is an lvalue or not?
Does mentioning the array name gives the base address in all the integers? 
what will happen if we try to store more number of elements than specified in array size? 
What is an array?
How to print 2 dimensional array in descending order?
What is Jagged Array?
Write a program to delete an element from an array?
Are arrays value types or reference types?
Assume you have an array that contains a number of strings (perhaps char * a[100]). Each string is a word from the dictionary. Your task, described in high-level terms, is to devise a way to determine and display all of the anagrams within the array (two words are anagrams if they contain the same characters; for example, tales and slate are anagrams.)
How can you sort the elements of the array in descending order? 
What are the 3 different types of arrays?
When does the compiler not implicitly generate the address of the first element of an array?
Is it possible to have negative index in an array?
Difference between arrays and linked list?
Why is it necessary to give the size of an array in an array declaration?
What is an array of pointers?
What is an array of pointers?
Why is it necessary to give the size of an array in an array declaration?
Can the sizeof operator be used to tell the size of an array passed to a function? 
How would you use qsort() function to sort the name stored in an array of pointers to string? 
Are pointers really faster than arrays? 
How can you sort the elements of the array in descending order? 
How to find the row and column dimension of a given 2-D array?
Data structuresHow to distinguish between a binary tree and a tree?Allocating memory for a 3-D array#include "alloc.h"#define MAXX 3#define MAXY 4#define MAXZ 5main( ){int ***p, i, j, k ;p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;for ( i = 0 ; i < MAXX ; i++ ){p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;for ( j = 0 ; j < MAXY ; j++ )p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;}for ( k = 0 ; k < MAXZ ; k++ ){for ( i = 0 ; i < MAXX ; i++ ){for ( j = 0 ; j < MAXY ; j++ ){p[i][j][k] = i + j + k ;printf ( "%d ", p[i][j][k] ) ;}printf ( "\n" ) ;}printf ( "\n\n" ) ;}}
How does C compiler store elements in a multi-dimensional array?
Explain about Variably Dimensioned Arrays ?
Explain about Bit Arrays ?