0% found this document useful (0 votes)
11 views

Arrays

Thank you

Uploaded by

nihar0008
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Arrays

Thank you

Uploaded by

nihar0008
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ARRAYS

Introduction

A data structure is the way data is stored in the machine and the functions used to access that
data. An easy way to think of a data structure is a collection of related data items. An array is a
data structure that is a collection of variables of one type that are accessed through a common
name. Each element of an array is given a number by which we can access that element which is
called an index. It solves the problem of storing a large number of values and manipulating them.
Arrays :

Previously we use variables to store the values. To use the variables we have to declare the
variable and initialize the variable i.e, assign the value to the variable. Suppose there are 1000
variables are present, so it is a tedious process to declare and initialize each and every variable
and also to handle 1000 variables. To overcome this situation we use the concept of array .In an
Array values of same type are stored. An array is a group of memory locations related by the fact
that they all have the same name and same type. To refer to a particular location or element in
the array we specify the name to the array and position number of particular element in the array.

Array in C is one of the most used data structures in C programming. It is a simple and fast way
of storing multiple values under a single name. In this , we will study the different aspects of
array in C language such as array declaration, definition, initialization, types of arrays, array
syntax, advantages and disadvantages, and many more.

What is Array in C?

An array in C is a fixed-size collection of similar data items stored in contiguous memory


locations. It can be used to store the collection of primitive data types such as int, char, float, etc.,
and also derived and user-defined data types such as pointers, structures, etc.
One Dimensional Array :

The One-dimensional arrays, also known as 1-D arrays in C are those arrays that
have only one dimension.

Declaration:
Before using the array in the program it must be declared

Syntax:

data_type array_name[size];

data_type represents the type of elements present in the array. array_name represents the name of
the array.

Size represents the number of elements that can be stored in the array.

Example:

int age[100];

float sal[15];

char grade[20];

Here age is an integer type array, which can store 100 elements of integer type. The array sal is
floating type array of size 15, can hold float values. Grade is a character type array which holds
20 characters.
The C arrays are static in nature, i.e., they are allocated memory at the compile time

Initialization:
We can explicitly initialize arrays at the time of declaration.

Syntax:

data_type array_name[size]={value1, value2,……..valueN};

Value1, value2, valueN are the constant values known as initializers, which are assigned to the
array elements one after another.

Example:

int marks[5]={10,2,0,23,4};

The values of the array elements after this initialization are:

marks[0]=10, marks[1]=2, marks[2]=0, marks[3]=23, marks[4]=4

NOTE:

1. In 1-D arrays it is optional to specify the size of the array. If size is omitted during
initialization then the compiler assumes the size of array equal to the number of initializers.

Example:

int marks[]={10,2,0,23,4};

Here the size of array marks is initialized to 5.

2. We can’t copy the elements of one array to another array by simply assigning it.

Example:

int a[5]={9,8,7,6,5};

int b[5];

b=a; //not valid

we have to copy all the elements by using for loop.

for(a=i; i<5; i++)


b[i]=a[i];

Processing & Accessing:


We can access any element of an array in C using the array subscript operator [ ] and the index
value i of the element.

array_name [index];

One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at
index 0 and the last element is at N – 1 where N is the number of elements in the array.

// C Program to illustrate element access using array

// subscript
#include <stdio.h>

int main()

// array declaration and initialization

int arr[5] = { 15, 25, 35, 45, 55 };

// accessing element at index 2 i.e 3rd element

printf("Element at arr[2]: %d\n", arr[2]);

// accessing element at index 4 i.e last element

printf("Element at arr[4]: %d\n", arr[4]);

// accessing element at index 0 i.e first element

printf("Element at arr[0]: %d", arr[0]);

return 0;

Output:

Element at arr[2]: 35

Element at arr[4]: 55

Element at arr[0]: 15

For processing arrays we mostly use for loop. The total no. of passes is equal to the no. of
elements present in the array and in each pass one element is processed.

Example:

#include<stdio.h>

main()

int a[3],i;

for(i=0;i<=2;i++) //Reading the array values


{

printf(“enter the elements”);

scanf(“%d”,&a[i]);

for(i=0;i<=2;i++) //display the array values

printf(“%d”,a[i]);

printf(“\n”);

This program reads and displays 3 elements of integer type.

Update Array Element:

We can update the value of an element at the given index i in a similar way to accessing an
element by using the array subscript operator [ ] and assignment operator =.

array_name[i] = new_value;

C Array Traversal:

Traversal is the process in which we visit every element of the data structure. For C array
traversal, we use loops to iterate through each element of the array.

Array Traversal using for Loop

for (int i = 0; i < N; i++)

array_name[i];

You might also like