0% found this document useful (0 votes)
21 views21 pages

C Language (Day-08)

An array is a collection of similar elements stored in contiguous memory locations. It allows storing multiple elements of the same data type through a common name. Some key points about arrays are: arrays must be declared before use by specifying the data type and number of elements, arrays use zero-based indexing, and array elements are stored sequentially in memory.

Uploaded by

bhumika_khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views21 pages

C Language (Day-08)

An array is a collection of similar elements stored in contiguous memory locations. It allows storing multiple elements of the same data type through a common name. Some key points about arrays are: arrays must be declared before use by specifying the data type and number of elements, arrays use zero-based indexing, and array elements are stored sequentially in memory.

Uploaded by

bhumika_khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• Concept of Array and its operations

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


What is an Array
Array is a collection or group of similar data type elements stored in contiguous
memory location. The individual data items can be characters, integers, floating
points numbers and so on. Here contiguous memory allocation means array
occupies contiguous bytes as needed in the memory.
Or
An array is a collection of similar elements. These similar elements could be all
ints, or all floats, or all chars, etc. Usually, the array of characters is called a
‘string’, whereas an array of ints or floats is called simply an array. Remember
that all elements of any given array must be of the same type.
 
i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Types of an Array
 Single / One Dimensional Array
 Multidimensional Array
 
Single / One Dimensional Array :
The array which is used to represent and store data in a linear form is
called as 'single or one dimensional array’. Has a unique identifier for
each element, called as a subscript or an index.
 
Syntax: <data-type> <array_name> [size];
Example: int a[5];

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Memory allocation for 1D Array
A[0] A[1] A[2] A[n]
5 7 2 12
1000 1002 1004 n

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
 An array is a collection of similar elements.
 The first element in the array is numbered 0, so the last element is 1 less
than the size of the array.
 An array is also known as a subscripted variable.
 Before using an array its type and dimension must be declared.
Bounds Checking
In C there is no check to see if the subscript used for an array exceeds the size of the array. Data
entered
with a subscript exceeding the array size will simply be placed in memory outside the array. This will
lead to unpredictable results.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
There are number of operations that can be performed on arrays. These
operations include :
Transversal
Insertion
Search
Deletion
Merging
Sorting

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


#include<stdio.h>
#include<conio.h>
main()
{
int x[10], i;
for(i=0;i<10;i++)
{
printf(“Enter the elements in array: \t”);
scanf(“%d”, &x[i]);
}
printf(“The Traverse of array is:\n”);
for(i=0;i<10;i++)
printf(“%d\n”, x[i]);
getch();
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
#include<stdio.h>
void main()
{
   int x[5],l, i;
   printf("\nEnter the elements of an array :");
   for (i=0; i < 5; i++)
   scanf("%d", &x[i]);
l=x[0]; 3 5 6 2 10
   for (i=0; i < 5; i++)
   if (l < x[i])
l=x[i];

   printf(“\nLargest Element is %d", l);


}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


#include<stdio.h>
void main()
{
   int x[5], ele, i;
   printf("\nEnter the values :");
   for (i = 0; i < 5; i++)
   scanf("%d", &x[i]);
   printf("\nEnter the elements to be searched :");
   scanf("%d", &ele);
   for (i = 0; i < 5; i++) {
if(x[i]==ele)
{
printf("element found "); }
}
  printf("Number not found");
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
#include <stdio.h>  
void main()
{
int x[5], p, c, n;  
printf("Enter the elements\n”);  
for ( c=0 ; c<5 ; c++ )
scanf("%d", &x[c]);  
printf("Enter the location where you wish to delete element\n");
scanf("%d", &p);  
if ( p>=5 )
printf("Deletion not possible.\n");
else
{
for (c=p -1 ; c < 5-1 ; c++ )
x[c] = x[c+1];  
printf("Resultant array is\n");  
for( c=0 ; c <5-1 ; c++ )
printf("%d\n", x[c]);
}
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
#include<stdio.h>
#include<conio.h>
main()
{ int x[5],i,j,t;
clrscr();
0 1 2 3 4
printf("\nenter the array element");
for(i=0;i<5;i++) 5 7 2 12 6
scanf("%d",&x[i]);
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
if(x[i]>x[j]) 0 1 2 3 4
{ t=x[i]; 2 5 6 7 12
x[i]=x[j];
x[j]=t; }
for(i=0;i<5;i++)
printf("\n%d",x[i]);
getch();
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1. What is Array?
A. Collection of different type of elements B. Collectionof similar type of elements
C. None of the above D. Both A and C Ans :-  B

2. What is right way to Initialize array?


A. int num[6] = { 2, 4, 12, 5, 45, 5 }; B. int n{} = { 2, 4, 12, 5, 45, 5 };
C. int n{6} = { 2, 4, 12 }; D. int n(6) = { 2, 4, 12, 5, 45, 5 }; Ans :-  A

3. Which of these best describes an array?


A. A data structure that shows a hierarchical behavior B. Container of objects of similar types
C. Arrays are immutable once initialised D. Array is not a data structure Ans :-  B

4. What is an Array in C language.?


A. A group of elements of same data type.
B. An array contains more than one element
C. Array elements are stored in memory in continuous or contiguous locations.
D. All the above. Ans :-  D

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

5. What are the Types of Arrays?


A. int, float, char, double B. struct, enum
C. Long D. All the above Ans :-  D

6. What is the maximun number of dimensions an array in C may have?


A. 2 B. 8
C. 16 D. No limit Ans :-  D

7. What will happen if in a C program you assign a value to an array element whose subscript exceeds the
size of array?
A. The element will be set to 0.
B. The compiler would report an error.
C. The program may crash if some important data gets overwritten.
D. The array size would appropriately grow.  Ans :-  C

8. Size of the array need not be specified, when


A. It is a formal parameter B. It is a declaratrion
C. Initialization is a part of definition D. All of these Ans :- C  
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

9. An array elements are always stored in ________ memory locations.


A. Random B. None of the above
C. Sequential D. Sequential and Random Ans :- C 

10. Array is an example of _______ type memory allocation.


A. Compile time B. Run time
C. Both A and B D. None of the above Ans :- A 

11. The parameter passing mechanism for an array is


A. call by reference B. call by value
C. call by value-result D. None of the above Ans :-  A 

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Assignments
 Write a program to print the sum of all the array elements.
 Write a program to copy an array in to another array.
 Write a program to find the largest value of an array.
 Write a program to insert any new element in an array.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like