Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Array

Array:

 An array is defined as the collection of similar type of data items stored at


contiguous memory locations. Arrays are the derived data type in C
programming language which can store int, char, double, float, etc.
 An array is a variable that can store multiple values. For example, if you want to
store 100 integers, you can create an array for it.
 int data[100]
 we can define an array which can store data at the contiguous memory locations.
Properties of Array
 Each element of an array is of same data type and carries the same
size, i.e. int = 4 bytes.
 Elements of the array are stored at contiguous memory locations
where the first element is stored at the first memory location.
 Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
base address and the size of the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing(travel across): By using the 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.

Declare An Array:
dataType arrayName[arraySize];
Few keynotes:
 Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
 If the size of an array is n, to access the last element, the n-1 index is used. In this
example, mark[4]
 Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be
2124d. Similarly, the address of mark[2] will be 2128d and so on.
 This is because the size of a float is 4 bytes.
Initialization of C Array
 simplest way to initialize an array
int marks[5];
marks[0]=80;//initialization of array  
marks[1]=60;  
marks[2]=70;  
marks[3]=85;  
marks[4]=75;
 Declaration with Initialization
int marks[5]={20,30,40,50,60}; //no requirement to define the size
int marks[]={20,30,40,50,60};  
Change Value of Array elements
Change Value of Array elements:
int mark[5] = {19, 10, 8, 17, 9}
mark[2] = -1; // make the value of the third element to -1
mark[4] = 0; // make the value of the fifth element to 0
To take input from the user:
​scanf("%d", &mark[2]); // take input and store it in the 3rd element
scanf("%d", &mark[i-1]); // take input and store it in the Ith element
To print an individual element :
printf("%d", mark[0]); // print the first element of the array
printf("%d", mark[i-1]); // print ith element of the array
Array Input/output
#include <stdio.h> //Program to take 5 values from the user and store them in
int main() { //an array, Print the elements stored in the array
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; i++) { // taking input and storing it in an array
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
for(int i = 0; i < 5; i++) { // printing elements of an array
printf("%d\n", values[i]);
}
return 0;
}
To calculate average:

#include <stdio.h> /*adding integers entered by the user to


the sum variable*/
int main() {
sum =sum+marks[i];
int i, n, sum = 0, average;
}
printf("Enter number of elements: ");
scanf("%d", &n);
average = sum / n;
int marks[n];
printf("Average = %d", average);
for(i=0; i < n; i++) {
return 0;
printf("Enter number%d: ",i+1);
}
scanf("%d", &marks[i]);
Sorting an array:

#include<stdio.h> }
int main() }//for accending number
}
{ for(i=0 ; i<n;i++)
//printing after assending
{
int n,i,j,temp,price[i]; order
for(j=0;j<n-1;j++)
printf("Enter the total number for(i=0;i<n;i++)
of prices:"); {
if( price[j] > price[j+1] )
{
scanf("%d",&n);
{ printf("%d \n",price[i]);
//for reading number form
user temp = price[j]; }
for(i=0;i<n;i++) price[j]= price[j+1]; return 0;
price[j+1]= temp;
{printf("The price of %d }
is:",i+1); }

scanf("%d",&price[i]);
Multidimensional Arrays:
 Multidimensional Arrays can be defined in simple words as array of arrays.
 For example:
float x[3][4];
 Initializing a multidimensional array:
Different ways to initialize two-dimensional array
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};
Sum of two matrices
#include<stdio.h> //to read two dimensional array
printf("give the value for matrix b");
int main() //printing two dimentional array
for(i=0;i<3;i++)
{
{ for(i=0;i<3;i++)
int i,j,a[3][3],b[3][3], result[3][3];// for(j=0;j<3;j++)
initializing two dimensional array {
{
printf("give the value for matrix a\n");//to for(j=0;j<3;j++)
printf("Enter b%d%d:",i+1,j+1);
read two dimensional array
scanf("%d",&b[i][j]); {
for(i=0;i<3;i++)
}
printf("%d\
{ } t",result[i][j]);
for(j=0;j<3;j++) //addition two dimensional matrix
}
for(i=0;i<3;i++)
{
{ printf("\n");
printf("Enter a%d
S for(j=0;j<3;j++) }
%d:",i+1,j+1);scanf("%d",&a[i][j]);
{
} return 0;
result[i][j]=a[i][j]+b[i][j];
} } }
}

You might also like