0% found this document useful (0 votes)
53 views6 pages

Arrays in C

The document discusses arrays in C programming. It defines arrays as collections of similar data types stored in contiguous memory locations. Arrays allow storing primitive data types like integers, characters, and floating-point numbers. One-dimensional arrays store elements in a single row or column, while multi-dimensional arrays can represent matrices with rows and columns. The document provides examples of declaring, initializing, and accessing elements of one-dimensional and two-dimensional arrays in C code. It also demonstrates using loops to input and output array element values.

Uploaded by

rsmldeptit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

Arrays in C

The document discusses arrays in C programming. It defines arrays as collections of similar data types stored in contiguous memory locations. Arrays allow storing primitive data types like integers, characters, and floating-point numbers. One-dimensional arrays store elements in a single row or column, while multi-dimensional arrays can represent matrices with rows and columns. The document provides examples of declaring, initializing, and accessing elements of one-dimensional and two-dimensional arrays in C code. It also demonstrates using loops to input and output array element values.

Uploaded by

rsmldeptit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Course Title: Introduction to C

Course Code: U-INC-180

Arrays:
 An array is defined as the collection of similar type of data items stored at contiguous
memory locations.
 An array is a group (or collection) of same data types.
 An array is defined as finite ordered collection of homogenous data, stored in
contiguous memory locations.
 An array is also known as a subscripted variable.
 Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc. It also has the capability to
store the collection of derived data types, such as pointers, structure, etc.
 The array is the simplest data structure where each data element can be randomly
accessed by using its index number.
 Each item in the array is called an element.
 Every element of an array is identified by a number and is known as index / subscript
of an array element.
 Array element index always start with zero (0).

C language support two types of array


1) One Dimensional Array
2) Multidimensional Array

One Dimensional
The array which have only one dimension i.e. elements in row or column fashion is
non as one dimensional array.

Declaration of array:
The array variable can be declared as
dataType arrayName[arraySize];

Example
int marks[5];

in above example int is a data type,


marks is a name of array, number in square bracket indicates the total number of elements
to be stored in an array. i.e. size of an array.

Initialization of array elements:

Array elements can be initialized in two different ways.


1) At the time of initialization

int marks[5] = {44,55,66,44,33};

Index
0 1 2 3 5
Position
marks 44 55 66 44 33

Notes Prepared by : V. D. Panchal Page No:1


Course Title: Introduction to C
Course Code: U-INC-180

2) After declaration of array variable, using index of array

marks[0] = 44;
marks[1] = 55;
marks[2] = 66;
marks[3] = 44;
marks[4] = 33;

To access array elements one can use any looping construct as shown in example below.

Example 1: initialize array elements and display using for loop.


#include<stdio.h>
#include<conio.h>
main()
{
int i;
int marks[5] = {44,55,66,33,55};
clrscr();
for(i=0;i<5;i++)
{
printf("\n %d",marks[i]);
}
getch();
}

Example 2: Read array elements and display them.


#include<stdio.h>
#include<conio.h>
main()
{
int i;
int marks[5];
clrscr();
printf(“Enter 5 elements of an array”);
for(i=0;i<5;i++)
{
scanf(“%d”,&marks[i]);
}
for(i=0;i<5;i++)
{
printf("\n marks[%d] = %d",i, marks[i]);
}
getch();
}

Notes Prepared by : V. D. Panchal Page No:2


Course Title: Introduction to C
Course Code: U-INC-180

Example 3: Read array elements and display total of them.


#include<stdio.h>
#include<conio.h>
main()
{
int i, tot=0;
int marks[5];
clrscr();
printf(“Enter 5 elements of an array”);
for(i=0;i<5;i++)
{
scanf(“%d”,&marks[i]);
}

for(i=0;i<5;i++)
{
tot = tot + marks[i]);
}

printf("\n Sum of all elements of an array is %d”,tot);


getch();
}

Example 4: Sorting array elements in ascending order.


#include <stdio.h>
#include <conio.h>
void main()
{

int i, j, temp, arr[5];


clrscr();
printf("Enter the numbers \n");
for (i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < 5; i++)
{

for (j = i + 1; j < 5; j++)


{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

Notes Prepared by : V. D. Panchal Page No:3


Course Title: Introduction to C
Course Code: U-INC-180

printf("The numbers arranged in ascending order are given below \n");


for (i = 0; i < 5; ++i)
{
printf("%d\n", arr[i]);
}
getch();
}

Example 5: Sorting array elements in descending order.


#include <stdio.h>
#include <conio.h>
void main()
{

int i, j, temp, arr[5];


clrscr();
printf("Enter the numbers \n");
for (i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < 5; i++)
{

for (j = i + 1; j < 5; j++)


{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < 5; ++i)
{
printf("%d\n", arr[i]);
}
getch();
}

Notes Prepared by : V. D. Panchal Page No:4


Course Title: Introduction to C
Course Code: U-INC-180

Two Dimensional / Multidimensional array


The two-dimensional array can be defined as an array of arrays. The 2D array is organized
as matrices which can be represented as the collection of rows and columns. The 2D array is
an simplest form of multidimensional array.

Two-dimensional arrays are declared as follows


Synatx:
Type array_name [row_size][column_size];
Where type is any valid data type in C and array_name will be a valid variable name. A
two-dimensional array can be considered as a table which will have row_size number of
rows and column_size number of columns.
A two-dimensional array myarr, which contains three rows and four columns can be shown
as follows:

Column 0 Column 1 Column 2 Column 3

Row 0 Myarr[0][0] Myarr[0][1] Myarr[0][2] Myarr[0][3]

Row 1 Myarr[1][0] Myarr[1][1] Myarr[1][2] Myarr[1][3]

Row 2 Myarr[2][0] Myarr[2][1] Myarr[2][2] Myarr[2][3]

Example:
int a[3][3];

Initialization of two dimensional array:


int a[3][3] = {
{11,22,33},
{55,66,77},
{33,22,12}
};

The two dimensional array can also be initialized as follows:


int a[3][3] = {11,22,33,55,66,77,33,22,12};

0 1 2

0 11 22 33

1 55 66 77

2 33 22 12

In the example above


a[0][0] – refers to the first element
a[0][1] – refers to the second element
a[0][2] – refers to the third element
a[1][0] – refers to the fourth element
a[1][1] – refers to the fifth element
a[1][2] – refers to the sixth element
a[2][0] – refers to the seventh element
a[2][1] – refers to the eighth element
a[2][2] – refers to the ninth element

Notes Prepared by : V. D. Panchal Page No:5


Course Title: Introduction to C
Course Code: U-INC-180

Example:
#include <stdio.h>
#include <conio.h>
main()
{
int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9}};
int i, j;

/* output each array element's value */


for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ )
{
printf(" %d\n", a[i][j]);
}
printf(“\n”);
}
getch();
}

Notes Prepared by : V. D. Panchal Page No:6

You might also like