What is One dimensional Array in C?

A one-dimensional array has one subscript. One Dimensional Array in C(1D) is an array which is represented either in one row or in one column.

The one-dimensional arrays are known as vectors.

In other words, it can be represented as in a single dimension-width or height as shown in the below figure:

array in c

Declaration of One Dimensional Array in C

One dimensional array in C can be declared as:

<data_type> array_name[size];

It allocates memory for size elements. In 1D array subscript of the first element is 0 and subscript of last element size is -1. In 1D array, the size must be a constant.

For Example int rollno[100]; char str[100];

When an array is declared, the compiler reserves or allocates memory to store the entire array. In C, the subscripts always start with 0 and increment by 1, so y[5J is the sixth element.

It is sometimes convenient to define an array size in terms of a symbolic constant rather than a fixed integer quantity.

This makes it easier to modify a program that utilizes an array, since all references to the maximum array size (e.g., within for loops as well as in array definitions) can be altered simply by changing the value of the symbolic constant.

For Example:

#define SIZE 100

char string[SIZE];

Suppose, we have array int score[7] as shown in below figure:

one dimensional array in c

The name of the array is the address of the first element and the subscript is the offset.

The base address or starting address of array score is 1000 (base address is same as address element score[0]) and the size of int be 2 bytes. Then, the address of the second element (score([1]) will be 1002, address of third element (score([2]) will be 1004 and so on.

So total size of the array in bytes = size of the array * size of the datatype.

Example:

int a[40]; total bytes=402=80 bytes. char b[50]; total bytes=501=50 bytes.
double sal[20]; total bytes=20*8=160 bytes.

To find the address of any element in array=base address +(subscript * size of datatype).

For Example int A[40]; Suppose base Address of A is 1000. To find address Of A[5] element, i.e. sixth element- 1000 +(5*2)=1010 .

Initializing One Dimensional Array in C

we can initialize one-dimensional array at compile time and at run time.

1. Compile Time Initialization:

Arrays can be initialized at the time they are declared. This is also known as compile-time initialization.

We can initialize the elements of arrays in the same way as the ordinary variables when they are declared. The general form or syntax of initialization of arrays is:

type array-name[size]={list of values};


(1) Initializing all specified memory Locations:

Arrays can be initialized at the time of declaration when their initial values are known in advance. Array elements can be initialized with data items of type int, char etc.

For Example: int a[5]={1,2,3,4,5};

During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and all these locations are initialized as shown in below figure.

Initializing all specified memory Locations

(2) Partial Array Initialization:

Partial array initialization is possible in C language. If the number of values to be initialized is less than the size of the array.

Then the elements will be initialized to zero automatically in case of the numeric array and in case of a non-numeric array, it will be initialized by space.

Example: int a[5]={0};

partial array

(3) Initialization without Size:

Consider the declaration along with the initialization.

Example: char b[ ]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};

In this declaration, even though we have not specified an exact number of elements to be used in array b. The array size will be set of the total number of initial values specified. So, the array size will be set to 8 automatically.

The array b is initialized as shown in the below figure:

without size

(4) Array Initialization with a String:

Consider the declaration with string initialization.

Example: char b[ ]=”COMPUTER”;

The array b is initialized as shown in the figure below:

with a string

Even though the string “COMPUTER” contains 8 characters, because it is a string, it always ends with a null character. So, the array size is 9 bytes (i.e., string length 1 byte for null character).

2. Run Time Initialization:

An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays.

Example: scanf( ) can be used to initialize an array.

int x[3]; scanf(“%d%d%d”,&x[0], &x[1], &x[2]);

The above statements will initialize array elements with the values entered through the keyboard.

//Learnprogramo
{
for(i=0;i<100;i=i+1)
{
if(i<50)
sum[i]=0.0;
else
sum[i]=1.0;
}

The first 50 elements of the array sum are initialized to 0 while the remaining 50 are initialized to 1.0 at run time.

Accessing One Dimensional Array Elements

We can access an array element using the array name and subscript or index written inside a pair of square brackets [ ]. Remember array indexing starts from 0. Nth element in array is at index N-1.

For Example: Suppose we have an integer array of length 5 whose name is marks.

int marks[5]={1,2,3,4,5};

Now we can access elements of array marks using subscript followed by array name.

(1). marks[0] = First element of array marks=1
(2). marks[1] = Second element of array marks=2
(3). marks[2] = Third element of array marks=3
(4). marks[3] = Fourth element of array marks=4
(5). marks[4] = Last element of array marks=5

Assigning values to Array Elements:

Values can be assigned to individual elements of an array using the assignment operator.

Syntax: array_name[index]=value;

Example: a[0]=10; a[4]=100;

We cannot copy all the elements of an array to another array by simply assigning it to the other array:

For Example: if we have two arrays a[5] and b[5] then,

int a[5]={1,2,3,4,5}; int b[5]; b=a; is not valid.

We will have to copy all the elements of the array one by one, using a loop.

//Learnprogramo
for(i=0; i<5; i++)
  b[i]=a[i];

Sample Programs:

Program 1: Write a program to print the sum of the elements of an array.

//Learnprogramo
#include<stdio.h>
int main()
{
int arr[5], i, s = 0;
for(i = 0; i < 5; i++)
{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}
for(i = 0; i < 5; i++)
{
s += arr[i];
}
printf("\nSum of elements = %d ", s);
return 0;
}

Output:

array in c

Program 2: Program to find the highest and the Lowest elements in an array.

//Learnprogramo
#include<stdio.h>
#define SIZE 10
int main()
{
int my_arr[SIZE] = {34,56,78,15,43,71,89,34,70,91};
int i, max, min; 
max = min = my_arr[0]; 
for(i = 0; i < SIZE; i++)
{
if(my_arr[i] > max)
{
max = my_arr[i];
}
if(my_arr[i] < min)
{
min = my_arr[i];
}
} 
printf("Lowest value = %d\n", min);
printf("Highest value = %d", max); 
return 0;
}

Output:

highest and lowest in array

Conclusion: In this lesson, we have learned about one-dimensional array on C. Now, in the next lesson, we will learn 2d arrays in C.

Also Read: