Multidimensional Array

Multi-dimensional Arrays in C

 

In C programming, Arrays can have any number of dimensions.

if you want to store data as a tabular form, like a table with rows and columns, need to get with multidimensional array.

you can create an array of arrays. These arrays are known as multidimensional arrays

A multidimensional array is basically an array of arrays.

In simple words, an array created with more than one dimension (size) is called a multi-dimensional array. The multi-dimensional array can be of a two-dimensional array or three-dimensional array or four-dimensional array or more.

 

type name[size1] [size2] ...[sizeN];

 Syntax: type name[size1] [size2] …[sizeN];

Example: int a [3][3][3];

int num1[2][3];

float num2[5][6];

Three Dimensional Array:-

datatype name[size_1][size_2][size_3];

Example: -

int students[2][3][4];

float bills[3][4][3];

 

 Memory Representation of array

Array is declared in the program, contiguous memory to it elements are allocated. Initial address of the array – address of the first element of the array is called base address of the array.

Each element will occupy the memory space required to accommodate the values for its type, i.e.; depending on elements datatype.

1, 4 or 8 bytes of memory is allocated for each element.

Total memory allocated to an Array = Number of elements * size of one element

Total memory allocated to an Integer Array of N elements = Number of elements * size of one element
= N * 4 bytes
= 10 * 4 bytes = 40 Bytes, where N = 10
= 500 * 4 bytes = 2000 Bytes, where N = 500

 

Row Major Order

When we store the array elements in row major order, first we will store the elements of first row followed by second row and so on.  Hence in the memory we can find the elements of first row followed by second row and so on. In memory there will not be any separation between the rows.

 

Column Major Order

 This is the opposite method of row major order of storing the elements in the memory. In this method all the first column elements are stored first, followed by second column elements and so on.

Pass arrays to a function in C

In C programming, you can pass an entire array to functions.

passing similar elements as an array takes less time than passing each element to a function as we are only passing the base address of the array to the function, and other elements can be accessed easily as an array is a contiguous memory block of the same data types.

void myFunction(int param[10])

{

   .

   .

   .

}

 

Eg. Pass arrays to a function

Program to calculate the sum of array elements by passing to a function

#include <stdio.h>

float calculateSum(float num[]);

int main()

{

  float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};

  // num array is passed to calculateSum()

  result = calculateSum(num);

  printf("Result = %.2f", result);

  return 0;

}

float calculateSum(float num[]) {

  float sum = 0.0;

  for (int i = 0; i < 6; ++i) {

    sum += num[i];

  }

  return sum;

}


Advantage of Arrays

  • Less amount of code: Using array we can aggregate N variables of same data type in a single data structure. Otherwise, we have to declare N individual variables.
  • Easy access of elements: We can access any element of array using array name and index. We can access all elements serially by iterating from index 0 to size-1 using a loop.
  • Easy to implement algorithms: Certain algorithms can be easily implemented using array like searching and sorting, finding maximum and minimum elements.
  • Random Access: We can access any elements of array.

Disadvantages or Limitations of Arrays

 

·                  Array is Static Data Structure. We cannot change the size of array in run-time.

·                  We must know in advance that how many elements are to be stored in array.

·                  Only elements of same data types can be stored in an array. We cannot store elements of multiple data types in a single array.

·                  As Array elements are stored in consecutive memory locations. So, insertions and deletions of an element is time consuming

·                  As we cannot change the size of an array,


SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

4.Time Management

                                      Time Management   •        Effective time management in project management involves strategic plann...