Data Structure and Algorithm - Edited
Data Structure and Algorithm - Edited
And
Algorithm
Assignment on array and sparse matrix
ROLL No. – 28
SECTION – E(2)
An array is a collection of data items, all of the same type, accessed using a common
name.
A one-dimensional array is like a list; a two dimensional array is like a matrix. The C
language places no limits on the number of dimensions in an array, though specific
implementations may.
Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays
as matrices, and use the general term arrays when the number of dimensions is
unspecified or unimportant.
Declaring Arrays
Array variables are declared identically to variables of their data type, except that the
variable name is followed by one pair of square [ ] brackets for each dimension of the
array.
Uninitialized arrays must have the dimensions of their rows, columns, etc. listed
within the square brackets.
Dimensions used when declaring arrays in C must be positive integral constants or
constant expressions.
Examples:
Initializing Arrays
Arrays may be initialized when they are declared, just as any other variables.
Place the initialization data in curly {} braces following the equals sign. Note the use
of commas in the examples below.
An array may be partially initialized, by providing fewer data items than the size of
the array. The remaining array elements will be automatically initialized to zero.
If an array is to be completely initialized, the dimension of the array is not required.
The compiler will automatically size the array to fit the initialized data.
Examples:
inti = 5, intArray[ 6 ] = { 1, 2, 3, 4, 5, 6 };
Using Arrays
Elements of an array are accessed by specifying the index (offset) of the desired
element within square [ ] brackets after the array name.
Array subscripts must be of integer type. ( int, long int, char, etc. )
VERY IMPORTANT: Array indices start at zero in C, and go to one less than the
size of the array. For example, a five element array will have indices zero through
four. This is because the index in C is actually an offset from the beginning of the
array. ( The first element is at the beginning of the array, and hence has zero offset. )
Arrays are commonly used in conjunction with loops, in order to perform the same
calculations on all ( or some part ) of the data items in the array.
The first sample program uses loops and arrays to calculate the first twenty Fibonacci
numbers. Fibonacci numbers are used to determine the sample points used in certain
optimization methods.
#include <stdlib.h>
#include <stdio.h>
inti, fibonacci[ 20 ];
fibonacci[ 0 ] = 0;
fibonacci[ 1 ] = 1;
#include <stdlib.h>
#include <stdio.h>
int numbers[ 10 ];
inti, index = 2;
for(i = 0; i< 10; i++ )
numbers[ i ] = i * 10;
numbers[ 8 ] = 25;
numbers[ 5 ] = numbers[ 9 ] / 3;
numbers[ 4 ] += numbers[ 2 ] / numbers[ 1 ];
numbers[ index ] = 5;
++numbers[ index ];
numbers[ numbers[ index++ ] ] = 100;
numbers[ index ] = numbers[ numbers[ index + 1 ] / 7 ]--;
Multidimensional Arrays
Multi-dimensional arrays are declared by providing more than one set of square [ ]
brackets after the variable name in the declaration statement.
One dimensional arrays do not require the dimension to be given if the array is to be
completely initialized. By analogy, multi-dimensional arrays do not require the
first dimension to be given if the array is to be completely initialized. All dimensions
after the first must be given in any case.
For two dimensional arrays, the first dimension is commonly considered to be the
number of rows, and the second dimension the number of columns.
A way of looking at this is that C stores two dimensional arrays by rows, with all
elements of a row being stored together as a single unit.
Multidimensional arrays may be completely initialized by listing all data elements
within a single pair of curly {} braces, as with single dimensional arrays.
It is better programming practice to enclose each row within a separate subset of curly
{} braces, to make the program more readable. This is required if any row other than
the last is to be partially initialized. When subsets of braces are used, the last item
within braces is not followed by a comma, but the subsets are themselves separated by
commas.
Multidimensional arrays may be partially initialized by not providing complete
initialization data. Individual rows of a multidimensional array may be partially
initialized, provided that subset braces are used.
Individual data items in a multidimensional array are accessed by fully qualifying an
array element. Alternatively, a smaller dimensional array may be accessed by
partially qualifying the array name. For example, if "data" has been declared as a
three dimensional array of floats, then data[ 1 ][ 2 ][ 5 ] would refer to a float,
data[ 1 ][ 2 ] would refer to a one-dimensional array of floats, and data[ 1 ] would
refer to a two-dimensional array of floats. The reasons for this and the incentive to do
this relate to memory-management issues that are beyond the scope of these notes.
Sample Program Using 2-D Arrays
#include <stdlib.h>
#include <stdio.h>
return 0;
}
Application of array
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.
#include <stdio.h>
int main()
{
inti, n;
floatarr[100];
return 0;
}
Output
#include <stdio.h>
int main()
{
intavg = 0;
int sum =0;
int x=0;
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
Output:
Enter number 1
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20
Question 3: WAP to implement selection sort algorithm
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
return 0;
}
Output
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
return 0;
}
Output
A matrix is a two-dimensional data object made of m rows and n columns, therefore having
total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse
matrix.
Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used
to store only those elements.
Computing time: Computing time can be saved by logically designing a data structure
traversing only non-zero elements.
Example:
00304
00570
00000
02600