How 1 Dimension integer array is represented in memory? with example demonstrate the initializing the array elements.

05. a] How 1 Dimension integer array is represented in memory? with the help of suitable example demonstrate the initializing the array elements.

Answer:

One-Dimensional Arrays: A list of items can be given one variable name using only one subscript and such a variable is called a single subscripted variable or one dimensional array.

Memory Representation of 1-D array: One-dimensional arrays are allocated in a contiguous block of memory.

Initialization of One-Dimensional Array: After array is declared, next is storing values in to an array is called initialization. There are two types of array initialization:

  • 1. Compile-time initialization
  • 2. Run-time initialization

1. Compile-time initialization:
If we assign values to the array during declaration it is called compile time initialization. Following are the different methods of compile time initialization.

  • a) Initialization with size
  • b) Initialization without size
  • c) Partial initialization
  • d) Initializing all values zero

a) Initialization with size: we can initialize values to all the elements of the array.
Syntax: data_type array_name[size]={list ofvalues};
Examples: int marks[4]={ 95,35, 67, 87};
float temperature[5]={29.5, 30.7, 35.6, 45.7, 19.5};
b) Initialization without size: We needn’t have to specify the size of array provided we are initializing the values in beginning itself.
Syntax: data_type array_name[ ]={list ofvalues};
Examples: int marks[ ]={ 95,35, 67, 87};
float temperature[ ]={29.5, 30.7, 35.6, 45.7, 19.5};
c) Partial initialization: If we not specify the all the elements in the array, the unspecified elements will be initialized tobzero.
Example: int marks[5]={10,12,20};
Here, marks[3] and marks[4] will be initialized to zero.
d) Initializing all values zero: If we want to store zero to all the elements in the array we can do.
Examples: int marks[4]={0};
float temperature[5]={0};

2.Run time initialization:

Run time initialization is storing values in an array when program is running or executing.

Following example illustrates run time storing of values using scanf and for loop:

Example:

printf(“Enter the marks”); 
for(i=0; i<4; i++)
{
scanf(“ %d”, &marks[i];
}

Note: Every iteration of for loop will help us to fill values into marks array (i.e. marks[0]=95, marks[1]=35, marks[2]=67, marks[3]=87)

Leave a Reply

Your email address will not be published. Required fields are marked *