ARRAYS
What are Arrays
For understanding the concept of arrays,let us consider the following program:
#include <stdio.h> int main () { int x; x=5; x=10; printf("\nx=%d",x); return 0; }
This program will print the value of x as 10. Here when the value 10 is assigned to x, the earlier value of x i.e.5 is lost.Thus ordinary variables are capable of holding only one value at a time. But in C programming, there are certain situations in which we would want to store more than one value at a time in a single variable.Therefore the concept of arrays are used.
An array is defined as finite ordered collection of homogeneous data, stored in contiguous memory locations.Instead of declaring individual variables, such as a0, a1, ..., and an, you declare one array variable such as numbers and use a[0], a[1], and ..., numbers[n] to represent individual variables. A specific element in an array is accessed by an index.
There are 2 types of C arrays. They are,
- One dimensional array
- Multi dimensional array
ONE- DIMENSIONAL ARRAYS
Array Declaration
To declare an array in C, it is mandatory to specify the type of the elements and the number of elements required by an array. General form of array declaration is as follows:
type arrayName [ arraySize ];The arraySize must be an integer constant greater than zero and type can be any valid C data type.
For example:
int student[10];Here student is an array which is sufficient to hold up to 10 integer numbers.
Array Initialization
After an array is declared it must be initialized. Otherwise, it will contain garbage value. An array can be initialized at either compile time or at runtime.1.Compile time array initialization
Compile time initialization of array elements is same as ordinary variable initialization. The general syntax is,type array_name[size] = {list of values};
The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
Exampleint student[5] = {55,65,57,54,78};
char name[5] = {'a','p','p','l','e'};Another method to initialize array during declaration:
int student[] = {55,65,57,54,78};
2.Run time array initialization
An array can also be initialized at run time using scanf() function. This approach is usually used for initializing large array, or to initialize array with user specified values.
Example
Example
#include <stdio.h> int main () { int n[ 10 ]; int i,j; printf("enter the elements of the array\n"); for ( i = 0; i < 10; i++ ) { scanf("%d",&n[i]); } printf("value in the array"); for (j = 0; j < 10; j++ ) { printf("\n%d",n[j] ); } return 0; }
MULTIDIMENSIONAL ARRAYS
Declaration
In C programming, we can create an array of arrays known as multidimensional array. The general form is:type name[size1][size2]...[sizeN];For example:
int class[5][10][4];
Initialization
2-D ARRAY
Two dimensional arrays can be initialized in different ways:
int a[2][3] = {{1,3,0},{-1,5,9}}; int a[][3] = {{1,3,0},{-1,5,9}}; int a[2][3] = {1,3,0,-1,5,
Three dimensional arrays can be initialized similar to that of 2-D arrays.
int test[2][3][4] ={ {{3,4,2,3},{0,-3,9,11},{23,12,23,2}}, {{2,56,3,23},{2,1,21,23},{1,12,32,7}} };
EXAMPLE 1 : SUM OF TWO MATRICES
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], c[2][2];
int i, j;
// Taking input using nested for loop
printf("Enter elements of 1st matrix\n");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("Enter a%d%d: ", i+1, j+1);
scanf("%f", &a[i][j]);
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrix\n");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("Enter b%d%d: ", i+1, j+1);
scanf("%f", &b[i][j]);
}
// adding corresponding elements of two arrays
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
// Displaying the sum
printf("\nSum Of Matrix:\n");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("%.1f\t", c[i][j]);
if(j==1)
printf("\n");
}
return 0;
}
EXAMPLE 2:To store values entered by the user in a three-dimensional array and display it.
#include <stdio.h>
int main()
{
// this array can store 8 elements
int i, j, k, test[2][2][2];
printf("Enter 8 values: \n");
for(i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
for(k = 0; k < 2; ++k ) {
scanf("%d", &test[i][j][k]);
}
}
}
// Displaying values with proper index.
printf("\nDisplaying values:\n");
for(i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
for(k = 0; k < 2; ++k ) {
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
Comments
Post a Comment