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

Arrays in C

good

Uploaded by

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

Arrays in C

good

Uploaded by

k.venu2006
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ARRAYS

Define array:
An array represents a group of elements of same data type.
Arrays are generally categorized into two types:
 Single Dimensional arrays (or 1 Dimensional arrays)
 Multi-Dimensional arrays (or 2 Dimensional arrays, 3 Dimensional arrays, …)

Single Dimensional Arrays: A one dimensional array or single dimensional array represents a
row or a column of elements.
For example, the marks obtained by a student in 5 different subjects can be represented by a
1D array.
· We can declare a one dimensional array and directly store elements at the time of its
declaration,as:
int marks[] = {50, 60, 55, 67, 70};

Array elements are stored in contiguous memory locations. Each element is identified by an
index starting with "0". The lowest address corresponds to the first element and the highest
address to the last element.

Declaring an Array in C
To declare an array in C, you need to specify the type of the elements and the number of
elements to be stored in it.

type arrayName[size];

Initializing an Array in C
If a set of comma-separated sequence values put inside curly brackets is assigned in the
declaration, the array is created with each element initialized with their corresponding value.

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

ex:
#include <stdio.h>
int main()
{
int marks[] = {50, 60, 55, 67, 70,i;
for(i=0<i<5;i++)
{
printf("array elemements are: %d", marks[i]);
return 0;
}

 Multi-Dimensional Arrays (2D, 3D … arrays):


Multi dimensional arrays represent 2D, 3D … arrays.
A two dimensional array is a combination of two or more (1D) one dimensional arrays. A three dimensional
array is a combination of two or more (2D) two dimensional arrays.
· Two Dimensional Arrays (2d array): A two dimensional array represents several rows and columns of
data.
To represent a two dimensional array, we should use two pairs of square braces [ ] [ ] after the array
name.
For example, the marks obtained by a group of students in five different subjects can be represented by a
2D array.
 We can declare a two dimensional array and directly store elements at the time of its declaration,
as:
 int marks[] [] = {{50, 60, 55, 67, 70},{62, 65, 70, 70, 81}, {72, 66, 77, 80, 69} };

ex:
#include<stdio.h>
void main()
{
int A[2][2]={{1,2},{3,4}},r,c;
printf("A matrix is \n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("%d\t",A[r][c]);
}
printf("\n");
}
}

Output:
1 2

3 4

You might also like