manishnips.blogspost.com

Array in Data structure


                                   WELCOME FRIENDS  at manishnips.net                               https://www.blogger.com/profile/11710251641564278815
 

Hello friends !
I am Manish Gupta.
And welcome to you all at https://www.blogger.com/profile/11710251641564278815 (manishnips.net)

How are you ?
In this page I describe  Introduction of Array in Data structure and also describe the program of Array.

अगर आप programming के field में अपना carrier बनाना चाहते हैं तो आपको सबसे पहले Array के basics को सीखना होगा उसके बाद ही आपको बड़े बड़े program समझ आयेंगे|
C programming के इस tutorial series में मैंने program लिखने और compile करने के steps बताएं हैं| मुझे उम्मीद है की यह पोस्ट आपके लिए काफी हेल्पफुल होगा| इस पोस्ट को अपने दोस्तों के साथ जरुर share करें| manishnips blog पर आने के लिए धन्यवाद|






Hello Friends! C Programming tutorial के इस page में आज मैं आपको बताऊंगा की Using of Array  writes C program और उस प्रोग्राम को कैसे compile कराते हैं| मैंने पिछले पोस्ट में बताया था की Array क्या  होता  हैं?

Array

Ø An arrays is a fixed size sequence collection of element of the same type.
Ø An arrays is a derived data type.
Ø When we want to handle large amount of data in the term of storing, accessing and manuculating then we use arrays.
Ø It takes common name for all its elements.
Ø Arrays works on index numbers and index begins from 0 and ends with size-1.
Ø An arrays is a collection of similar data type in which each element is located in seprate memory location.
Ø Array is static and linear data structure.
Characteristics of an array
Ø All the elements of an array shares the common name and they are distinigius from one another through the index number.
Ø In particular element of an array can be modified seprate without affecting other element.
Ø Index number plays an essential role for calling each element.
Ø An Array element can be equated to another ordinary variable.
Ø The arrays element are stored in continous memeory location.
Ø Size of an arrays depends on arrys types and number of element.
Types of an array
Ø There are two types of an Array
1.    Single dimensional array
2.    Multi dimensional array
      I.            Single dimensional array
Ø An array which is declare with only one index, is called single dimensional array.
Ø It is used for searching, sorting etc…
Ø Syntax….
Data type Array name [size];
      Example……
 int ar [30];

   II.            Multi(two) dimensional Array
Ø An array which works on double index, is known as Multi dimensional array.
Ø It is used for Matrix manuculation.
Ø Syntax….
Data type Array name[size][size];
   Example…..                 
                             Int ar[40][30];

Program Related of Arrays..


Que.. WAP to find sum of five nos. using Array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={12,15,17,19,21};
int i,s=0;
clrscr();
printf("Enter arrays elements are=");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
s=s+a[i];
}
printf("\nSum=%d",s);
getch();
}
OUTPUT
MANISH NIPS


अगर आप programming के field में अपना carrier बनाना चाहते हैं तो आपको सबसे पहले Array के basics को सीखना होगा उसके बाद ही आपको बड़े बड़े program समझ आयेंगे|
C programming के इस tutorial series में मैंने program लिखने और compile करने के steps बताएं हैं| मुझे उम्मीद है की यह पोस्ट आपके लिए काफी हेल्पफुल होगा| इस पोस्ट को अपने दोस्तों के साथ जरुर share करें| manishnips blog पर आने के लिए धन्यवाद|

Que… WAP to find sum of array.

#include<stdio.h>

#include<conio.h>

void main()

{

int a[5]={12,15,17,19,21};

int s=0;

clrscr();

s=a[0]+a[1]+a[2]+a[4]+a[3];

printf("\nSum=%d",s);

getch();

}

OUTPUT
MANISH NIPS

Que… WAP to find sum of an array…
#include<stdio.h>
#include<conio.h>

void main()
{
int ar[50],n,s=0,i;
clrscr();
printf("Enter numbers of elements=");
scanf("%d",&n);
printf("Enter array's elements are=");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("entered elements are");
for(i=0;i<n;i++)
{
printf("\n%d",ar[i]);
s=s+ar[i];
}
printf("\nsum=%d",s);
getch();
}

OUTPUT
MANISH NIPS

