In c programming one of the frequently problem is to handle similar types of data. For example
Consider a variable use to store data that we studied, like to store fees of students Here we Can Take a variable fees to store the values in it fees = 4000; but a college will have Hundreds of Variable to store the fees of all students so we need to consider hundred of Variables of variables It is way to define it secondly to take only one variable and store all Students’ fees in that Variable here the second approach is more beneficial as it avoid the Need of writing Hundreds of Statements.
Array
is a collection of same type of elements arranged one by one in the special sequence
or
Array is a variable to store a group of
elements of same data type
i.e. once array is declared as integer, then all values which are present in an array are Of type integer only. However we cannot store different type of elements into the same array. For example we cannot store integer. Float and character data type together in same array
Uses of array
It has the ability to access any member element directly
It is used to store data in large quantities
It can be used to implement mathematical vector and matrices as well as other kind of Rectangular tables
It is used to implement data structure such as hash, string stack heaps tables etc.
Also used dynamic memory allocation and determine partial or complex control flow in Program.
Types
of array
Array can be
classified in to two type
1. One dimensional array
2. Two dimensional array
1. One dimensional array
One dimensional array represent a single row and column of elements the row indicates horizontally arrangement of elements . Column indicates vertical arrangement of elements.
Syntax
Data type array _name [array size] ;
Here “data type “is the type of the array we want to define “array name “is the name given to the array and “array size ” is the size of the array that we want to assign to the array the array size is always mentioned inside the“ [ ] ”
eg
int age [5];
int |
age |
[10]; |
The following will be the result of the above declarations:
Age [0] age[1] age[2] age[3] age[4]
|
|
|
|
|
initialized one
dimensional array
syntax
data a type array _name[size]={list of value}
int arr [5]={2,4,6,8,10 };
in this case , the compiler determines the size of array by calculating the number of elements of array.
age[0] age[1] age[2] age[3] age[4]
2 |
4 |
6 |
8 |
10 |
ex
#include<stdio.h>
#include<conio.h>
Void
main ()
{
Int
a[5]={2,4,6,8,10};
Int i=0;
For(
i=0;b i<=4; i++)
{
Printf(“\n
%d”,a[i]);
}
getch();
}
OUTPUT:
2
4
6
8
10
2 .two
dimensional array:
A two dimensional array represents more than one row and column. Two dimensional arrays Can be used to store the tabular data. This means that when the data is in the form of row as Well as column. to access a particular element from the array we have to use two Subscripts. One for row number and other for column number. The notation is of the form array [i] [j] where i Stands for row subscript and j stands for column subscript. The array holds i*j elements. Suppose there is a multidimensional array array [i] [j] [k] [l]. Then this array can hold i*j*k*l Numbers of data in the same way, the array of any dimensional can be Initialized in c Programming
Syntax
Data type array _name [row] [columns];
eg
int marks[4] [3];
|
Column 1 |
Column 2 |
Column 3 |
|
|
|
|
Row 1 |
Mark[0[0] |
Mark[0[1] |
Mark[o[3] |
Row 2 |
Mark[1[0] |
Mark[1[1] |
Mark[1[3] |
Row 3 |
Mark[2[0] |
Mark[2[1] |
Mark[2[3] |
Row 4 |
Mark[3[0] |
Mark[3[1] |
Mark[3[3] |
Initialized
two dimensional arrays:
syntax
data type array _name [r]
[c];={list of values} ;
eg
int arr[2] [3]={
{1,2,3},{4,5,6} };
EX
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
printf("Enter elements of 1st matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
print("Enter a%d%d: ", i + 1, j
+ 1);
scarf("%f", &a[i][j]);
}
printf("Enter elements of 2nd matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter b%d%d: ", i + 1,
j + 1);
scanf("%f", &b[i][j]);
}
for
(int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
result[i][j] = a[i][j] + b[i][j];
}
print("\sum Of Matrix:");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
prints("%.1f\t", result[i][j]);
if (j == 1)
printf("\n");
}
return 0;
}
Output
Enter elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;e
Enter b21: 0.23;
Enter b22: 23;
output
Sum Of Matrix:
2.2 0.5
-0.9 25.0
No comments:
Post a Comment