Showing posts with label One dimensional array. Show all posts
Showing posts with label One dimensional array. Show all posts

Tuesday, 2 August 2016

Arrays Linear Search

Search for key Element in an Array using Linear Search

Download the Program

#include<stdio.h>
void main()
{
    int i, j, n, a[100], key;
    printf("\nEnter value of n:");
    scanf("%d", &n);
    printf("\nEnter array elements:");
    for(i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("\nEnter key:");
    scanf("%d", &key);

    for(i=0; i<n; i++)
    {
            if(a[i] == key)
                break;
    }
    if(i<n)
        printf("\nKey found at position %d", i+1);
    else
        printf("\nNot found");
}

Output:
Enter value of n: 6
Enter array elements: 1  3  2  4  5  1
Enter key: 3
Key found at position 2

Sunday, 24 July 2016

Program to Delete an Element from an Array at Valid Position

Write program to Delete an Element from an Array at Valid Position

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int a[MAX], pos, elem;
int n = 0;
void create();
void display();
void delete();

void main()
{
                int i;
                printf("\nEnter the number of elements: ");
                scanf("%d", &n);
                if(n!=0)
                                printf("\nEnter the elements: ");
                                for(i=0; i<n; i++)
                                {
                                                scanf("%d", &a[i]);
                                }
                delete();
                display();
}

void display()
{
                int i;
                if(n == 0)
                {
                                printf("\nNo elements to display");
                                return;
                }
                printf("\nArray elements are: ");
                for(i=0; i<n;i++)
                                printf("%d\t ", a[i]);
}

void delete()
{
                int i;
                if(n == 0)
                {
                                printf("\nArray is empty and no elements to delete");
                                return;
                }

                do
                {
                                printf("\nEnter a valid position from where element to be deleted:    ");
                                scanf("%d", &pos);
                }while(pos>=n);
               
                elem = a[pos];
                printf("\nDeleted element is : %d \n", elem);
                for( i = pos; i< n-1; i++)
                {
                                a[i] = a[i+1];
                }
                n = n-1;
}

Output:
Case 1:
Enter the number of elements: 0
Array is empty and no elements to delete
No elements to display

Case 2:
Enter the number of elements: 3
Enter the elements: 11             12           13
Enter a valid position from where element to be deleted:    6
Enter a valid position from where element to be deleted:    1
Deleted element is : 12
Array elements are: 11    13

Program to Insert an Element to an Array at Valid Position

Write a program to Insert an Element to an Array at Valid Position


#include<stdio.h>
#include<stdlib.h>
#define MAX 5

int a[MAX], pos, elem;
int n = 0;
void create();
void display();
void insert();

void main()
{
                int i;
                printf("\nEnter the number of elements: ");
                scanf("%d", &n);
                if(n != 0)
                                printf("\nEnter the elements: ");
                                for(i=0; i<n; i++)
                                {
                                                scanf("%d", &a[i]);
                                }
                insert();
                display();
}
void display()
{
                int i;
                if(n == 0)
                {
                                printf("\nNo elements to display");
                                return;
                }
                printf("\nArray elements are: ");
                for(i=0; i<n; i++)
                                printf("%d\t ", a[i]);
}
void insert()
{
                int i;
                if(n == MAX)
                {
                                printf("\nArray is full. Insertion is not possible");
                                return;
                }
                do
                {
                                printf("\nEnter a valid position where element to be inserted:    ");
                                scanf("%d", &pos);
                }while(pos>n);

                printf("\nEnter the value to be inserted:   ");
                scanf("%d", &elem);
                for(i =n-1; i>=pos ; i--)
                {
                                a[i+1] = a[i];
                }
                a[pos] = elem;
                n = n+1;
    }

Output:

Case 1:
Enter the number of elements: 3
Enter the elements: 11                  22           33
Enter a valid position where element to be inserted:    5
Enter a valid position where element to be inserted:    1
Enter the value to be inserted:   44
Array elements are: 11   44      22      33


Case 2:
Enter the number of elements: 5
Enter the elements: 11                  22           33           44           55
Array is full. Insertion is not possible
Array elements are: 11   22      33      44      55

Also Check,



Saturday, 23 July 2016

Arrays

Program to Read and Display elements of One dimensional array

Write a program to Read and Display elements of One Dimensional Array

#include<stdio.h>
void main()
{
        int n, i, a[100];
        printf("\nEnter the number of elements:");
        scanf("%d", &n);
        printf("\nEnter the elements:");
        for(i=0; i<n; i++)
        {
                        scanf("%d", &a[i]);
        }
        printf("\nArray elements are: ");
        for(i=0; i<n; i++)
        {
                        printf("%d ", a[i]);
        }
}
Output:
Enter the number of elements:   5
Enter the elements:  1  2 3 4 5