Arrays: Introduction, One-dimensional arrays, Declaring and
Initializing arrays, Multidimensional arrays.
Strings: Introduction to Strings, String operations with and
without using String handling functions, Array of strings
Array:-
An array is defined as an ordered set of similar data
items. All the data items of an array are stored in consecutive memory
locations in RAM. The elements of an array are of same data type and each item
can be accessed using the same name.
Declaration of an array:- We know that all the variables are declared before they
are used in the program. Similarly, an array must be declared before it
is used. During declaration, the size of the array has to be specified. The
size used during declaration of the array informs the compiler to allocate and
reserve the specified memory locations.
Syntax:- data_type array_name[n];
where,
n is the number of data items (or) index(or) dimension.
0
to (n-1) is the range of array.
Ex: int a[5];
float
x[10];
Initialization
of Arrays:-
The
different types of initializing arrays:
1. At Compile time
(i)
Initializing all specified
memory locations.
(ii)
Partial array
initialization
(iii)
Initialization without
size.
(iv)
String initialization.
2.
At Run Time
1.
Compile Time Initialization
We can initialize the elements of arrays in
the same way as the ordinary variables when they are declared. The general form
of initialization of arrays is
type array-name[size]={
list of values};
(i) Initializing all specified memory locations:-
Arrays can be initialized
at the time of declaration when their initial values are known in
advance. Array elements can be initialized with data items of type int, char
etc.
Ex:-
int a[5]={10,15,1,3,20};
During compilation, 5 contiguous memory locations are
reserved by the compiler for the variable a and all these locations are
initialized as shown in figure.
Fig:
Initialization of int Arrays
Ex:-
int
a[3]={9,2,4,5,6}; //error: no. of
initial vales are more than the size of array.
(ii) Partial array initialization:- Partial array initialization is possible in c
language. If the number of values to be initialized is less than the
size of the array, then the elements will be initialized to zero automatically.
Ex:-
int a[5]={10,15};
Eventhough compiler
allocates 5 memory locations, using this declaration statement; the compiler
initializes first two locations with 10 and 15, the next set of memory
locations are automatically initialized to 0's by compiler as shown in figure.
Fig:
Partial Array Initialization
Initialization with all
zeros:-
Ex:-
int a[5]={0};
(iii) Initialization without size:- Consider the declaration along with the initialization.
Ex:-
char
b[]={'C','O','M','P','U','T','E','R'};
In this declaration,
eventhough we have not specified exact number of elements to be used in array
b, the array size will be set of the total number of initial values specified.
So, the array size will be set to 8 automatically. The array b is initialized
as shown in figure.
Fig:
Initialization without size
Ex:- int ch[]={1,0,3,5} //
array size is 4
(iv)Array initialization with a string: -Consider the declaration with string initialization.
Ex:-
char b[]="COMPUTER";
The array b is initialized as shown in
figure.
Fig:
Array Initialized with a String
Eventhough the string "COMPUTER" contains 8
characters, because it is a string, it always ends with null character. So, the
array size is 9 bytes (i.e., string length 1 byte for null character).
Ex:
char
b[9]="COMPUTER";
// correct
char
b[8]="COMPUTER";// wrong
2.
Run Time Initialization
An array can be explicitly initialized at run
time. This approach is usually applied for initializing large arrays.
Ex:- scanf can be used to initialize an
array. int x[3]; scanf(“%d%d%d”,&x[0],&x[1],&x[2]);
The
above statements will initialize array elements with the values entered through
the key
board. (Or) for(i=0;i<100;i=i+1)
{
if(i<50)
sum[i]=0.0; else sum[i]=1.0;
}
The first 50 elements of the array sum are
initialized to 0 while the remaining 50 are initialized to 1.0 at run time.
2D arrays
Ans:
An array consisting of two subscripts is known as two-dimensional array. These
are often known as array of the array. In two dimensional arrays the array is
divided into rows and columns. These are well suited to handle a table of data.
In 2-D array we can declare an array as :
Declaration:-
Syntax: data_type array_name[row_size][column_size];
Ex:- int arr[3][3];
where
first index value shows the number of the rows and second index value shows the
number of the columns in the array.
Initializing
two-dimensional arrays:
Like
the one-dimensional arrays, two-dimensional arrays may be initialized by
following their declaration with a list of initial values enclosed in braces.
Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements
of the first row to zero and the second row to one. The initialization is done
row by row.
The above statement can also be written as
int a[2][3] = {{ 0,0,0},{1,1,1}};
by surrounding the elements of each row by
braces.
We can also initialize a two-dimensional
array in the form of a matrix as shown below int a[2][3]={
{0,0,0},
{1,1,1}
};
When the array is completely initialized with all values,
explicitly we need not specify the size of the first dimension.
Ex: int a[][3]={
{0,2,3},
{2,1,2}
};
If the values are missing in an initializer,
they are automatically set to zero.
Ex:
int a[2][3]={
{1,1},
{2}
};
Will initialize the first two elements of the first row
to one, the first element of the second row to two and all other elements to
zero.
The memory is allocation for a 2D array
Ans:
These are stored in the memory as given below.
·
Row-Major order
Implementation
·
Column-Major order
Implementation
In Row-Major Implementation of the arrays, the arrays are stored in the
memory in terms of the row design, i.e. first the first row of the array
is stored in the memory then second and so on. Suppose we have an array named arr
having 3 rows and 3 columns then it can be stored in the memory in the
following manner :
int arr[3][3];
arr[0][0] arr[0][1] arr[0][2]
arr[1][0] arr[1][1] arr[1][2]
arr[2][0] arr[2][1] arr[2][2]
Thus
an array of 3*3 can be declared as follows :
arr[3][3]
= { 1, 2, 3,
4,
5, 6,
7,
8, 9 };
and
it will be represented in the memory with row major implementation as follows :
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
|
|
|
|
|
|
|
|
|
In Column-Major Implementationof the arrays, the arrays are stored in the
memory in the term of the column design, i.e. the first column of the
array is stored in the memory then the second and so on. By taking above eg. we
can show it as follows :
arr[3][3]
= { 1, 2, 3,
4,
5, 6,
7,
8, 9 };
and
it will be represented in the memory with column major implementation as
follows :
1
|
4
|
7
|
2
|
5
|
8
|
3
|
6
|
9
|
|
|
|
|
|
|
|
|
|
Two-dimensional
arrays of variable length
An
array consisting of two subscripts is known as two-dimensional array. These are
often known as array of the array. In two dimensional arrays the array is
divided into rows and columns,. These are well suited to handle the table of
data. In 2-D array we can declare an array as :
Declaration:-
Syntax:
data_type
array_name[row_size][column_size];
Ex: int arr[3][3] ;
Where
first index value shows the number of the rows and second index value shows the
no. of the columns in the array.
These
are stored in the memory as given below.
arr[0][0]
|
arr[0][1]
|
arr[0][2]
|
|
|
|
arr[1][0]
|
arr[1][1]
|
arr[1][2]
|
|
|
|
arr[2][0]
|
arr[2][1]
|
arr[2][2]
|
|
|
|
Initialization
:-
To
initialize values for variable length arrays we can use scanf statement and
loop constructs.
Ex:-
for (i=0 ; i<3; i++)
for(j=0;j<3;j++)
scanf(“%d”,&arr[i][j]);
Multidimensional
arrays are
often known as array of the arrays. In multidimensional arrays the array
is divided into rows and columns, mainly while considering multidimensional
arrays we will be discussing mainly about two dimensional arrays and a bit
about three dimensional arrays.
Syntax: data_type
array_name[size1][size2][size3]------[sizeN];
In 2-D array we can declare an array as :
int arr[3][3] = { 1, 2, 3,
4,
5, 6,
7,
8, 9
};
where first index value shows the number of
the rows and second index value shows the number of the columns in the array.
To access the various elements in 2-D array we can use:
printf("%d",
a[2][3]);
/*
output will be 6, as a[2][3] means third element of the second row of the array
*/
In 3-D we can declare the array in the
following manner :
int
arr[3][3][3] =
{1,
2, 3, 4, 5, 6, 7, 8, 9,
10,
11, 12,
13,
14, 15,
16,
17, 18,
19,
20, 21,
22,
23, 24,
25,
26, 27
};
/* here we have divided array
into grid for sake of convenience as in above declaration we have created 3
different grids, each have rows and columns */
If we want to access the element the in 3-D
array we can do it as follows :
printf("%d",a[2][2][2]);
/*
its output will be 26, as a[2][2][2] means first value in [] corresponds to the
grid no. i.e. 3 and the second value in [] means third row in the corresponding
grid and last [] means third column */
Ex:-
int
arr[3][5][12];
float table[5][4][5][3];
arr is 3D array declared to
contain 180 (3*5*12) int type elements. Similarly table is a 4D array
containing 300 elements of float type.
5. Write a program to perform matrix addition.
Ans: /*ADDITION OF TWO MATRICES*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;
printf("\n Enter the size of Matrix
A:");
scanf("%d%d", &m,&n);
printf("\n Enter the size of Matrix
B:");
scanf("%d%d", &p,&q);
if(m!=p || n!=q)
{
printf("Matrix
addition not possible.");
exit(0);
}
printf(" Enter the Matrix A
values:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Enter the Matrix B
values:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n The Matrix A is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("
%d",a[i][j]);
printf("\n");
}
printf("\n The Matrix B is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("
%d",b[i][j]);
printf("\n");
}
printf("\n The Output Matrix C
is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("
%d",c[i][j]);
printf("\n");
}
}
OUTPUT:
Enter the size of Matrix A:2
3
Enter
the size of Matrix B:2
3
Enter
the Matrix A values:
1
2
3
4
5
6
Enter
the Matrix B values:
6
5
4
3
2
1
The
Matrix A is
1 2
3
4 5
6
The
Matrix B is
6 5
4
3 2
1
The
Output Matrix C is
7 7
7
7 7
7
6. Write a program to perform matrix
multiplication Ans: /*MATRIX MULTIPLICATION*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void
main()
{
int
a[10][10],b[10][10],c[10][10];
int
i,j,k,m,n,p,q;
printf("\
Enter the size of Matrix A:");
scanf("%d%d",
&m,&n);
printf("\
Enter the size of Matrix B:");
scanf("%d%d",
&p,&q);
if(n!=p)
{
printf("Matrix
Multiplication not possible.");
exit(0);
}
printf("
Enter the Matrix A values:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("
Enter the Matrix B values:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("\n
The Matrix A is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("
%d",a[i][j]);
printf("\n");
}
printf("\n
The Matrix B is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("
%d",b[i][j]);
printf("\n");
}
printf("\n
The Output Matrix C is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("
%d",c[i][j]);
printf("\n");
}
}
OUTPUT:
Enter
the size of Matrix A:2
3
Enter
the size of Matrix B:3
2
Enter
the Matrix A values:
2
2
2
2
2
2
Enter
the Matrix B values:
3
3
3
3
3
3
The
Matrix A is
2 2
2
2 2
2
The
Matrix B is
3 3
3 3
3 3
The
Output Matrix C is
1515
15 5
7. Define C string? How to declare and
initialize C strings with an example? Ans: C Strings:-
In C language a string is group of characters (or) array
of characters, which is terminated by delimiter \0 (null). Thus, C uses
variable-length delimited strings in programs.
Declaring Strings:-
C does not support string as a data type. It allows us to
represent strings as character arrays. In C, a string variable is any valid C variable
name and is always declared as an array of characters.
Syntax:- char
string_name[size];
The size determines the number of characters
in the string name.
Ex:- char
city[10];
char
name[30];
Initializing strings:-
There are several methods to initialize
values for string variables.
Ex:- char city[8]=”NEWYORK”;
char
city[8]={„N‟,‟E‟,‟W‟,‟Y‟,‟O‟,‟R‟,‟K‟,‟\0‟};
The string city size is 8
but it contains 7 characters and one character space is for NULL terminator.
Storing strings in memory:-
In C a string is stored in an array of
characters and terminated by \0 (null).
A string is stored in array, the name of the
string is a pointer to the beginning of the string.
The character requires only one memory
location.
If we use one-character string it requires
two locations. The difference is shown
below,
The difference between array and string is
shown below,
Because strings are
variable-length structure, we must provide enough room for maximum-length string
to store and one byte for delimiter.
Why do we need null?
A string is not a datatype
but a data structure. String implementation is logical not physical. The
physical structure is array in which the string is stored. The string is
variable-length, so we need to identify logical end of data in that physical
structure.
String constant (or)
Literal:-
String constant is a
sequence of characters enclosed in double quotes. When string constants are
used in C program, it automatically initializes a null at end of string.
Ex:- “Hello” “Welcome” “Welcome to C Lab”
8. Explain about the string Input/ Output
functions with example? Ans: Reading and Writing strings:-
C
language provides several string handling functions for input and output.
String
Input/Output Functions:-
C provides two basic methods to read and
write strings. Using formatted input/output functions and using a special set
of functions.
Reading
strings from terminal:-
(a) formatted input function:- scanf can be used with %s format specification to read
a string.
Ex:- char name[10];
scanf(“%s”,name);
Here don‟t use „&‟ because name of string
is a pointer to array. The problem with scanf is that it terminates its input
on the first white space it finds.
Ex:- NEW YORK
Reads
only NEW (from above example).
(b) Unformatted input functions:-
(1) getchar():- It is used to read a single character from
keyboard. Using this function repeatedly we may read entire line of text
Ex:- char ch=‟z‟; ch=getchar();
(2) gets():-
It is more convenient method of reading a string of text including blank
spaces.
Ex:- char line[100]; gets(line);
Writing
strings on to the screen:-
(1) Using formatted output functions:- printf with %s format specifier we can
print strings in different formats on to screen.
Ex:-
char name[10];
printf(“%s”,name);
Ex:-
char name[10];
printf(“%0.4”,name);
/* If name is JANUARY prints only 4 characters
ie., JANU */
Printf(“%10.4s”,name);
printf(“%-10.4s”,name);
(2) Using unformatted output functions:-
(a) putchar():-
It is used to print a character on the screen.
Ex:- putchar(ch);
(b) puts():-
It is used to print strings including blank spaces.
Ex:- char line[15]=”Welcome to lab”;
puts(line);
9. Explain about the following string handling functions
with example programs.
(i) strlen (ii) strcpy (iii) strcmp (iv) strcat
Ans:
C supports a number of string handling
functions. All of these built-in functions are aimed at performing various
operations on strings and they are defined in the header file string.h.
(i).
strlen( )
This function is used to find the length of
the string excluding the NULL character. In other words, this function is used
to count the number of characters in a string. Its syntax is as follows:
Int strlen(string);
Example: char str1[ ] = “WELCOME”;
int
n;
n =
strlen(str1);
/*
A program to calculate length of string by using strlen() function*/
#include<stdio.h>
#include<string.h>
main()
{
char string1[50];
int length;
printf(“\n Enter any string:”);
gets(string1);
length=strlen(string1);
printf(“\n The length of string=%d”,length);
}
(ii). strcpy( )
This function is used to copy one string to
the other. Its syntax is as follows:
strcpy(string1,string2);
where string1 and string2 are one-dimensional
character arrays.
This
function copies the content of string2 to string1.
E.g., string1 contains master and string2
contains madam, then string1 holds madam after execution of the strcpy
(string1,string2) function.
Example: char
str1[ ] = “WELCOME”;
char
str2[ ] =”HELLO”;
strcpy(str1,str2);
/*
A program to copy one string to another using strcpy() function */
#include<stdio.h>
#include<string.h>
main( )
{
char string1[30],string2[30];
printf(“\n Enter first string:”);
gets(string1);
printf(“\n Enter second string:”);
gets(string2);
strcpy(string1,string2);
printf(“\n First string=%s”,string1);
printf(“\n Second string=%s”,string2);
}
(iii). strcmp ( )
This function compares two
strings character by character (ASCII comparison) and returns one of three
values {-1,0,1}. The numeric difference is „0‟ if strings are equal .If it is
negative string1 is alphabetically above string2 .If it is positive string2 is
alphabetically above string1.
Its syntax is as follows:
Int
strcmp(string1,string2);
Example: char
str1[ ] = “ROM”;
char
str2[ ] =”RAM”;
strcmp(str1,str2);
(or)
strcmp(“ROM”,”RAM”);
/* A program to compare two
strings using strcmp() function */
#include<stdio.h>
#include<string.h>
main()
{
char string1[30],string2[15];
int x;
printf(“\n Enter first string:”);
gets(string1);
printf(“\n Enter second string:”);
gets(string2);
x=strcmp(string1,string2);
if(x==0)
printf(“\n Both strings are equal”);
else if(x>0)
printf(“\n First string is bigger”);
else
printf(“\n Second string is bigger”);
}
(iv). strcat ( )
This function is used to
concatenate two strings. i.e., it appends one string at the end of the
specified string. Its syntax as follows:
strcat(string1,string2);
where string1 and string2 are one-dimensional
character arrays.
This
function joins two strings together. In other words, it adds the string2 to
string1 and the string1 contains the final concatenated string. E.g., string1
contains prog and string2 contains ram, then string1 holds
program after execution of the strcat() function.
Example: char
str1[10 ] = “VERY”;
char
str2[ 5] =”GOOD”;
22
strcat(str1,str2);
/* A program to concatenate
one string with another using strcat() function*/
#include<stdio.h>
#include<string.h>
main()
{
char string1[30],string2[15];
printf(“\n Enter first string:”);
gets(string1);
printf(“\n Enter second string:”);
gets(string2);
strcat(string1,string2);
printf(“\n Concatenated string=%s”,string1);
}
10.
Write
about storage representation of fixed and variable length format strings with
example?
Ans: String concepts:-
In general a string is a series of characters (or) a
group of characters. While implementation of strings, a string created in
pascal differs from a string created in C language.
1.
Fixed-length
strings:
When
implementing fixed-length strings, the size of the variable is fixed. If we
make it too small we can‟t store, if we make it too big, then waste of memory.
And another problem is we can‟t differentiate data (characters) from non-data
(spaces, null etc).
2.
Variable-length
string:
The solution is creating strings in variable
size; so that it can expand and contract to accommodate data. Two common
techniques used,
(a) Length controlled strings:
These
strings added a count which specifies the number of characters in the string.
Ex:-
5 H E L L O
(b) Delimited strings:
Another technique is using a delimiter at the
end of strings. The most common delimiter is the ASCII null character (\0).
Ex:-
11.
How
can we declare and initialize Array of strings in C? Write a program to read
and display array of strings.
Ans: We have array of integers, array of
floating point numbers, etc.. similarly we have array of strings also.
Collection
of strings is represented using array of strings.
Declaration:-
Char
arr[row][col];
where,
arr
|
-
|
name
of the array
|
row
|
-
|
represents
number of strings
|
col
|
-
|
represents size of each string
|
Initialization:-
char
arr[row][col] = { list of strings };
Example:-
char city[5][10] = {
“DELHI”, “CHENNAI”, “BANGALORE”, “HYDERABAD”, “MUMBAI” };
D
|
E
|
L
|
H
|
I
|
\0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C
|
H
|
E
|
N
|
N
|
A
|
I
|
\0
|
|
|
|
|
|
|
|
|
|
|
|
|
B
|
A
|
N
|
G
|
A
|
L
|
O
|
R
|
E
|
\0
|
|
|
|
|
|
|
|
|
|
|
H
|
Y
|
D
|
E
|
R
|
A
|
B
|
A
|
D
|
\0
|
|
|
|
|
|
|
|
|
|
|
M
|
U
|
M
|
B
|
A
|
I
|
\0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In the above storage representation memory is
wasted due to the fixed length for all strings.
Jagged
Arrays and Pointers
A
jagged array is a two-dimensional array possessing a different number of columns
for each row. Conceptually, this is illustrated in Figure 4-18, where the array has three rows with a varying number of
columns per row.
Figure 4-18. Jagged array
Before
we learn how to create such an array, let’s examine a two-dimensional array
created
using compound literals. A compound literal is a C construct that
consists of what appears to be a cast operator followed by an initializer list
enclosed in braces. An example of a compound literal follows for both a
constant integer and an array of integers. These would be used as part of a
declaration:
(const int) {100}
(int[3]) {10, 20, 30}
In
the following declaration, we create the array arr1 by declaring it
as an array of
pointers
to an integer and using a block statement of compound literals to initialize
it:
int (*(arr1[]))
= {
(int[])
{0, 1, 2},
(int[])
{3, 4, 5},
(int[])
{6, 7, 8}};
This
array has three rows and three columns. The array’s elements are initialized
with the value 0 through 8 in row column order. Figure 4-19 depicts how memory is laid out for this array.
Figure 4-19. Two-dimensional array
The
following sequence displays the addresses and values of each array element:
for(int
j=0; j<3; j++) {
C
Programming Array Application :
Array is used for different verities of applications. Array is used to store the data or values of
same data type. Below are the some of the applications of array –
A. Stores Elements of Same Data Type
Array is used to store the number of elements belonging to same data
type.
int arr[30];
Above array is used to store the integer numbers in an array.
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Similarly if we declare the character array then it can hold only
character. So in short character array can store character variables while
floating array stores only floating numbers.
B. Array Used for Maintaining multiple variable names using
single name
Suppose we need to store 5 roll numbers of students then without
declaration of array we need to declare following –
int roll1,roll2,roll3,roll4,roll5;
1.
Now in order to get roll number of first student we need
to access roll1.
2.
Guess if we need to store roll numbers of 100 students
then what will be the procedure.
3.
Maintaining all the variables and remembering all these
things is very difficult.
Consider the Array :
int roll[5];
So we are using array which can store multiple values and we have to
remember just single variable name.
C. Array Can be Used for Sorting Elements
We can store elements to be sorted in an array and then by using
different sorting technique we can sort the elements.
Different Sorting Techniques are :
1.
Bubble Sort
2.
Insertion Sort
3.
Selection Sort
4.
Bucket Sort
D. Array Can Perform Matrix Operation
Matrix operations can be performed using the array.We can use 2-D array
to store the matrix.
[box]Matrix can be multi-dimensional[/box]
[box]Matrix can be multi-dimensional[/box]
E. Array Can be Used in CPU Scheduling
CPU Scheduling is generally managed by Queue. Queue can be managed and
implemented using the array. Array may be allocated dynamically i.e at run
time. [Animation will Explain more about Round Robin Scheduling Algorithm | Video Animation]
F. Array Can be Used in Recursive Function
When the function calls another function or the same function again then
the current values are stores onto the stack and those values will be retrieve
when control comes back. This is similar operation like stack.
Sparse Array
In computer science, a sparse array is an array in which most of the
elements have the default value (usually 0 or null). The occurrence of
zero-value elements in a large array is inefficient for both computation and
storage. An array in which there is a large number of zero elements is referred
to as being sparse.
In the case of sparse arrays, one can ask for a value from an
"empty" array position. If one does this, then for an array of
numbers, a value of zero should be returned, and for an array of objects, a
value of null should be returned.
A naive implementation of an array may allocate space for the entire
array, but in the case where there are few non-default values, this
implementation is inefficient. Typically the algorithm used instead of an
ordinary array is determined by other known features (or statistical features)
of the array. For instance, if the sparsity is known in advance or if the
elements are arranged according to some function (e.g., the elements occur in
blocks).
A heap memory allocator in a program might choose to store regions of
blank space in a linked list rather than storing all of the allocated regions
in, say a bit array.
Sparse matrix is used in almost all areas of the natural sciences
¨ Matrix that contains
more number of zero elements are called sparse matrices.
¨ Matrices with
relatively high proportion of zero entries are called sparse matrices.
¨ Hence storing more
number of zeros is a waste of memory.
¨ Some of the problems require lot of zeros to be stored as a part of
solution.
No comments:
Post a Comment