Computer Science, asked by yashpdhumal4373, 1 year ago

What is one dimensional array in data structure?

Answers

Answered by Dk971422gmailcom
0
Suppose you want to store age of 50 students using a programming language. This can be done by taking 50 different variables for each student. But it is not easy to handle 50 different variables for this. An alternative to this is arrays where we need not declare 50 variables for 50 students. When a list of data item is specified under one name using a single subscript, then such a variable is called a one dimensional(1-D) array. An array is declared in following way:

datatype array-name[size]

where type specifies type of element such as int, float, char etc .and size indicates maximum number of elements that can be stored inside the array.

For example, to represent age of 5 students( in C /C++ language), we can declare it as

int age [5]

An array is represented in memory in contiguous memory location as shown below

In above figure 0,1,2...4 are called index of an array. Index of an array start from 0 to (size-1).

Initialization of 1-D array

Elements of an array can be initialized after an array is declared. An array can be declared at Compile time or at Run time

Compile time initializationint age[5] = {20,21,19,23,25}

Here first element of age is initialized to 20, second to 21 and so on.

Run time initializationfor( int i =0; i<5;i++){ age[i]= 20; }

Here all elements of array age are initialized to 20

Similar questions