Arrays Syllabus:: Arrays: Concepts, Definition, Declaration, Accessing Elements, Storing
Arrays Syllabus:: Arrays: Concepts, Definition, Declaration, Accessing Elements, Storing
Arrays Syllabus:: Arrays: Concepts, Definition, Declaration, Accessing Elements, Storing
Syllabus:
Arrays: concepts, definition, declaration, accessing elements, storing
elements, arrays and functions, two-dimensional and multi-dimensional
arrays, applications of arrays.
Contents
S.No Topic Page No.
1 Concept of Array
2 Definition of array
3 Declaration of Arrays
3.1 Example program
4 Accessing Elements
4.1 Calculating the addressing of array elements
4.1.1 Example
5 Storing Elements
5.1 Initialization of arrays
5.2 Input values to the elements of array
5.3 Assign Values to the elements
5.4 Calculating the length of the array
5.5 operations on arrays
6 Arrays and Functions
6.1 Passing individual elements
6.1.1 Passing Data Values
6.1.2 Passing Addresses
6.2 Passing entire Array
7 Sample Programs
8 Review Questions
8.1 Long answer Questions
8.2 Short Answer Questions
9 Snippet Programs
10 List of Figures
11 List of Tables
1.CONCEPTS
The fundamental data types, namely char, int, float, double are
used to store only one value at any given time.
Hence these fundamental data types can handle limited amounts of
data.
In some cases we need to handle large volume of data in terms of
reading, processing and printing.
To process such large amounts of data, we need a powerful data type
that would facilitate efficient storing, accessing and manipulation of
data items.
“C‟ supports a derived data type known as array that can be used for
such applications.
Derived Types
2.Arrays: (Definition)
An array is a fixed size sequenced collection of elements of the same
data type.
Or
An array is collection of same data type elements in a single entity.
Or
An array is collection of homogeneous elements in a single variable.
3.DECLARATION OF ARRAYS
Like any other variables, arrays must be declared before they are used.
Data Type: What kind of values array can store (Example: int, float,
char).
array_name:To identify the array.
Size of array(INDEX): The maximum number of values that the array
can hold.
Example:
int marks[10];
The above statement declares a marks variable to be an array
containing 10 elements.
In C, the array INDEX (also known as subscripts) starts from zero.
This means that the array marks will contain 10 elements in all.
The first element will be stored in marks[0]
The second element will be stored in marks[1]
.
.
.
The tenth element will be stored in marks[9].
Ist 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
eleme eleme eleme eleme eleme eleme eleme
eleme elem eleme
nt nt nt nt nt nt nt
nt ent nt
marks mark mark mark mark mark mark mark mark mark
[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
3.1 Example-1: Write a program to print bytes reserved for various types of
data and space required for storing them in memory using array.
# include<stdio.h>
main ( )
{
int a[10];
char c[10];
float b[10];
printf(“the type “int” requires %d bytes”, sizeof(int));
pirntf(“ \n The type “char” requires %d bytes”, sizeof(char));
printf(“ \n The type “float” requires %d bytes”, sizeof(float));
printf(“ \n %d memory locations are reserved for ten “int” elements”,
sizeof(a));
printf (“ \n %d memory locations are reserved for ten “char”
elements”,sizeof(c));
printf (“ \n %d memory locations are reserved for ten “float”
elements”,sizeof(b));
}
Output:
The type “int” requires 2 bytes
The type “char” requires 1 bytes
The type “float” requires 4 bytes
20 memory locations are reserved for ten “int‟ elements
10 memory locations are reserved for ten “char‟ elements
40 memory locations are reserved for ten “float‟ elements.
4.ACCESSING ELEMENTS OF THE ARRAY
For accessing an individual element of the array, the array INDEX
(Subscript) must be used
For example to access the fourth element of the array, you must write
array[3].
To access all the elements of the array, we must use a loop.
That is we can access all the elements of the array by varying the
value of the index into the array.
Example:
// Set each element of the array to 10
int I marks[5];
for(i=0;i<5;i++)
marks[i]=10;
Explanation:
The code accesses every individual element of the array and sets its
value to 10
In the for loop, first the value of marks[0] is set to 0, then the index(i)
incremented and the next value i.e., marks[1] is set to 10.
The procedure is continued until all the 5 elements of the array are
set to 10.
10 10 10 10 10
[0] [1] [2] [3] [4]
The array marks after executing the code
The name of the array is a symbolic reference for the address to the
first byte of the array. Therefore, whenever we use the array name, we
are actually referring to the first byte of that array. The index specifies
an offset from the beginning of the element being referred.
Valid Statements:
a = number[0] + 10;
value[6] = number[i] * 3;
When we declare and define an array, we are just allocating space for
the elements; no values are stored in the array.
To store values in the array, there are three(3) ways:
a) Initialize the elements
b) Input the values for the elements
c) Assign values to the elements
Initialize the
elements
e Store values
in the array Input values for
the elements
Assign values to
the elements
Example:
int marks[5] = { 90, 31, 41, 9, 88}
An array with name marks is declared that has space that is enough
to store 5 elements.
The first element i.e., marks[0] is assigned with the value 90.
Similarly the second element of the array i.e., marks[1] is assigned
with the value 31 and so on.
Marks[0] 90
Marks[1] 31
Marks[2] 41
Marks[3] 9
Marks[4] 88
Initialization of marks[5]
While initializing the array at the time of declaration , the programmer may
omit to mention the size of the array.
Example:
int marks[ ]={90,31,41};
Here the compiler will allocate enough space for all initialized
elements.
If the number of values provided is less than the number of elements
in the array, the un-assigned elements are filled with zeroes.
int i,marks[5];
for (i=0;i<5;i++)
scanf(“%d”,&marks[i]);
Explanation:
In the code, we start with the index I at o and input the value for the first
element of the array.
Since the array have 5 elements, we must input values for elements whose
index varies from 0 to 4.
Therefore in the for loop, we test for condition (i<5) which means the
number of elements in the array.
5.3 Assigning Values:
The third way to assign values to individual elements of the array is
by using the assignment operator.
Any value that evaluates to an appropriate data type as that of the
array can be assigned to the individual array element.
A simple assignment statement can be written as
marks[3] = 100;
int i,array1[5],array2[5];
for(i=0;i<5;i++)
array[1]=array[2];
2 5 3 1 7
age[0] age[1] age[2] age[3] age[4]
One-Dimensional
Arrays
main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
func(arr[3]);
}
void func(int num)
{
printf(“%d”, num);
}
EXPLANATION:
In the above example, only one element of the array is passed to the
called function.
This is done by using the index expression.
So arr[3] actually evaluates to a single integer value.
Main()
{
Int arr[5]={ 1, 2, 3, 4,5 };
Fun(&arr[3]);
}
Void fun(int *num)
{ Printf(“%d”,num);
}
6.2 Passing The Entire Array:
The array name refers to the first byte of the array in memory.
The address of rest of the elements in the array can be calculated
using the array name and the index value of the element.
Therefore when we need to pass an entire array to a function, we can
simply pass the name of the array.
#include<stdio.h>
int main()
scanf(“%d”,&n);
for(i=0;i<n;i++)
printf(“\n arr[%d] = “, i );
scanf(“%d”, &arr[i] );
}
OUTPUT
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
2 . C Program to Calculate Average Using Arrays
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum=0.0, average;
printf("Enter the numbers of data: ");
scanf("%d",&n);
while (n>100 || n<=0)
{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d",&n);
}
for(i=0; i<n; ++i)
{
printf("%d. Enter number: ",i+1);
scanf("%f",&num[i]);
sum+=num[i];
}
average=sum/n;
printf("Average = %.2f",average);
return 0;
}
Output
This program calculates the average if the number of data are from 1 to 100.
If user enters value of n above 100 or below 100 then, while loop is executed
which asks user to enter value of n until it is between 1 and 100.
3 C code to find largest and smallest number in an array
#include<stdio.h>
int main(){
int a[50],size,i,big,small;
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("Largest element: %d",big);
small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);
return 0;
}
Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1
/*
Program to Illustrate the Concept of Passing 1-D Array to
Function
Program to Find Largest from an Array
*/
#include <stdio.h>
#define SIZE 50
int big(int [], int);
main()
{
int a[SIZE], n, i, b;
printf("\nEnter size of array: ");
scanf("%d", &n);
printf("\nEnter elements:\n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
b = big(a, n);
printf("\nLargest number: %d", b);
getch();
}
int big(int a[], int n)
{
int b, i;
b = a[0];
for (i=0; i<n; i++)
if (a[i] > b)
b = a[i];
return b;
}
Second Dimension
2 Declaration of 2-Dimensional Arrays:
SYNTAX:
Data_type array_name[row_size][column_size];
EXPLANATION:
A two dimensional array called marks declared that has m(3) rows
and n(5) columns.
The first element of the array is denoted by marks[0][0], the second
element as marks[0][1] and so on.
marks[0][0] stores the marks obtained by the first student in the
first subject, marks[1][0] stores the marks of the second student
in the first subject, and so on.
2.1 CALCULATION OF ADDRESS:
the computer stores the base address and the address of the other
elements is calculated using the following formula
Address (A [I] [J] = BA + w { M ( J – 1 ) + ( I - 1 ) }
If the array elements are stored in column major order.
And
Address (A [I] [J] = BA + w { N ( I – 1 ) + ( J - 1 ) }
If the array elements are stored in row major order.
SOLUTION:
Address (marks[18,4])
= 1000 + 2 { 5 ( 18 – 1 ) + ( 4 – 1 ) }
= 1000 + 2 { 5 (17) + 3 }
= 1000 + 2 (88)
= 1176
3 INITIALIZATION OF TWO-DIMENSIONAL ARRAYS :
// passing a row
main()
{
int array[2][3] = { { 1, 2, 3 } , { 4, 5, 6} } ;
func ( array[1] );
}
7. Sample Programs :
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k;
int sum=0;
clrscr();
printf("nEnter First Matrix : n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
sum = 0;
for(k=0;k<=2;k++)
sum = sum + a[i][k] * b[k][j];
c[i][j]=sum;
}
getch();
}
Steps :
Dimension of Matrix 1 : 1 X 3
Dimension of Matrix 2 : 3 X 2
Evaluate : 1 X 2 = 2
Evaluate : 4 X 5 = 20
Evaluate : 6 X 7 = 22
-------------------------
Add : 64 ///c[0][0]
Step 2 :
Evaluate : 1 X 3 = 3
Evaluate : 4 X 8 = 40
Evaluate : 6 X 9 = 54
-------------------------
Add : 89 ///c[0][1]
Programmable Implementation :
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
sum = 0;
for(k=0;k<=2;k++)
sum = sum + a[i][k] * b[k][j];
c[i][j]=sum;
}
»
// to display 3 X 3 matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("enter the elements\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
/* m - Number of rows
n - Number of Columns */
clrscr();
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
{
printf("Enter the Element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
// ------------------------------------------
if ( m1 != m2 || n1 != n2 )
{
printf("nOrder of two matrices is not same ");
exit(0);
}
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
{
printf("Enter the Element b[%d][%d] : ",i,j);
scanf("%d",&b[i][j]);
}
// ------------------------------------------
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++ )
{
printf("%dt",c[i][j]);
}
printf("n");
}
getch();
}
Output
Algorithm:
Addition of two matrices:
Rule: Addition of two matrices is only possible if
both matrices are of same size.
Suppose two matrices A and B is of same size m X n
Sum of two matrices is defined as
(A + B)ij = Aij + Bij
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrices A and B of size of 2 X 3 is as
follow:
Program to find Transpose of Given Square Matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],m,i,j,temp;
/* actual size of matrix is m*n
i,j - for scanning of array
temp - for interchanging of a[i][j] and a[j][i] */
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
{
printf("n");
for(j=0 ; j 〈 m ; j++)
printf("%dt",a[i][j]);
}
/* Find transpose */
for(i=1;i<m;i++)
for(j=0;j<i;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
/* printing of all elements of final matrix */
BY:
MR. D.SRINIVAS RAO
ASST.,PROF.
DEPT OF IT
GITAM UNIVERSITY
HTP CAMPUS