Que.. WAP to enter elements in an array & print them.
#include<stdio.h>
#include<conio.h>
void main()
{
int ar[50],i,n;
clrscr();
printf("Enter number of elements=");
scanf("%d",&n);
printf("Enter arrays elements are=");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("Reversr order=");
for(i=0;i<n;i++)
{
printf("\n%d",ar[i]);
}
getch();
}
Que… WAP to print entered elemnt in reverse order…
#include<stdio.h>
#include<conio.h>
void main()
{
int ar[50],i,n;
clrscr();
printf("Enter number of elements=");
scanf("%d",&n);
printf("Enter arrays elements are=");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("Reversr order=");
for(i=n-1;i>=0;i--)
{
printf("\n%d",ar[i]);
}
getch();
}
OUTPUT
MANISH NIPS

Que…. WAP to find sum of entered arrays elements…

#include<stdio.h>
#include<conio.h>

void main()
{
int ar[50],n,s=0,i;
clrscr();
printf("Enter numbers of elements=");
scanf("%d",&n);
printf("Enter array's elements are=");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("entered elements are");
for(i=0;i<n;i++)
{
printf("\n%d",ar[i]);
s=s+ar[i];
}
printf("\nsum=%d",s);
getch();
}

OUTPUT
MANISH GUPTA
Que… WAP to find smallest element in arrays.

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[50],i,n,s;
clrscr();
printf("Enter number of elements=");
scanf("%d",&n);
printf("Enter arrays elements are=");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
s=ar[0];
for(i=1;i<n;i++)
{
if(s>ar[i])
s=ar[i];
}
printf("smallest element=%d",s);

getch();
}

OUTPUT
MANISH NIPS

Que…. WAP to find gretest element  of an arrays.

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[50],n,i,g;
clrscr();
printf("Enter number of arrays elements=");
scanf("%d",&n);
printf("Enter array's elements=");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
g=ar[0];
for(i=1;i<n;i++)
{
if(g<ar[i])
g=ar[i];
}
printf("Gretest arrays elements is=%d",g);

getch();
}
OUTPUT
MANISH NIPS

Que… WAP to find smallest & gretest element of an arrays


#include<stdio.h>
#include<conio.h>
void main()
{
int ar[50],n,i,g,s;
clrscr();
printf("Enter number of arrays elements=");
scanf("%d",&n);
printf("Enter array's elements=");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
g=ar[0];
s=ar[0];
for(i=1;i<n;i++)
{
if(g<ar[i])
g=ar[i];
if(s>ar[i])
s=ar[i];
}
printf("Gretest arrays elements is=%d",g);
printf(“Smallest arrays elements is=%d”,s);
getch();
}

