0% found this document useful (0 votes)
20 views3 pages

Assignment 7

The document outlines a C program that allows users to perform various operations on an array, including searching for an element, inserting a new element at a specified index, deleting an element at the third position, and sorting the array in ascending order. It provides a user interface for inputting the array size and elements, as well as selecting the desired operation. The program includes error handling for invalid choices and displays the modified array after each operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Assignment 7

The document outlines a C program that allows users to perform various operations on an array, including searching for an element, inserting a new element at a specified index, deleting an element at the third position, and sorting the array in ascending order. It provides a user interface for inputting the array size and elements, as well as selecting the desired operation. The program includes error handling for invalid choices and displays the modified array after each operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 7

Q1. Write a program take an array as input and perform the following operation:
a) Search an element entered by user
b) Insert an element at given index
c) Delete an element at 3rd element of array
d) Sort elements in ascending order
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,ch;
printf("Enter the size of array : ");
scanf("%d",&n);
int arr[n];
printf("Enter the Element of array : \n");
for(int i=0; i<n; i++)
scanf("%d",&arr[i]);
printf("Search an element (press 1)");
printf("\nInsert an New element at given index (press 2)");
printf("\nDelete an element (press 3)");
printf("\nSort Element (press 4)");

printf("\nYour Choice : ");


scanf("%d",&ch);

if(ch==1) {
int temp,flag=0;
printf("\nSearch Element : ");
scanf("%d",&temp);
for(int i=0; i<n; i++) {
if(arr[i]== temp) {
printf("Element Found");
flag++;
break;
}
}
if(flag==0)
printf("Element not Found");
}
else if(ch==2) {
int temp,index;
printf("New Element : ");
scanf("%d",&temp);
printf("Index : ");
scanf("%d",&index);
for(int i=n; i>index; i--)
arr[i] = arr[i-1];
arr[index]=temp;

printf("New Array : ");


for(int i=0; i<n; i++)
printf(" %d",arr[i]);
printf("\n");
}
else if(ch==3) {
int pos;
printf("Position : ");
scanf("%d",&pos);
for(int i=pos; i<n; i++) {
arr[i-1]=arr[i];
}
arr[n-1]=NULL;

printf("New Array : ");


for(int i=0; i<n; i++)
printf(" %d",arr[i]);
printf("\n");
}
else if(ch == 4) {
for(int i=0; i<n; i++) {
for(int j=i; j<n; j++) {
if(arr[i]>arr[j]) {
int temp = arr[i];
arr[i]=arr[j];
arr[j] = temp;
}
}
}
printf("New Array : ");
for(int i=0; i<n; i++)
printf(" %d",arr[i]);
printf("\n");
}
else
printf("\nInvalid Choice !");

Output :

You might also like