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

Characteristics of Arrays: Array in C

An array in C is a collection of similar data types stored in contiguous memory locations that can be accessed using an index. Arrays allow storing multiple values in a single variable and provide advantages like easy traversal, random access, and code optimization. There are two main types of arrays in C: single dimensional arrays which use one subscript and store elements in a linear fashion, and two dimensional arrays which use two subscripts to represent rows and columns for storing elements in a matrix form. Common array operations include initialization, accessing elements, sorting, and performing arithmetic operations on two dimensional arrays.

Uploaded by

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

Characteristics of Arrays: Array in C

An array in C is a collection of similar data types stored in contiguous memory locations that can be accessed using an index. Arrays allow storing multiple values in a single variable and provide advantages like easy traversal, random access, and code optimization. There are two main types of arrays in C: single dimensional arrays which use one subscript and store elements in a linear fashion, and two dimensional arrays which use two subscripts to represent rows and columns for storing elements in a matrix form. Common array operations include initialization, accessing elements, sorting, and performing arithmetic operations on two dimensional arrays.

Uploaded by

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

Array in C

 An array is defined as the collection of similar data items that are stored under a common name.
 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.
 Each value refer by its index. Using[ ](subscript) for accessing the index

Characteristics of Arrays

 The declaration int a[5] creates 5 variables of integer types in the memory. Instead of declaring 5 variables
for 5 values.
 The array elements are stored in continuous memory locations

Advantage of C Array

1) Code Optimization: Less code to access the data.

2) Ease of traversing: By using for loop, we can retrieve the elements of an array easily.

3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.

4) Random Access: We can access any element randomly using the array.

Disadvantage of C Array

1) Fixed size of length


2) Occupies more memory

Types of Array

 Single Dimensional Array / One Dimensional Array


 Two Dimensional Array

Single Dimensional Array (One Dimensional array)


 An array with only one subscript is termed as one dimensional array
 Single dimensional arrays are used to store list of values of same data type.
 In single dimensional array, data is stored in linear form.
 Single dimensional arrays are also called as one-dimensional arrays, Linear Arrays or simply 1-D
Arrays.

Syntax
Data type array_name[array_size];  

Now, let us see the example to declare the array.


int marks[5];    Array Declaration
Initialization of C Array
marks[0]=80;//initialization of array  
marks[1]=60;  
marks[2]=70;  
marks[3]=85;  
marks[4]=75;  

Declaration with Initialization


We can initialize the c array at the time of declaration. Let's see the code.
int marks[5]={20,30,40,50,60};  

In such case, there is no requirement to define the size. So it may also be written as the following code.

int marks[]={20,30,40,50,60};  

Example Program
#include<stdio.h>
int main()
{
int i=0;
int marks[5]={20,30,40,50,60}; //declaration and initialization of array
for(i=0;i<5;i++)
{
printf("%d \t",marks[i]);
}
return 0;
}
Output
20 30 40 50 60

Example 2:
#include <stdio.h>
int main()
{
int number[5];
printf("Enter 5 number: ");
for(int i = 0; i < 5; ++i)
{
scanf("%d", &number[i]);
}
printf("Displaying the number: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", number[i]);
}
return 0;
}
Output
Enter 5 number: 1 2 3 4 5                                                                           

Displaying the number: 1        2       3       4       5 

Example: Sorting an array in ascending order.

#include<stdio.h>
int main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23}; (7,9,10,23,101)
for(i = 0; i<5; i++)
{
for(j = i+1; j<5; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<5; i++)

{
printf("%d\n",a[i]);
}
return 0;
}
Output
Printing Sorted Element List ...                                                                      
101                                                                                                   
23                                                                                                    
10                                                                                                    
9                                                                                                     

Two Dimensional Array in C
 An array with two subscripts is termed as two dimensional 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 syntax to declare the 2D array


Data type  array_name[rows][columns];  
Example:
int x[3][3];

Example
#include<stdio.h>
int main()
{
int i=0,j=0;
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};
for(i=0;i<3;i++) 0
{
printf("\n");
for(j=0;j<3;j++) 0, 1, 2
{
printf(" %d \t",arr[i][j]);
}
}
return 0;
}
Output
1       2       3                                                                                    
 4       5       6                                                                                    
 7       8       9 

Example
#include<stdio.h>
int main()
{
int disp[10][10];
int i, j,n;
printf("Enter the row and colum: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
for(j=0;j<n;j++)
{
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
printf("Two Dimensional array elements:\n");
for(i=0; i<n; i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d \t", disp[i][j]);

}
}
return 0;
}
Output
Enter the row and colum: 3                                                                            
Enter value for disp[0][0]:1                                                                          
Enter value for disp[0][1]:2                                                                          
Enter value for disp[0][2]:3                                                                          
Enter value for disp[1][0]:4                                                                          
Enter value for disp[1][1]:5                                                                          
Enter value for disp[1][2]:6                                                                          
Enter value for disp[2][0]:7                                                                          
Enter value for disp[2][1]:8                                                                          
Enter value for disp[2][2]:9                                                                          
Two Dimensional array elements:                                                                       
                                                                                                      
1       2       3                                                                                     
4       5       6                                                                                     
7       8       9 

#include <stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;

printf("\nENTER VALUES FOR FIRST MATRIX :\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("\nENTER VALUES FOR SECOND MATRIX :\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

printf("\n THE ADDITION OF MATRIX IS :\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%5d",c[i][j]);
}
printf("\n");
}
return 0;
}

Output
ENTER VALUES FOR FIRST MATRIX :                                                                       
1                                                                                                     
2                                                                                                     
3                                                                                                     
4                                                                                                                                                                          
ENTER VALUES FOR SECOND MATRIX :                                                                      
1                                                                                                     
2                                                                                                     
3                                                                                                     
4                                                                                                     
THE ADDITION OF MATRIX IS :                                                                          
    2    4                                                                                            
    6    8 

You might also like