0% found this document useful (0 votes)
8 views10 pages

Unit2 Arrays

Uploaded by

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

Unit2 Arrays

Uploaded by

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

Department of Computer Science and Engineering

M.V.S.R.Engineering College, Nadergul, Hyderabad.


B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Solving and Programming Unit : II
Teacher:- K.Srinivas
ARRAYS

INTRODUCTION: - Why arrays are needed when we have basic data types. Basic data types are scalar type. The variables
are capable of holding only one value at a time. For example

main()
{ int x;
x=5;
x=10;
printf(“%d”,x);
}
The output of the program is 10 because when the value of 10 is assigned to x, the earlier value of x i.e. 5 is overwrites
(the value 5 is lost). Thus ordinary variables hold only one value at a time.

There are some situations in which we would want to store more than one value at a time in a single variable.

For example, suppose we want to store the marks obtained by 100 students in a class. In such case we have two options
to store these marks in memory.

1) Construct 100 variables to store marks obtained by 100 students i.e. each variable containing one student’s marks.
2) Construct one variable capable of storing all the hundred values. Obviously, the second option is better because it would
be much easier to handle one variable than handling 100 variables. So C language provides us a data type called array to
store values of similar data type.

Definition of Array: - An array is a collection of elements of similar data type which are stored in contiguous (adjacent)
memory locations. Arrays are comes under derived data types. Array is also known as homogenous data structure.

Arrays are static in size. Arrays elements are represented with same name and identified by memory locations with index
variable. Arrays are one dimensional, multidimensional.

ARRAY DECLARATION: - The array is declared same as other variables before it is used in the program. The general
format is

1. One/Single dimensional array:- data_type arr_name[size];

data_type refers to the type of the data elements stored in array(basic data types).
arr_name is the name of the array.
size indicates the maximum size of the array(i.e. Number of data elements to be stored .)
e.g int a[100]; /*an integer type array of size 100*/

char name[20]; /*a char type array of size 20*/

Here, int specifies the type of the variable, just as it does with the ordinary variables and a specifies the name
of the variable. The number 100 tells how many elements of the type int will be in our array. The [ ] tells the compiler
that we are dealing with an array.

ARRAY INITIALISATION: - Like primary data variables array elements are not initialized it contains garbage
values.

To initialize arrays the following is the format

int x[5 ]={23,12}; In this if we do not initialize all the memory locations then remaining memory location are
initialized based on the data type with 0 or blank space(Char).
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

Ex. int a[10]= {1,2,3,4,5,6,7,8,9,10};

After declaration of the array the array elements are stored in contiguous memory locations. For example

int arr[7]={45,98,76,86,90,56,78};

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6]

45 98 76 86 90 56 78

1000 1002 1004 1006 1008 1010 1012

int according to your system the size is 2 or 4 bytes. i.e. 2 bytes in this example.

14 bytes get immediately reserved in memory. 14 bytes because each of the 7 integers would be 2 bytes long.

Memory allocated= no of elements*sizeof (datatype);

=7*2=14 bytes

Automatic size allocation or un sized array:- Un sized array or automatic allocation of size, size of array is
calculated based on the number of elements given in the initialization list. i.e. array size is 4 for the given example.

int a[]={4,2,6,7};

Accessing Elements of an Array

Once an array is declared, now we will see how individual elements in the array can be referred. This is done
with subscript/index, the subscript/index variable will have integer constant value in the square brackets following the
array name represent the location of the element. This number specifies the element’s position in the array. All the
array elements are numbered, starting with 0. Thus, arr[2] is not the second element of the array but the third.

Entering data into an array

For example if we want to take marks of the student in to an array. Here is the code that places data into the
array.

int marks[6],i;
printf(“Enter the marks\n”);
for(i=0;i<6;i++)
{ scanf(“%d”,&marks[i]); }

We use for loop for reading elements into array. The first time through the loop, i has a value 0, so the scanf()
statement will cause the value typed first to be stored in the array at marks[0] because we are passing the address of
this particular array position to the scanf() function(i.e &marks[0]). This process will be repeated until i become 6 (i.e.
until the 6 elements are stored in array marks)
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

