Professional Documents
Culture Documents
SEMESTER-2,2022-23
SUBMITTED BY->
GAURAV SAIN
Roll no:-2021UCS1714
Section:-CSE-2
Q1.Write a program to nd the mean and the median of the numbers stored in an
array.
#include <stdio.h>
int main(void){
int n;
int total=0;
int arr[n];
//mean
printf("Mean is:%d\n",total/n);
//median
if(n%2==0){
printf("Median is:%d\n",arr[n/2]);
}else{
printf("Median is:%d\n",arr[n/2] +arr[(n-1)/2]);
}
return 0;
}
fi
#include "stdio.h"
#include "stdlib.h"
/* Array implementation of stacks in C*/
struct stack{
int top;
int capacity;
int *arr;
};
return st;
}
}else{
st->top++;
st->arr[st->top]=val;
}
}
int main(void){
struct stack *st;
int size;
printf("Please Enter the size of stack\n");
scanf("%d",&size);
st=newStack(size);
printf("New Stack has been created successfully\n");
push(st,10);
display(st);
push(st,50);
display(st);
push(st,100);
display(st);
push(st,500);
display(st);
push(st,1000);
display(st);
push(st,5000);
display(st);
printf("\n");
===========================OUTPUT=================================
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct queue{
int front;
int rear;
int capacity;
int *arr;
};
return q;
}
// }
q->rear++;
q->arr[q->rear]=data;
if(q->front==-1){
q->front=q->rear;
}
}else{
printf("Queue is Full\n");
}
}
if(isEmpty(q)){
printf("Queue is empty,Add first\n");
return -1;
}else{
int data=q->arr[q->front];
if(q->front==q->rear){
q->front=q->rear=-1;
}else{
q->front++;
}
// q->rear--;
return data;
}
return 0;
}
int main(void){
dequeue(Q);
display(Q);
printf("%dafter\n",size(Q));
dequeue(Q);
display(Q);
printf("%dafter\n",size(Q));
dequeue(Q);
display(Q);
printf(“%dafter\n”,size(Q));}
===============================OUTPUT=========================
#include "stdio.h"
#include <stdlib.h>
struct circular{
int front;
int rear;
int *array;
int capacity;
};
}
}
while(pt!=q->rear){
printf("%d\t", q->array[pt]);
pt=(pt+1)%q->capacity;
}
//for printing the rear element
printf("%d",q->array[pt]);
printf("\n");
}
int main(void){
struct circular *q=construct(6);
enqueue(q,1);
display(q);
enqueue(q,2);
display(q);
enqueue(q,3);
display(q);
enqueue(q,4);
display(q);
enqueue(q,5);
display(q);
enqueue(q,5);
display(q);
dequeue(q);
display(q);
dequeue(q);
display(q);
printf("size is:%d",size(q)) ;
==============================OUTPUT===========================
#include "stdio.h"
int num;
int sz=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<sz;i++){
if(arr[i]==num){
printf("Element is found!\n");
return 0;
}
}
============================OUTPUT=============================
#include<stdio.h>
arr[n] = key;
return (n + 1);
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int main()
{
int arr[20] = {12, 16, 20, 40, 50, 70};
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
// Inserting key
n = insertSorted(arr, n, key, capacity);
n = deleteElement(arr, n, key);
return 0;
===============================OUTPUT============================
#include "stdio.h"
#define students 10
#define subjects 5
int main(void){
int marks[students][subjects];
// will store marks of 10 students in five subjects
for(int i=0;i<students;i++){
for(int j=0;j<subjects;j++){
printf("Enter the marks of %dth student in %dth
subject!\n",i+1,j+1);
scanf("%d",&marks[i][j]);
}
}
//displaying now
for(int i=0;i<students;i++){
for(int j=0;j<subjects;j++){
printf("Marks of %dth student in %dth subject are:
%d\n",i+1,j+1,marks[i][j]);
}
}
return 0;
==============================OUTPUT=============================
#include "stdio.h"
#include "stdlib.h"
return nn;
}
Node *nn=createNode(data,priority);
if((*head)->priority<priority){
nn->next=*head;
// updating head pointer
(*head)=nn;
}
else{
//traversing and finding position to insert
while(temp->next!=NULL && temp->priority<priority ){
temp=temp->next;
}
nn->next=temp->next;
temp->next=nn;
}
}
free(temp);
return (*head)->data;
}
int main(void){
//adding values to priority queue
Node *head=createNode(4,1);
push(&head,2,2);
push(&head,7,5);
push(&head,4,3);
push(&head,5,4);
display(&head);
int val=pop(&head);
printf("Deleted:%d\n",val);
return 0;
}
================================OUTPUT============================
#include <stdio.h>
#include <stdlib.h>
// structure of a Binary tree node
typedef struct node{
int data;
struct node *left;
struct node *right;
}TreeNode;
return nn;
}
printf("%d ",node->data);
preOrder(node->left);
preOrder(node->right);
}
inOrder(node->left);
printf("%d ",node->data);
inOrder(node->right);
}
postOrder(node->left);
postOrder(node->right);
printf("%d ",node->data);
}
int main(void){
TreeNode *root=createNode(10);
TreeNode *nn1=createNode(20);
TreeNode *nn2=createNode(21);
root->left=nn1;
root->right=nn2;
TreeNode *nn3=createNode(45);
nn1->left=nn3;
TreeNode *nn4=createNode(63);
nn1->left->right=nn4;
TreeNode *nn5=createNode(34);
nn2->left=nn5;
nn2->right=createNode(27);
printf("%d\n",root->right->left->data);
nn2->right->left=createNode(18);
==============================OUTPUT===========================
#include <stdio.h>
#include <stdlib.h>
// doubly linked list implementation
struct Node{
int data;
struct Node *prev;
struct Node *next;
};
nn->prev=NULL;
if(*head_ref!=NULL){
(*head_ref)->prev=nn;
}
(*head_ref)=nn;
return;
temp->next=nn;
nn->next=NULL;
int main(void){
struct Node *head=NULL;
atFirst(&head,10);
atFirst(&head,23);
display(head);
atLast(&head,13);
display(head);
}
===============================OUTPUT============================
#include <stdio.h>
#include <stdlib.h>
struct Node{
/* data */
int data;
struct Node *next;
};
if(nn==NULL){
printf("Memory nOT Allocated\n ");
}else{
// new node mei data dala
nn->data=data;
//new node k next mei address
nn->next=*head_ref;
if(*head_ref==NULL){
*head_ref=nn;
return;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=nn;
nn->next=NULL;
}
}
if(nn==NULL){
printf("Memory not allocated\n");
return;
}
else if(pos==1){
atFirst(head_ref,data);
}
else if(pos==size(*head_ref)){
atLast(head_ref,data);
}
else{
nn->data=data;
while(pos!=1){
pos--;
temp=temp->next;
}
// gest
nn->next=temp->next;
temp->next=nn;
return;
}
while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}
return size;
}
int main(void){
struct Node *head=NULL;
struct Node *two=NULL;
struct Node *three=NULL;
struct Node *four=NULL;
head->data=1;
head->next=two;
two->data=2;
two->next=three;
three->data=3;
three->next=four;
four->data=4;
four->next=NULL;
atAny(&head,1000,3);
atAny(&head,100,3);
atAny(&head,10,3);
printf("Inserted Successfully! now displaying");
display(head);
return 0;
}
================================OUTPUT===========================
struct Node{
/* data */
int data;
struct Node *next;
};
int main(void){
struct Node *head=NULL;
struct Node *two=NULL;
struct Node *three=NULL;
struct Node *four=NULL;
head->data=1;
head->next=two;
two->data=2;
two->next=three;
three->data=3;
three->next=four;
four->data=4;
four->next=NULL;
printf("%d\n",head->data);
printf("%d\n",two->data);
printf("%d\n",three->data);
printf("%d\n",four->data);
return 0;
}
==============================OUTPUT=============================
int main(void) {
int ch;
do {
my_poly * poly1, * poly2, * poly3;
tmp_node->next = NULL;
printf("\nContinue adding more terms to the polynomial list?(Y = 1/N = 0): ");
scanf("%d", &flag);
printf("\nFLAG: %c\n", flag);
if(flag) {
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
}
} while (flag);
}
while(poly1 || poly2)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
if(poly1) {
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2) {
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->next;
}
}
printf("\nAddition Complete");
}
===============================OUTPUT================================
int main(void){
int arr[]={12,43,17,88,34,75,43};
int len=sizeof(arr)/sizeof(arr[0]);
printf("Original Array\n");
for(int i=0;i<len;i++){
printf("%d\t",arr[i]);
}
printf("\t\n");
// bubble sort
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++){
if(arr[j+1]<arr[j]){
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
// sorted array
printf("Sorted Array\n");
for(int i=0;i<len;i++){
printf("%d\t",arr[i]);
}
printf("\n");
===============================OUTPUT=============================
#include "stdio.h"
int main(void){
int nums1={1,2,3,0,0,0};
int nums2={2,5,6};
int
int i=0;
int j=0;
int k=0;
while(i<m){
arr[k]=nums1[i];
k++;
i++;
}
while(j<n){
arr[k]=nums2[j];
k++;
j++;
}
for(int pt=0;pt<nums1.length;pt++){
nums1[pt]=arr[pt];
}
}
================================OUTPUT===========================
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
tf
tf
tf
tf
tf
tf
tf
tf
tf
tf
tf
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
prin ("Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
prin ("\n Queue size : %d", count);
}
tf
tf
rear = temp;
}
count++;
}
void display()
{
front1 = front;
void deq()
{
front1 = front;
if (front1 == NULL)
{
prin ("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
tf
tf
tf
tf
front1 = front1->ptr;
prin ("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
prin ("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
prin ("\n Queue empty");
else
prin ("Queue not empty");
}
tf
tf
tf
tf
=========================OUTPUT==========================
while(curr!=NULL){
struct Node *frwd=curr->next;
curr->next=prev;
prev=curr;
curr=frwd;
}
*head=prev;
}
int main(void){
struct Node *head=NULL;
struct Node *two=NULL;
struct Node *three=NULL;
struct Node *four=NULL;
head->data=1;
head->next=two;
two->data=2;
two->next=three;
three->data=3;
three->next=four;
four->data=4;
four->next=NULL;
display(head);
reverse(&head);
display(head);
return 0;
}
===============================OUTPUT=============================
THANK YOU!!