9953330565 Low Rate Call Girls In Rohini Delhi NCR
Array
1.
2. Array
• a fixed-size sequential collection of elements of the same type
• a collection of variables of the same type
• arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
• An array is a collection of similar data items stored in continuous memory
locations with single name.
3. Array indexes always begin with 0. Hence when we say array of size 10, array has
elements from index 0 to 9. If we specify or use array as intArr[10], intArr[11],
intArr[200], the C compiler will not show any error, but we will get run time errors
while executing the program
4.
5. Characteristics of Array
• An array holds elements that have the same data type.
• Array elements are stored in contiguous memory locations.
• All the elements of an array share the same name.
• Array name represents the address of the starting element.
6. Advantage of Array
• Using arrays, other data structures like linked lists, stacks, queues, trees, graphs
etc can be implemented.
• Two-dimensional arrays are used to represent matrices.
• Arrays represent multiple data items of the same type using a single name.
• In arrays, the elements can be accessed randomly by using the index number.
• It is better and convenient way of storing the data of same datatype with same
size.
• It allows us to store known number of elements in it.
• It allocates memory in contiguous memory locations for its elements. It does not
allocate any extra space/ memory for its elements. Hence there is no memory
overflow or shortage of memory in arrays.
7.
8. Disadvantage of Array
• The number of elements to be stored in an array should be known in advance.
• An array is a static structure (which means the array is of fixed size). Once
declared the size of the array cannot be modified. The memory which is allocated
to it cannot be increased or decreased.
• Insertion and deletion are quite difficult in an array as the elements are stored in
consecutive memory locations and the shifting operation is costly.
• Allocating more memory than the requirement leads to wastage of memory space
and less allocation of memory also leads to a problem.
• It is not possible to hold dissimilar data type.
10. One Dimensional Array
• single dimensional arrays are used to store a row of values. In single dimensional
array, data is stored in linear form.
• Single dimensional arrays are also called as one-dimensional arrays, Linear
Arrays or simply 1-D Arrays.
• An array which has only one subscript is termed as one dimensional array.
• For example:
• Char name[25];
11. Declaration of one dimensional array
• Data_type array_name[array_size];
The type of array
elements
The name of array
variable
The number of
elements that can
be stored
12. For Example: declaration of one dimensional array
Syntax:
data_type array_name[array_size];
Example:
char name[25];
int a[10];
float temperature[25];
float marks[25];
char fname[20];
14. #include <stdio.h>
void main()
{
int arr[10];
int i;
printf("Input 10 elements in the
array :n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
printf("nElements in array are:
");
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("n");
}
WAP to input 10 numbers in array and display them
15. WRITE A PROGRAM TO READ AGE OF N PERSONS AND DISPLAY ONLY
THOSE PERSONS WHOSE BETWEEN 50 AND 60.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,age[100],count=0;
printf("Enter the number of persons ::
");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("nEnter age of %d persons ::
",i);
scanf("%d",&age[i]);
}
for (i=1;i<=n;i++)
{
if(age[i]>50 && age[i] < 60)
count++;
}
printf("nnNumber of persons whose
age between 50-60 are :: %d",count);
getch();
}
16. WAP to input ‘n’ numbers and sort them in ascending order
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N n");
scanf("%d", &n);
printf("Enter the numbers n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}}}
printf("The numbers arranged in ascending
order are given below n");
for (i = 0; i < n; ++i)
printf("%dn", number[i]);
}
17. Array to have sum of numbers
#include <conio.h>
int main()
{
int a[1000],i,n,sum=0;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum+=a[i];
}
printf("sum of array is : %d",sum);
return 0;
}
18.
19. #include <stdio.h>
int main()
{
int n, r = 0, t;
printf("Enter a number to check if
it's a palindrome or notn");
scanf("%d", &n);
t = n;
while (t != 0)
{
r = t%10;
r = r + t*10;
t = t/10;
}
if (n == r)
printf("%d is a palindrome
number.n", n);
else
printf("%d isn't a palindrome
number.n", n);
return 0;
}
Palindrome
20. Multi Dimensional Array
• An array of arrays is called as multi dimensional array. In simple words, an
array created with more than one dimension (size) is called as multi dimensional
array.
• Multi dimensional array can be of two dimensional array or three
dimensional array or four dimensional array or more...
• Most popular and commonly used multi dimensional array is two dimensional
array. The 2-D arrays are used to store data in the form of table. We also use
2-D arrays to create mathematical matrices.
• The maximum capacity of elements of array is the product of row size and column
size.
21. Declaration of multi dimensional array
Data_type array_Name [ row_Size ] [ column_Size ] ;
For example:
int a[m][n];
Here a[0][0] is initial and a[m-1][n-1] is the last element.
22.
23. Initialization of multi dimensional array
2-D array can be initialized in a way similar to that of 1-D array. for example:-
int mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22};
These values are assigned to the elements row wise, so the values of
elements after this initialization are
Mat[0][0]=11, Mat[1][0]=14, Mat[2][0]=17 Mat[3][0]=20
Mat[0][1]=12, Mat[1][1]=15, Mat[2][1]=18 Mat[3][1]=21
Mat[0][2]=13, Mat[1][2]=16, Mat[2][2]=19 Mat[3][2]=22
24. • While initializing we can group the elements row wise using inner
braces.
for example:-
int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}};
• If we initialize an array as
int mat[4][3]={{11},{12,13},{14,15,16},{17}};
Then the compiler will assume its all rest value as 0,which are not defined.
Mat[0][0]=11, Mat[1][0]=12, Mat[2][0]=14, Mat[3][0]=17
Mat[0][1]=0, Mat[1][1]=13, Mat[2][1]=15 Mat[3][1]=0
Mat[0][2]=0, Mat[1][2]=0, Mat[2][2]=16, Mat[3][2]=0
25. WAP to input data in 2-dimensional array and display in matrix form
#include<stdio.h>
int main(){
int disp[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:",
i, j);
scanf("%d", &disp[i][j]);
} }
//Displaying array elements
printf("Two Dimensional array
elements:n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("n");
}}}
return 0;
}
26.
27. Transpose of a matrix: 2-dimensional array
#include <stdio.h>
void main()
{
int array[10][10];
int i, j, m, n;
printf("Enter the order of the matrix n");
scanf("%d %d", &m, &n);
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("Transpose of matrix is n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
printf(" %d", array[i][j]);
}
printf("n");
}
}
29. #include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],i,j;
clrscr();
printf("nENTER VALUES FOR
MATRIX A:n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("nENTER VALUES FOR
MATRIX B:n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("nTHE VALUES OF
MATRIX C ARE:n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("n");
getch();
30. Assignments
• WAP to find transpose of 3*3 matrix.
• WAP to subtract two 3*3 matrices.
• WAP to multiply two 2*2 matrices.
• WAP to take salary of 100 employee and sort them on the basis of
ascending order.
• WAP to take ‘n’ input and find greatest and least number among
them.
• WAP to input marks of 20 students and display their sum and
average marks.
32. #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];
printf("Enter number of rows and
columns of first matrixn");
scanf("%d%d", &m, &n);
printf("Enter elements of first
matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and
columns of second matrixn");
scanf("%d%d", &p, &q);
printf("Enter elements of second
matrixn");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum +
first[c][k]*second[k][d];
multiply[c][d] = sum;
sum = 0;
}}
printf("Product of the matrices:n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%dt", multiply[c][d]);
printf("n");
}}
return 0;
}