Professional Documents
Culture Documents
Array
Array
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> }
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];
} } }
}