1Write a program to find the mean and the median of the numbers stored in an array
#include <stdio.h>
#include <stdlib.h>
int cmpfunc(const void* a, const void* b)
return (*(int*)a - *(int*)b);
double findMean(int a[], int n)
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
return (double)sum / (double)n;
double findMedian(int a[], int n)
// First we sort the array
qsort(a, n, sizeof(int), cmpfunc);
// check for even case
if (n % 2 != 0)
return (double)a[n / 2];
return (double)(a[(n - 1) / 2] + a[n / 2]) / 2.0;
int main()
int a[] = { 1, 3, 4, 2, 7, 5, 8, 6 };
int N = sizeof(a) / sizeof(a[0]);
// Function call
printf("Mean = %f\n", findMean(a, N));
printf("Median = %f\n", findMedian(a, N));
return 0;
}
Q2 Write a program to insert one element in an array and delete an element from an array.
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int array[MAX_SIZE];
int n, pos, value;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
printf("Enter the position where the element should be inserted: ");
scanf("%d", &pos);
switch (pos) {
case 1:
// Insert element at the beginning of the array
// Shift elements to the right
for (int i = n - 1; i >= 0; i--) {
array[i + 1] = array[i];
// Prompt for the value of the new element
printf("Enter the value of the new element: ");
scanf("%d", &value);
// Assign the value to the specified position
array[0] = value;
// Increment the size of the array
n++;
printf("Element inserted successfully.\n");
break;
case 2:
// Insert element at the end of the array
// Prompt for the value of the new element
printf("Enter the value of the new element: ");
scanf("%d", &value);
// Assign the value to the specified position
array[n] = value;
// Increment the size of the array
n++;
printf("Element inserted successfully.\n");
break;
default:
if (pos >= 3 && pos <= n + 1) {
// Insert element at the specified position
// Shift elements to the right
for (int i = n - 1; i >= pos - 1; i--) {
array[i + 1] = array[i];
}
// Prompt for the value of the new element
printf("Enter the value of the new element: ");
scanf("%d", &value);
// Assign the value to the specified position
array[pos - 1] = value;
// Increment the size of the array
n++;
printf("Element inserted successfully.\n");
} else {
printf("Invalid position.\n");
break;
printf("Updated array: ");
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
printf("Enter the position of the element to be deleted: ");
scanf("%d", &pos);
switch (pos) {
case 1:
// Delete element from the beginning of the array
// Shift elements to the left
for (int i = 0; i < n - 1; i++) {
array[i] = array[i + 1];
// Decrement the size of the array
n--;
printf("Element deleted successfully.\n");
break;
case 2:
// Delete element from the end of the array
// Decrement the size of the array
n--;
printf("Element deleted successfully.\n");
break;
default:
if (pos >= 3 && pos <= n) {
// Delete element from the specified position
// Shift elements to the left
for (int i = pos - 1; i < n - 1; i++) {
array[i] = array[i + 1];
// Decrement the size of the array
n--;
printf("Element deleted successfully.\n");
} else {
printf("Invalid position.\n");
}
break;
printf("Updated array: ");
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
return 0;
}
Q3 write a program to search for a number in an array.
int main()
int arr[250], search, n, i;
printf("Please enter how many elements should be available in an array\n");
scanf("%d",&n);
printf("\nPlease enter %d numbers or integers one by one", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("\nPlease enter the number you want to search");
scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (arr[i] == search)
printf("\n%d is present at location %d\n", search, i+1);
break;
if (i == n)
printf("%d is not available in the array.\n", search);
return 0;
}
Q4 Write a program to sort an array
// C program to sort the array in an
// ascending order using selection sort
#include <stdio.h>
void swap(int* xp, int* yp)
int temp = *xp;
*xp = *yp;
*yp = temp;
// Function to perform Selection Sort
void selectionSort(int arr[], int n)
int i, j, min_idx;
// One by one move boundary of
// unsorted subarray
for (i = 0; i < n - 1; i++) {
// Find the minimum element in
// unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element
// with the first element
swap(&arr[min_idx], &arr[i]);
}
}
// Function to print an array
void printArray(int arr[], int size)
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
// Driver code
int main()
int arr[] = { 0, 23, 14, 12, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
selectionSort(arr, n);
printf("\nSorted array in Ascending order: \n");
printArray(arr, n);
return 0;
}
Q5 Write a program to merge two sorted arrays
#include <stdio.h>
int main()
int n1,n2,n3; //Array Size Declaration
int a[10000], b[10000], c[20000];
printf("Enter the size of first array: ");
scanf("%d",&n1);
printf("Enter the array elements: ");
for(int i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the size of second array: ");
scanf("%d",&n2);
printf("Enter the array elements: ");
for(int i = 0; i < n2; i++)
scanf("%d", &b[i]);
n3 = n1 + n2;
for(int i = 0; i < n1; i++)
c[i] = a[i];
for(int i = 0; i < n2; i++)
c[i + n1] = b[i];
printf("The merged array: ");
for(int i = 0; i < n3; i++)
printf("%d ", c[i]); //Print the merged array
printf("\nFinal array after sorting: ");
for(int i = 0; i < n3; i++){
int temp;
for(int j = i + 1; j < n3; j++) {
if(c[i] > c[j]) {
temp = c[i];
c[i] = c[j];
c[j] = temp;
for(int i = 0; i < n3 ; i++) //Print the sorted Array
printf(" %d ",c[i]);
return 0;
Q6 Write a program to store the marks obtained by 10 students in 5 courses in a two-dimensional
array
#include <stdio.h>
int main()
int arr[10][5];
int a;
for(int i=0;i<10;i++)
printf("marks obtained by %d student \n",i+1);
for(int j=0;j<5;j++)
printf("enter marks in %d subject :",j+1);
scanf("%d",&arr[i][j]);
}
}
Q7 Write a program to implement linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
};
int main()
struct node* head;
struct node* first;
struct node* second;
head=(struct node *)malloc(sizeof(struct node *));
first=(struct node *)malloc(sizeof(struct node *));
second=(struct node *)malloc(sizeof(struct node *));
head->data=15;
head->next=first;
first->data=14;
first->next=second;
second->data=13;
second->next=NULL;
struct node * ptr;
ptr=head;
while(ptr!=NULL)
printf("%d \n",ptr->data);
ptr=ptr->next;
Q8 Write a program to insert a node in a linked list and delete a node from a linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
};
int main()
struct node* head;
struct node* first;
struct node* second;
head=(struct node *)malloc(sizeof(struct node *));
first=(struct node *)malloc(sizeof(struct node *));
second=(struct node *)malloc(sizeof(struct node *));
head->data=15;
head->next=first;
first->data=14;
first->next=second;
second->data=13;
second->next=NULL;
struct node* p;
p=(struct node *)malloc(sizeof(struct node *));
int a;
printf("enter data \n");
scanf("%d",&a);
p->data=head->data;
p->next=head->next;
head->data=a;
head->next=p;
printf("after insertion \n");
struct node * ptr;
ptr=head;
while(ptr!=NULL)
printf("%d \n",ptr->data);
ptr=ptr->next;
struct node *d;
d=head;
head->data=p->data;
head->next=p->next;
printf("after deletion \n");
struct node * pt;
pt=head;
while(pt!=NULL)
printf("%d \n",pt->data);
pt=pt->next;
Q9 Write a program to print the element sofa linked list in reverse order
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
struct node* prev;
};
int main()
struct node* head;
struct node* first;
struct node* second;
head=(struct node *)malloc(sizeof(struct node *));
first=(struct node *)malloc(sizeof(struct node *));
second=(struct node *)malloc(sizeof(struct node *));
head->data=15;
head->next=first;
head->prev=NULL;
first->data=14;
first->next=second;
first->prev=head;
second->data=13;
second->next=NULL;
second->prev=first;
struct node* a;
a=head;
printf("before inverse \n");
while(a!=NULL)
printf("%d \n",a->data);
a=a->next;
struct node* d;
d=second;
printf("after inverse \n");
while(d!=NULL)
printf("%d \n",d->data);
d=d->prev;
Q10 Write a program to reverse a linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
struct node* prev;
};
int main()
struct node* head;
struct node* first;
struct node* second;
int arr[3];
head=(struct node *)malloc(sizeof(struct node *));
first=(struct node *)malloc(sizeof(struct node *));
second=(struct node *)malloc(sizeof(struct node *));
head->data=15;
head->next=first;
head->prev=NULL;
first->data=14;
first->next=second;
first->prev=head;
second->data=13;
second->next=NULL;
second->prev=first;
struct node* ptr;
ptr=head;
printf("before reversing \n");
for(int i=0;i<3;i++)
printf("%d \n",ptr->data);
arr[i]=ptr->data;
ptr=ptr->next;
ptr=head;
printf("after reversing the order \n");
for(int i=2;i>=0;i--)
ptr->data=arr[i];
printf("%d \n",ptr->data);
ptr=ptr->next;
Q 11Write a program to add two polynomials using linked lists
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coef;
int exp;
struct Node* next;
};
typedef struct Node Node;
void insert(Node** poly, int coef, int exp) {
Node* temp = (Node*) malloc(sizeof(Node));
temp->coef = coef;
temp->exp = exp;
temp->next = NULL;
if (*poly == NULL) {
*poly = temp;
return;
Node* current = *poly;
while (current->next != NULL) {
current = current->next;
current->next = temp;
void print(Node* poly) {
if (poly == NULL) {
printf("0\n");
return;
Node* current = poly;
while (current != NULL) {
printf("%dx^%d", current->coef, current->exp);
if (current->next != NULL) {
printf(" + ");
current = current->next;
printf("\n");
}
Node* add(Node* poly1, Node* poly2) {
Node* result = NULL;
while (poly1 != NULL && poly2 != NULL) {
if (poly1->exp == poly2->exp) {
insert(&result, poly1->coef + poly2->coef, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->exp > poly2->exp) {
insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
} else {
insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next;
while (poly1 != NULL) {
insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
while (poly2 != NULL) {
insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next;
return result;
int main() {
Node* poly1 = NULL;
insert(&poly1, 5, 4);
insert(&poly1, 3, 2);
insert(&poly1, 1, 0);
Node* poly2 = NULL;
insert(&poly2, 4, 4);
insert(&poly2, 2, 2);
insert(&poly2, 1, 1);
printf("First polynomial: ");
print(poly1);
printf("Second polynomial: ");
print(poly2);
Node* result = add(poly1, poly2);
printf("Result: ");
print(result);
return 0;
Q12 Write a program to implement a doubly-LinkedList
Done above in Q10
Q13 Write a program to implement a stack using an array