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

Array

• a fixed-size sequential collection of elements of the same type


• a collection of variables of the same type
• arrays consist of contiguous memory locations. The lowest address corresponds
to the first element and the highest address to the last element.
• An array is a collection of similar data items stored in continuous memory
locations with single name.
Array indexes always begin with 0. Hence when we say array of size 10, array has
elements from index 0 to 9. If we specify or use array as intArr[10], intArr[11],
intArr[200], the C compiler will not show any error, but we will get run time errors
while executing the program
Characteristics of Array

• An array holds elements that have the same data type.


• Array elements are stored in contiguous memory locations.
• All the elements of an array share the same name.
• Array name represents the address of the starting element.
Advantage of Array

• Using arrays, other data structures like linked lists, stacks, queues, trees, graphs etc
can be implemented.
• Two-dimensional arrays are used to represent matrices.
• Arrays represent multiple data items of the same type using a single name.
• In arrays, the elements can be accessed randomly by using the index number.
• It is better and convenient way of storing the data of same datatype with same size.
• It allows us to store known number of elements in it.
• It allocates memory in contiguous memory locations for its elements. It does not
allocate any extra space/ memory for its elements. Hence there is no memory
overflow or shortage of memory in arrays.
Disadvantage of Array

• The number of elements to be stored in an array should be known in advance.


• An array is a static structure (which means the array is of fixed size). Once
declared the size of the array cannot be modified. The memory which is allocated
to it cannot be increased or decreased.
• Insertion and deletion are quite difficult in an array as the elements are stored in
consecutive memory locations and the shifting operation is costly.
• Allocating more memory than the requirement leads to wastage of memory space
and less allocation of memory also leads to a problem.
• It is not possible to hold dissimilar data type.
Types of Array

One Dimensional

Multi Dimensional
One Dimensional Array

• single dimensional arrays are used to store a row of values. 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.
• An array which has only one subscript is termed as one dimensional array.

• For example:
• Char name[25];
Declaration of one dimensional array

• Data_type array_name[array_size];

The type of array The name of array The number of


elements variable elements that can
be stored
For Example: declaration of one dimensional array

Syntax:
data_type array_name[array_size];
Example:
char name[25];
int a[10];
float temperature[25];
float marks[25];
char fname[20];
Array Initialization :One dimensional array

• Syntax:
Data_type array_name[array_size]={val1,val2,val3,………,valn};
For example:
int marks[4]={76,78,98,55};

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};


WAP to input 10 numbers in array and display them

