You are on page 1of 2

Experiment no. 3.

TO PERFORM DECLARATION, INITIALIZATION AND ACCESSING


SINGLE DIMENSIONAL ARRAYS

OBJECTIVES:
· To declare, initialize and access single dimensional arrays.
· To demonstrate a simple basic program utilizing single dimensional arrays in C.

THEORY:
Arrays are a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. Instead of declaring individual variables, such as num0, num1, ... num99, we declare one
array variable such as numbers and use num[0], num[1],... num[99] to represent individual variables. A
one-dimensional array is like a list.

Declaration
Arrays are defined in much the same manner as ordinary variables, except that each array name must
be accompanied by a size specification (i.e., the number of elements). For a one-dimensional array, the
size is specified by a positive integer expression, enclosed in square brackets. The expression is usually
written as a positive integer constant. In general terms, a one-dimensional array declaration may be
expressed as:
data- type array[ size ] ;

Initialization
Arrays may be initialized when they are declared. The initialization data is placed in curly {} braces
following the equals sign. An array may be partially initialized, by providing fewer data items than the
size of the array. The initialization maybe expressed as:
array[ size ] = { value1, value2, value3, …};

Accessing
A specific element in an array is accessed by an index. In general terms, an element of a one
dimensional can be accessed as:
array[ index ] ;

DEMONSTRATION:
Program 1:To reverse a one dimensional array

#include <stdio.h>
int main() {
int values[5];

printf("Enter 5 integers: ");


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Experiment no. 3.1

Output

CONCLUSION:
Hence, from the above experiment we are declare, initialize and access single dimensional arrays and
use them to write programs in C.

You might also like