Computer Science, asked by jugnuroquiya, 1 month ago

Write a program to insert an element in the given array (1D) at the beginning. After

insertion, identify the largest element from the array, and delete it. After deleting the

largest element, display the final array elements on the screen.​

Answers

Answered by amikkr
0

Here is a C program that inserts an element at the beginning of a 1D array, identifies the largest element in the array, and deletes it:

#include <stdio.h>

// function prototype for inserting an element at the beginning of the array

void insert_beginning(int* arr, int size, int element);

// function prototype for deleting the largest element from the array

void delete_largest(int* arr, int size);

int main() {

 int size = 5; // size of the array

 int element = 10; // element to insert at the beginning of the array

 // initialize the array with some values

 int arr[5] = {1, 2, 3, 4, 5};

 // insert the element at the beginning of the array

 insert_beginning(arr, size, element);

 // delete the largest element from the array

 delete_largest(arr, size);

 // print the final array elements

 printf("Final array: ");

 for (int i = 0; i < size; i++) {

   printf("%d ", arr[i]);

 }

 printf("\n");

 return 0;

}

// function to insert an element at the beginning of the array

void insert_beginning(int* arr, int size, int element) {

 // shift all elements to the right by 1

 for (int i = size; i > 0; i--) {

   arr[i] = arr[i-1];

 }

 // insert the element at the beginning of the array

 arr[0] = element;

}

// function to delete the largest element from the array

void delete_largest(int* arr, int size) {

 // find the index of the largest element in the array

 int largest_index = 0;

 for (int i = 1; i < size; i++) {

   if (arr[i] > arr[largest_index]) {

     largest_index = i;

   }

 }

 // shift all elements to the left by 1, starting from the index of the largest element

 for (int i = largest_index; i < size-1; i++) {

   arr[i] = arr[i+1];

 }

}

This code should output "Final array: 10 2 3 4 5" when run.

#SPJ1

Similar questions