EXAMPLE 1:- Reading the elements from input and storing to array and printing it.
#include<stdio.h>
void main()
{ int a[100],i,n;
printf("HOW MANY ELEMENTS YOU WANT TO STORE IN THE ARRAY\N : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);/*Reading elements into array*/
printf("CONTENTS OF THE ARRAY ARE\n");
for(i=0;i<n;i++)
printf("a[%d] = %d\t",i,a[i]);/*retrieving data from array*/
}
OUTPUT: HOW MANY ELEMENTS YOU WANT TO STORE IN THE ARRAY : 5
ENTER THE ELEMENTS
12345
CONTENTS OF THE ARRAY
a[0]=1 a[1]=2 a[2]=3 a[3]=4 a[4]=5

Array Boundaries checking In C there is no check to see if the subscript used for an array exceeds the size of the
array. Data entered with a subscript exceeding the array size simply is placed in memory outside the array; probably
on top of other data, or on the program itself. This will lead to unpredictable results and there will be no error message
to warn you that you are going beyond the array size. In some cases the computer may hang. So it the user
responsibility to check the boundaries while accessing the array elements. Thus the following program may turn out
to be suicidal.
main()
{ int num[40];
for(i=0;i<100;i++)
num[i]=i;
}
Thus, to see to it that we do not reach beyond the array size is entirely the programmer’s botheration and not the
compiler’s.

SINGLE DIMENSIONAL CHARACTER ARRAY


The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a
character array. Character arrays are often called as “string”, char =name[30];

Initialization of character array is done in two ways 1. Character by character 2. As string i.e. group of characters

1. Character by character 2. As string i.e. group of characters

char name[30]={‘S’,’r’,’i’,’n’,’i’,’v’,’a’,’s’,’.’,’K’,’\0’}; char name[30]={“Srinivas K”};

Character array as string, string terminator/end of the string is denoted by null representation is’\0’ it is the
combination of two characters back slash and zero. It is an escape sequence.

Accessing the character array bye two ways. With formatted Input/output commands

1. with %c format 2. With %s format.

1. for(i=0;i<n;i++) 1. scanf(“%s”,a);
scanf(“%c”,&a[i]); 2. printf(“%s”,a);
2. for(i=0;i<n;i++)
printf(“%c”,a[i]);
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

Ex. Read Your name and print it character by character.

main()
{ char name[30];
int i,n;
printf(“Enter the no of characters to read\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%c”,name[i]);
printf(“Name is “);
for(i=0;i<n;i++)
printf(“%c”,name[i]);
}

OUTPUT:- Enter the no of characters to read


7
Krishna
Name is Krishna

Ex. Read Your name and print it as a string(group of characters).

main()
{ char name[30];
int i;
printf(“Enter the Name\n”);
scanf(“%s”,name);
printf(“Name is “);
printf(“%s”,name);
for(i=0;name[i]!=’\0’;i++)// this is with checking string terminator printing the name character by character
printf(“%c”,name[i]);
}

OUTPUT:- Enter the Name


Vishnu
Name is Vishnu
MULTI DIMENSIONAL ARRAYS:- Means more than one dimension. When dimension are increasing the
complexity of process the arrays increases. Multi dimensional arrays are declared as follows

data_type array_name[row_size][column_size][ row_size][column_size] ….

Multi dimensional capacity depends on the system memory size.

TWO DIMENSIONAL ARRAYS:- So far we are looked at arrays with only one dimension. Two-dimensional
arrays are declared as follows

data_type array_name[row_size][column_size];

Example: int arr[3][4];


Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

Initializing a two-dimensional array is as follows

int arr[3][4]={ {1,2,3,4},{5,6,7,8},{8,9,10,11,12} };

Or even this way also valid

int arr[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};

NOTE: It is important to remember that while initializing an array it is necessary to mention the
second(column) dimension, whereas the first dimension(row) is optional.

Thus the following declaration are the correct one

int arr[2][3]={12,34,23,45,56,45};

int arr[][3]={12,34,23,45,56,45};

Whereas the following declarations are wrong.

int arr[][]={12,34,23,45,56,45}; X

Int arr[2][]={12,34,23,45,56,45}; X

Example: Program to read matrix into 2-dimensional array and print.

#include<stdio.h>
void main()
{ int a[20][20],i,j,m,n;
clrscr();
printf("Enter the order of the matrix\n");
scanf("%d %d",&m,&n);
printf("Enter the elements in to array\n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("The matrix elements are\n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
}

OUTPUT: Enter the order of matrix


33
Enter the matrix elements into array
123
456
789
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

The matrix elements are


123
456
789

MEMORY MAP OF A 2 DIMENSIONAL ARRAY

The elements of the array of the above program cannot store in rows and columns in memory. The array
arrangement is only conceptually true. This is because memory doesn’t contain rows and columns. In memory
whether it is a one-dimensional or two dimensional array the array elements are stored in one continuous memory
locations. The arrangement of array elements of a two dimensional array in memory is shown below:

Suppose if we initialize an array

int a[4][3]={ {23,45,34},{12,67,98,},{22,16,29},{73,75,13} };

the elements are stored in memory as follows

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2] a[3][0] a[3][1] a[3][2]

23 45 34 12 67 98 22 16 29 73 75 13

1000 1002 1004 1006 1008 1010 1012 1014 1016 1018 1020 1022

Example: Program for addition of two matrices

# include <stdio.h>
void main()
{ int a[10][10],b[10][10],c[10][10];
int i,j;
int r1,r2,c1,c2;
printf("Enter the size of the first matrix ( rows and coloums)\n");
scanf("%d%d",&r1,&c1);
printf("Enter the size of the seond matrix (rows and coloums)\n");
scanf("%d%d",&r2,&c2);
printf("Enter the elements in matrix one");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements in matrix two");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
printf("The sum of the two matrices is \n");
for(i=0;i<r1;i++)
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

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

OUTPUT : Enter the size of the first matrix ( rows and columns)
22
Enter the size of the second matrix ( rows and columns)
22
Enter the elements into first matrix
1 2
2 4
Enter the elements into second matrix
5 6
6 8
The sum of two matrices is
6 8
8 12

Example: program for matrix multiplication

# include <stdio.h>
void main()
{ int a[10][10],b[10][10],c[10][10], r1,c1,r2,c2,i,j,k;
printf("\n Enter the order of first matrix(rows and columns)\n");
scanf("%d%d",&r1,&c1);
printf("\n Enter the order of second matrix\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
printf("Matrix multiplication is not possible");
else
{ printf("enter the first matrix elements\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter the matrix element b\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
{ for(j=0;j<c2;j++)
{ c[i][j]=0;
for(k=0;k<c2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

printf("The product matrix is .....\n");


for(i=0;i<r1;i++)
{ for(j=0;j<c2;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
}

OUTPUT : Enter the size of the first matrix ( rows and columns)
22
Enter the size of the second matrix ( rows and columns)
22
Enter the elements into first matrix
2 3
4 5
Enter the elements into second matrix
1 0
0 1
The Product of two matrices is
2 3
4 5

Example of Transpose of a matrix

#include<stdio.h>
void main()
{ int a[10][10],i,j,r,c;
printf("Enter the matrix size r and c(rows and columns)\n");
scanf("%d%d",&r,&c);
printf("Enter the matrix elements\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("Matrix before transpose\n");
for(i=0;i<r;i++)
{for(j=0;j<c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("Matrix after transpose\n");
for(i=0;i<c;i++)
{for(j=0;j<r;j++)
printf("%d ",a[j][i]);
printf("\n");
}
}
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

OUTPUT : Enter the matrix size r and c ( rows and columns)


32
Enter the matrix elements
2 3
4 5
6 7

Matrix before transpose


2 3
4 5
6 7

Matrix after transpose


2 4 6
3 5 7

Example to check if a given matrix is an identity matrix or Not

#include <stdio.h>
void main()
{ int a[10][10];
int i, j, row, col, flag = 1;
printf("Enter the order of the matrix \n");
scanf("%d %d", &row, &col);
if(row==col)
{ printf("Enter the elements of matrix \n");
for (i = 0; i < row; i++)
{for (j = 0; j < column; j++)
{ scanf("%d", &a[i][j]);}}
printf("MATRIX A is \n");
for (i = 0; i < row; i++)
{ for (j = 0; j < column; j++)
{
printf("%3d", a[i][j]);
}
printf("\n");
}
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if ((i==j && a[i][j] != 1) && (!=0 && a[j][i] != 0))
{
flag = 0;
break;
}
}
}
if (flag == 1 )
printf("It is identity matrix \n");
else
printf("It is not a identity matrix \n");
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, EEE-II) 2019-2020, semester -I
Subject : Problem Sloving and Programming Unit : II
Teacher:- K.Srinivas

else
printf("Identity check Not possible\n");
}

OUTPUT :
Enter the order of the matrix
3 3

Enter the elements of matrix

1 0 0
0 1 0
0 0 1

MATRIX A is
1 0 0
0 1 0
0 0 1

It is identity matrix.

You might also like