Write a menu driven c program to create an integer array and populate it with some data. Then perform the following operations with user choice: a) Insert an element at beginning, at end, at any position of the array. b) Delete an element at beginning, from end, from a given position of the array. c) Replace the element of a given position with another number. d) Search if an element exist in the list (using linear search). e) Display the array elements

Recommended Answers

All 9 Replies

Maybe it needs an update to include "don't feed the students answers."

That's not against our rules.

commented: I know that. If folk are unclear about what/why, then why not hand them fish? +16

I know that. If folk are unclear about what/why, then why not hand them fish?

You can choose to hand people or not hand people whatever you want. That's the point. DaniWeb isn't meant to be in the business of policing its member's actions.

So when I show an example of an attached PHP file, and the OP is having trouble posting code that's a keep it organized problem? Some days I can't keep up!

How is you posting an unrelated PHP file (regarding a DaniWeb bug from a few weeks back) helping him figure out how to use the editor to post his own code?

Start a thread in the Meta forum about the DaniWeb bug.

  1. The bug was transitory.
  2. The example is to show what Daniweb does with attached code. This is On Topic since OP was having troubles sharing code.
  3. Said code was PHP. Why was it this file? Because it was the file I had on hand and top of the list when I went to find a file to show as example.
  4. Now who's pushing the line around on what's related/unrelated/keep it organized?

Why are you discussing this in a thread titled "Write a menu driven c program..."? Shouldn't this be discussed in the mods forum?

commented: Hi Jim! +0
commented: My point exactly +34

To help MD Saiful:

The link that rproffitt provided offers the following code:

#include <stdio.h>
#include <stdlib.h>

int a[10], pos, elem;

int n = 0;

void create();

void display();

void insert();

void del();

void main()

{

    int choice;

    while (1)

    {

        printf("\n\n~~~~MENU~~~~");

        printf("\n=>1. Create an array of N integers");

        printf("\n=>2. Display of array elements");

        printf("\n=>3. Insert ELEM at a given POS");

        printf("\n=>4. Delete an element at a given POS");

        printf("\n=>5. Exit");

        printf("\nEnter your choice: ");

        scanf("%d", &choice);

        switch (choice)

        {

            case 1:
                create();

                break;

            case 2:
                display();

                break;

            case 3:
                insert();

                break;

            case 4:
                del();

                break;

            case 5:
                exit(1);

                break;

            default:
                printf("\nPlease enter a valid choice:");
        }
    }
}

void create()

{

    int i;

    printf("\nEnter the number of elements: ");

    scanf("%d", &n);

    printf("\nEnter the elements: ");

    for (i = 0; i < n; i++)

    {

        scanf("%d", &a[i]);
    }
}

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 == 5)

    {

        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;

    display();

}

void del()

{

    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;

    display();

}

Here's a brief article on geeksforgeeks that explains how it works:

https://www.geeksforgeeks.org/how-to-insert-an-element-at-a-specific-position-in-an-array-in-c/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.