OUTPUT
MANISH NIPS
Que…WAP to enter element in a  two arrays…

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j;
clrscr();
printf("Enter matrix elements of A=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix elements of B=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Matrix format of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nMatrix format of B\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT
MANISH NIPS

Que…. WAP to print transpose of a matrix using Array….

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("Enter matrix elements of A=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Matrix format of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nTranspose of Matrix A=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
getch();
}

OUTPUT
MANISH NIPS

Que… WAP to find sum of two matrixes  using Array…

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j;
clrscr();
printf("Enter matrix elements of A=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix elements of B=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Matrix format of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nMatrix format of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\nSum of two matrices=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]+b[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT
MANISH NIPS

Que… WAP to find subtraction of two matrixes  using Array…

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j;
clrscr();
printf("Enter matrix elements of A=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix elements of B=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Matrix format of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nMatrix format of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\nSum of two matrices=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]-b[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT
MANISH NIPS


Que….. WAP to findsum of colomn ,row and diagonal of matrices. Using Arrays..
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,rs1=0,rs2=0,rs3=0,cs1=0,cs2=0,cs3=0,ds=0;
clrscr();
printf("Enter array's elements A=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix format A=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Format=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==0)
rs1=rs1+a[i][j];
if(i==1)
rs2=rs2+a[i][j];
if(i==2)
rs3=rs3+a[i][j];
if(j==0)
cs1=cs1+a[i][j];
if(j==1)
cs2=cs2+a[i][j];
if(j==2)
cs3=cs3+a[i][j];
if(i==j)
ds=ds+a[i][j];
printf("%d\t",a[i][j]);
}
if(i==0)
printf("%d",rs1);
if(i==1)
printf("%d",rs2);
if(i==2)
printf("%d\t",rs3);
printf("\n");
}
printf("%d\t%d\t%d\t%d\t",cs1,cs2,cs3,ds) ;
getch();
}

OUTPUT
MANISH NIPS

 

 # In other method, Describe Array in Data structure


Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.
  • Element − Each item stored in an array is called an element.
  • Index − Each location of an element in an array has a numerical index, which is used to identify the element.

Array Representation

Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Array Declaration
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Array Representation
As per the above illustration, following are the important points to be considered.
  • Index starts with 0.
  • Array length is 10 which means it can store 10 elements.
  • Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.

Basic Operations

Following are the basic operations supported by an array.
  • Traverse − print all the array elements one by one.
  • Insertion − Adds an element at the given index.
  • Deletion − Deletes an element at the given index.
  • Search − Searches an element using the given index or by the value.
  • Update − Updates an element at the given index.
In C, when an array is initialized with size, then it assigns defaults values to its elements in following order.
Data TypeDefault Value
boolfalse
char0
int0
float0.0
double0.0f
void
wchar_t0

Insertion Operation

Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of the array −

Algorithm

Let Array be a linear unordered array of MAX elements.

Example

Result
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA −
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop

Example

Following is the implementation of the above algorithm −
 Live Demo
#include <stdio.h>

main() {
   int LA[] = {1,3,5,7,8};
   int item = 10, k = 3, n = 5;
   int i = 0, j = n;
   
   printf("The original array elements are :\n");

   for(i = 0; i<n; i++) {
      printf("LA[%d] = %d \n", i, LA[i]);
   }

   n = n + 1;
 
   while( j >= k) {
      LA[j+1] = LA[j];
      j = j - 1;
   }

   LA[k] = item;

   printf("The array elements after insertion :\n");

   for(i = 0; i<n; i++) {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
}
When we compile and execute the above program, it produces the following result −

Output

The original array elements are :
LA[0] = 1 
LA[1] = 3 
LA[2] = 5 
LA[3] = 7 
LA[4] = 8 
The array elements after insertion :
LA[0] = 1 
LA[1] = 3 
LA[2] = 5 
LA[3] = 10 
LA[4] = 7 
LA[5] = 8 
For other variations of array insertion operation click here

Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all elements of an array.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop

Example

Following is the implementation of the above algorithm −
 Manish Gupta
#include <stdio.h>

void main() {
   int LA[] = {1,3,5,7,8};
   int k = 3, n = 5;
   int i, j;
   
   printf("The original array elements are :\n");
 
   for(i = 0; i<n; i++) {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
    
   j = k;
 
   while( j < n) {
      LA[j-1] = LA[j];
      j = j + 1;
   }
 
   n = n -1;
   
   printf("The array elements after deletion :\n");
 
   for(i = 0; i<n; i++) {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
}
When we compile and execute the above program, it produces the following result −

Output

The original array elements are :
LA[0] = 1 
LA[1] = 3 
LA[2] = 5 
LA[3] = 7 
LA[4] = 8 
The array elements after deletion :
LA[0] = 1 
LA[1] = 3 
LA[2] = 7 
LA[3] = 8 

Search Operation

You can perform a search for an array element based on its value or its index.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop

Example

Following is the implementation of the above algorithm −
Manish Gupta
#include <stdio.h>

void main() {
   int LA[] = {1,3,5,7,8};
   int item = 5, n = 5;
   int i = 0, j = 0;
   
   printf("The original array elements are :\n");
 
   for(i = 0; i<n; i++) {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
    
   while( j < n){
      if( LA[j] == item ) {
         break;
      }
  
      j = j + 1;
   }
 
   printf("Found element %d at position %d\n", item, j+1);
}
When we compile and execute the above program, it produces the following result −

Output

The original array elements are :
LA[0] = 1 
LA[1] = 3 
LA[2] = 5 
LA[3] = 7 
LA[4] = 8 
Found element 5 at position 3

Update Operation

Update operation refers to updating an existing element from the array at a given index.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop

Example

Following is the implementation of the above algorithm −
 Live Demo
#include <stdio.h>

void main() {
   int LA[] = {1,3,5,7,8};
   int k = 3, n = 5, item = 10;
   int i, j;
   
   printf("The original array elements are :\n");
 
   for(i = 0; i<n; i++) {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
    
   LA[k-1] = item;

   printf("The array elements after updation :\n");
 
   for(i = 0; i<n; i++) {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
}
When we compile and execute the above program, it produces the following result −

Output

The original array elements are :
LA[0] = 1 
LA[1] = 3 
LA[2] = 5 
LA[3] = 7 
LA[4] = 8 
The array elements after updation :
LA[0] = 1 
LA[1] = 3 
LA[2] = 10 
LA[3] = 7 
LA[4] = 8 
Oldest

7 comments

Click here for comments
https://richestsoft.com/
admin
8 July 2021 at 04:04 ×

Great work. This is very important information for anyone who wants to start blogging. If you need any service related to website developers in India then contact us. We are very active in this field.

Reply
avatar