#include <stdio.h> }
void main()
{ printf("\nElements in array are:
int arr[10]; ");
int i; for(i=0; i<10; i++)
printf("Input 10 elements in the {
array :\n"); printf("%d ", arr[i]);
for(i=0; i<10; i++) }
{ printf("\n");
printf("element - %d : ",i); }
scanf("%d", &arr[i]);
WRITE A PROGRAM TO READ AGE OF N PERSONS AND DISPLAY ONLY
THOSE PERSONS WHOSE BETWEEN 50 AND 60.
#include<stdio.h> }
#include<conio.h>
void main() for (i=1;i<=n;i++)
{ {
int i,n,age[100],count=0; if(age[i]>50 && age[i] < 60)
printf("Enter the number of persons :: "); count++;
scanf("%d",&n); }
for (i=1;i<=n;i++) printf("\n\nNumber of persons whose age
{ between 50-60 are :: %d",count);
printf("\nEnter age of %d persons :: ",i); getch();
scanf("%d",&age[i]); }
WAP to input ‘n’ numbers and sort them in ascending order

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


#include <stdio.h> {
void main() if (number[i] > number[j])
{ {
int i, j, a, n, number[30]; a = number[i];
printf("Enter the value of N \n"); number[i] = number[j];
scanf("%d", &n); number[j] = a;
printf("Enter the numbers \n"); }}}
for (i = 0; i < n; ++i) printf("The numbers arranged in ascending order
are given below \n");
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
{
}
Array to have sum of numbers

#include <conio.h> scanf("%d",&a[i]);


int main() }
{ for(i=0; i<n; i++)
int a[1000],i,n,sum=0; {
printf("Enter size of the array : "); sum+=a[i];
scanf("%d",&n); }
printf("Enter elements in array : "); printf("sum of array is : %d",sum);
for(i=0; i<n; i++) return 0;
{ }
Palindrome

#include <stdio.h> t = t/10;


int main() }
{
int n, r = 0, t; if (n == r)
printf("Enter a number to check if printf("%d is a palindrome
it's a palindrome or not\n"); number.\n", n);
scanf("%d", &n); else
t = n; printf("%d isn't a palindrome
while (t != 0) number.\n", n);
{
r = t%10; return 0;
r = r + t*10; }
Multi Dimensional Array

• An array of arrays is called as multi dimensional array. In simple words, an


array created with more than one dimension (size) is called as multi dimensional
array.
• Multi dimensional array can be of two dimensional array or three
dimensional array or four dimensional array or more...
• Most popular and commonly used multi dimensional array is two dimensional
array. The 2-D arrays are used to store data in the form of table. We also use
2-D arrays to create mathematical matrices.
• The maximum capacity of elements of array is the product of row size and
column size.
Declaration of multi dimensional array

Data_type array_Name [ row_Size ] [ column_Size ] ;

For example:
int a[m][n];
Here a[0][0] is initial and a[m-1][n-1] is the last element.
Initialization of multi dimensional array

2-D array can be initialized in a way similar to that of 1-D array. for example:-
int mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22};
These values are assigned to the elements row wise, so the values of
elements after this initialization are
Mat[0][0]=11, Mat[1][0]=14, Mat[2][0]=17 Mat[3][0]=20
Mat[0][1]=12, Mat[1][1]=15, Mat[2][1]=18 Mat[3][1]=21
Mat[0][2]=13, Mat[1][2]=16, Mat[2][2]=19 Mat[3][2]=22
• While initializing we can group the elements row wise using inner
braces.
for example:-
int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}};

• If we initialize an array as
int mat[4][3]={{11},{12,13},{14,15,16},{17}};
Then the compiler will assume its all rest value as 0,which are not defined.
Mat[0][0]=11, Mat[1][0]=12, Mat[2][0]=14, Mat[3][0]=17
Mat[0][1]=0, Mat[1][1]=13, Mat[2][1]=15 Mat[3][1]=0
Mat[0][2]=0, Mat[1][2]=0, Mat[2][2]=16, Mat[3][2]=0
WAP to input data in 2-dimensional array and display in matrix form

#include<stdio.h> printf("Two Dimensional array elements:\n");


int main(){ for(i=0; i<2; i++) {
int disp[2][3]; for(j=0;j<3;j++) {
int i, j; printf("%d ", disp[i][j]);
for(i=0; i<2; i++) { if(j==2){
for(j=0;j<3;j++) { printf("\n");
printf("Enter value for disp[%d][%d]:", i, }}}
j); return 0;
scanf("%d", &disp[i][j]); }
}}
//Displaying array elements
Transpose of a matrix: 2-dimensional array

#include <stdio.h> }
void main() }
{ printf("Transpose of matrix is \n");
int array[10][10]; for (j = 0; j < n; ++j)
int i, j, m, n; {
printf("Enter the order of the matrix \n"); for (i = 0; i < m; ++i)
scanf("%d %d", &m, &n); {
for (i = 0; i < m; ++i) printf(" %d", array[i][j]);
{ }
for (j = 0; j < n; ++j) printf("\n");
{ }
scanf("%d", &array[i][j]); }
WAP to add two matrices
#include <stdio.h> for(j=0;j<3;j++)
#include <conio.h> c[i][j]=a[i][j]+b[i][j];
void main() printf("\nTHE VALUES OF MATRIX C
{ ARE:\n");
int a[2][3],b[2][3],c[2][3],i,j; for(i=0;i<2;i++)
clrscr(); for(j=0;j<3;j++)
printf("\nENTER VALUES FOR printf("%5d",c[i][j]);
MATRIX A:\n"); printf("\n");
for(i=0;i<2;i++) getch();
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR
MATRIX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
Assignments

• WAP to find transpose of 3*3 matrix.


• WAP to subtract two 3*3 matrices.
• WAP to multiply two 2*2 matrices.
• WAP to take salary of 100 employee and sort them on the basis of
ascending order.
• WAP to take ‘n’ input and find greatest and least number among
them.
• WAP to input marks of 20 students and display their sum and
average marks.
WAP to multiply two 2*2 matrices
#include <stdio.h> for (d = 0; d < q; d++) {
int main() for (k = 0; k < p; k++) {
{ sum = sum + first[c]
int m, n, p, q, c, d, k, sum = 0; [k]*second[k][d];
int first[10][10], second[10][10], multiply[c][d] = sum;
multiply[10][10]; sum = 0;
printf("Enter number of rows and }}
columns of first matrix\n"); printf("Product of the matrices:\n");
scanf("%d%d", &m, &n); for (c = 0; c < m; c++) {
printf("Enter elements of first for (d = 0; d < q; d++)
matrix\n"); printf("%d\t", multiply[c][d]);
for (c = 0; c < m; c++) printf("\n");
for (d = 0; d < n; d++) }}
scanf("%d", &first[c][d]); return 0;
printf("Enter number of rows and }
columns of second matrix\n");
scanf("%d%d", &p, &q);
printf("Enter elements of second
matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
Differences between one dimensional and two
dimensional array

You might also like