Pages

Write a C program to implement the following: 1. Take an int array of 100 elements. Populate it using random number generator built-in function. 2. User will be given the following choices: a) To insert a new element in the array. b) To delete an element in the array. c) To search an item in the array. d) To update an item in the array. e) To show the elements of the array. f) To quit from the program.

Monday 27 May 2013

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int main()
{
    int i, a[100], loc, item, size=99;
    char option;
    for(i=0; i<=size; i++)
    {
             a[i]= rand() % 20;
    }
    printf("Press a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
    option=getche();
    while(option!='f')
    {
    switch(option)
    {
           case 'a':
                if(size==100)
                {
                             printf("you can never add more elements because array in completlty filled.");
                }
                else
                {
                    printf("\nEnter location where you want to enter number: ");
                    scanf("%d", &loc);
                    printf("\nEnter number: ");
                    scanf("%d", &item);
                    for(i=size+1; i>loc; i--)
                                {
                                         a[i]=a[i-1];
                                }
                   a[loc]=item;
                   size++;
                   break;
                }
    case 'b':
         printf("\nEnter location where you want to delete number: ");
    scanf("%d", &loc);
    for(i=loc; i<=size; i++)
    {
             a[i]=a[i+1];
    }
    size--;
    break;
    case 'c':
         printf("\nEnter number which you want to search from array: ");
    scanf("%d", &item);
    for(i=0; i<=size; i++)
    {
             if(item==a[i])
             {
                         loc=i;
                         break;
             }
    }
    if(i>size)
    {
           printf("\nNumber is not found in array.");
    }
    else
    {
        printf("\n%d is found at location %d", item, loc);
    }
    break;
    case 'd':
         printf("\nEnter location where you want to update number: ");
    scanf("%d", &loc);
    printf("Enter number: ");
    scanf("%d", &item);
    a[loc]=item;
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
    case 'e':
    printf("\n");
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
         default:
         printf("\nYou have entered invalid entry.");
}
printf("\nPress a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
option=getche();
}
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *