0% found this document useful (0 votes)
63 views60 pages

Program Using Function of Array

The document discusses program using functions and arrays in C. It defines a function calculateSum() that takes an array as a parameter and returns the sum of all elements. The main() function passes an array to calculateSum() and prints the result.

Uploaded by

Jagan
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)
63 views60 pages

Program Using Function of Array

The document discusses program using functions and arrays in C. It defines a function calculateSum() that takes an array as a parameter and returns the sum of all elements. The main() function passes an array to calculateSum() and prints the result.

Uploaded by

Jagan
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
You are on page 1/ 60

Program using function of array

Program:

#include <stdio.h>

float calculateSum(float num[]);

int main() {

float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};

// num array is passed to calculateSum()

result = calculateSum(num);

printf("Result = %.2f", result);

return 0;

float calculateSum(float num[]) {

float sum = 0.0;

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

sum += num[i];

return sum;

}
OUTPUT:

Result=162.50
C PROGRAMMING USING POINTERS AND STRUCTURES

PROGRAM:

#include <stdio.h>

void geeks()

int var = 20;

int* ptr;

ptr = &var;

printf("Value at ptr = %p \n", ptr);

printf("Value at var = %d \n", var);

printf("Value at *ptr = %d \n", *ptr);

int main()

geeks();

return 0;
}
OUTPUT

Value at ptr = 0x7ffd15b5deec

Value at var = 20

Value at *ptr = 20
STRUCTURES

POINTER:

#include <stdio.h>

struct Point {

int x, y, z;

};

int main()

struct Point p1 = { .y = 0, .z = 1, .x = 2 };

struct Point p2 = { .x = 20 };

printf("x = %d, y = %d, z = %d\n", p1.x, p1.y, p1.z);

printf("x = %d", p2.x);

return 0;

}
OUTPUT:

x = 2, y = 0, z = 1

x = 20
IMPLEMENTATION OF BINARY TREE

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *prev;

struct node *next;

};

typedef struct node list;

list *head, *tail, *current, *newn;

void inorder(struct node *t)

if(t != NULL)

inorder(t->prev);

printf("%d->",t->data);

inorder(t->next);

struct node * insert(int key, struct node *t)

if(t == NULL)

{
t = (list*)malloc(sizeof(list));

t->data = key;

t->prev = NULL;

t->next = NULL;

else if(t->prev == NULL)

t->prev = insert(key,t->prev);

else if(t->next == NULL)

t->next = insert(key,t->next);

return(t);

int main()

int x=1, y, z=1;

current = (list*)malloc(sizeof(list));

printf("Enter data:");

scanf("%d",&current->data);

current->next = NULL;

current->prev = NULL;

head = current;

while(z == 1)

printf("Enter data:");

scanf("%d",&y);

current = insert(y,current);
printf("want to insert more:");

scanf("%d",&z);

printf("\nInorder Traversal:");

newn = head;

inorder(newn);

}
OUTPUT:

Enter data:300

Enter data:90

Want to insert more:90

Inorder traversal:90->300->
ARRAY IMPLEMENTATION OF STACK AND QUEUE

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

#define SIZE 4

int top = -1, inp_array[SIZE];

void push();

void pop();

void show();

int main()

int choice;

while (1)

printf("\nPerform operations on the stack:");

printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");

printf("\n\nEnter the choice: ");

scanf("%d", &choice);

switch (choice)

{
case 1:

push();

break;

case 2:

pop();

break;

case 3:

show();

break;

case 4:

exit(0);

default:

printf("\nInvalid choice!!");

void push()

int x;

if (top == SIZE - 1)

printf("\nOverflow!!");

else

printf("\nEnter the element to be added onto the stack: ");


scanf("%d", &x);

top = top + 1;

inp_array[top] = x;

void pop()

if (top == -1)

printf("\nUnderflow!!");

else

printf("\nPopped element: %d", inp_array[top]);

top = top - 1;

void show()

if (top == -1)

printf("\nUnderflow!!");

else

printf("\nElements present in the stack: \n");

for (int i = top; i >= 0; --i)


printf("%d\n", inp_array[i]);

}
OUTPUT:

Perform operations on the stack:

1.Push the element

2.Pop the element

3.Show

4.End

Enter the choice: 1

Enter the element to be added onto the stack: 3

Perform operations on the stack:

1.Push the element

2.Pop the element

3.Show

4.End

Enter the choice: 1

Enter the element to be added onto the stack: 5

Perform operations on the stack:

1.Push the element

2.Pop the element

3.Show

4.End

Enter the choice: 3

Elements present in the stack: 1

3
QUEUE

PROGRAM:

#include <stdio.h>

#define MAX 50

void insert();

void delete();

void display();

int queue_array[MAX];

int rear = - 1;

int front = - 1;

main()

int choice;

while (1)

printf("1.Insert element to queue \n");

printf("2.Delete element from queue \n");

printf("3.Display all elements of queue \n");

printf("4.Quit \n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch (choice)

case 1:

insert();

break;
case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice \n");

} /* End of switch */

} /* End of while */

} /* End of main() */

void insert()

int add_item;

if (rear == MAX - 1)

printf("Queue Overflow \n");

else

if (front == - 1)

/*If queue is initially empty */

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

}
} /* End of insert() */

void delete()

if (front == - 1 || front > rear)

printf("Queue Underflow \n");

return ;

else

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

} /* End of delete() */

void display()

int i;

if (front == - 1)

printf("Queue is empty \n");

else

printf("Queue is : \n");

for (i = front; i <= rear; i++)

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

printf("\n");

} /* End of display() */
OUTPUT:

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 10

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 15

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 20

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 30

1.Insert element to queue

2.Delete element from queue


3.Display all elements of queue

4.Quit

Enter your choice : 2

Element deleted from queue is : 10

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 3

Queue is :

15 20 30

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 4


IMPLEMENTATION OF SEARCHING TECHNIQUES IN C

PROGRAM:

#include<stdio.h>

int main (){

int a[50], n, i, key, flag = 0;

printf("enter the no: of elements");

scanf ("%d",&n);

printf("enter the elements:

");

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

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

printf("enter a key element:

");

scanf ("%d", &key);

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

if (a[i] == key){

flag = 1;

break;

if (flag == 1)

printf("search is successful:");

Else:

printf("search is unsuccessfull:");

return 0;

}
OUTPUT:

enter the no: of elements5

enter the elements:12

45

13

67

78

enter a key element:67

search is successful:
IMPLEMENTATION OF HASHING ANY TWO COLLISION TECHNIQUES

PROGRAM:

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index] == NULL)

h[index]=key;

break;

}
if(i == TABLE_SIZE)

printf("\nelement cannot be inserted\n");

void search()

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

void display()

int i;
printf("\nelements in the hash table are \n");

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

printf("\nat index %d \t value = %d",i,h[i]);

main()

int opt,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

}
OUTPUT:

Output

Press 1. Insert 2. Display 3. Search 4.Exit

enter a value to insert into hash table

12

Press 1. Insert 2. Display 3. Search 4.Exit

enter a value to insert into hash table

13

Press 1. Insert 2. Display 3. Search 4.Exit

enter a value to insert into hash table

22

Press 1. Insert 2. Display 3. Search 4.Exit

elements in the hash table are

at index 0 value = 0
at index 1 value = 0

at index 2 value = 12

at index 3 value = 13

at index 4 value = 22

at index 5 value = 0

at index 6 value = 0

at index 7 value = 0

at index 8 value = 0

at index 9 value = 0

Press 1. Insert 2. Display 3. Search 4.Exit

enter search element

12

value is found at index 2

Press 1. Insert 2. Display 3. Search 4.Exit

23

enter search element

23

value is not found

Press 1. Insert 2. Display 3. Search 4.Exit

4
ARRAY IMPLEMENTATION OF LIST ADT

PROGRAM:

#include<stdio.h>

#include<alloc.h>

#include<conio.h>

struct list

int capacity;

int size;

int *array;

};

typedef struct list *ptrToNode;

typedef ptrToNode LIST;

typedef int POSITION;

int Isempty(LIST L)

return L->size==0;

void MakeEmpty(LIST L)

if(Isempty(L))

printf("\n LIST is already Empty");

else

L->size=0;
printf("\n Now List becomes Empty");

LIST Createlist(int max)

LIST L;

L=(struct list*)malloc(sizeof(struct list));

if(L==NULL)

printf("\n Fatal Error");

else

L->capacity=max;

L->array=(int*)malloc(sizeof(int)*max);

if(L->array==NULL)

printf("\n Fatal Error");

else

L->size=0;

printf("\n List is Created successfully");

return L;

int Isfull(LIST L)

return L->size==L->capacity;

}
void Insert(int x,LIST L,POSITION P)

int i;

if(Isfull(L))

printf("\n List is Full");

else

for(i=L->size-1;i>=P;i--)

L->array[i+1]=L->array[i];

L->size++;

L->array[P]=x;

POSITION Findprevious(int x,LIST L)

POSITION P;

P=-1;

while(P!=L->size&&L->array[P+1]!=x)

P++;

return P;

POSITION Find(int x,LIST L)

POSITION P;
P=0;

while(P!=L->size&&L->array[P]!=x)

P++;

return P;

void Delete(int x,LIST L)

int i;

POSITION P;

P=Find(x,L);

if(P==L->size)

printf("\n Element not found in the list");

else

for(i=P;i<L->size;i++)

L->array[i]=L->array[i+1];

L->size--;

LIST Deletelist(LIST L)

MakeEmpty(L);

free(L);

L=NULL;
return L;

void Display(LIST L)

int i;

for(i=0;i<L->size;i++)

printf("\n %d",L->array[i]);

“Larray.c” File:

#include"Larray.h"

#include<stdlib.h>

void main()

LIST L=NULL;

POSITION P;

int a,choice,ch,element;

clrscr();

printf("\n\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.MakeEmpty\n6.Find\n7.IsEmpty\n8.IsFull\
n9.Deletelist\n10.Exit\n");

A:

printf("\n Enter Ur Option:\t");

scanf("%d",&choice);

switch(choice)

case 1:

if(L==NULL)
L=Createlist(5);

else

printf("\nList is already created");

break;

case 2:

if(L==NULL)

printf("\nList is not yet created");

else

printf("\nEnter the Element to insert:\t");

scanf("%d",&element);

if(L->size==0)

Insert(element,L,0);

else

printf("\n where u want to insert?\t1:Front\t2:Back\t3:middle\t::: ");

scanf("%d",&ch);

if(ch==1)

Insert(element,L,0);

else

if(ch==2)

Insert(element,L,L->size);

else

if(ch==3)

printf("\nWhere you want to insert:\t");

scanf("%d",&a);

P=Find(a,L);
if(P<L->size)

Insert(element,L,P);

else

printf("\nElement is not in the list");

else

printf("\n Ur choice is not available");

break;

case 3:

if(L==NULL)

printf("\nList is not yet created");

if(Isempty(L))

printf("\nList is empty");

else

printf("\nEnter the element to delete:\t");

scanf("%d",&a);

Delete(a,L);

break;

case 4:

if(L==NULL)

printf("\nList is not yet created");

else

if(Isempty(L))

printf("\nList is empty");

else
{

printf("\nElements present in the list are:");

Display(L);

break;

case 5:

if(L==NULL)

printf("\n List is not yet created ");

else

MakeEmpty(L);

break;

case 6:

if(L==NULL)

printf("\n Not yet created");

else

if(Isempty(L))

printf("\n List is empty");

else

printf("\n which element is to find:\t");

scanf("%d",&a);

P=Find(a,L);

printf("\n Element is at %d\t[0 to 4 means present]\t[5 means not present]",P);

break;

case 7:

if(L==NULL)

printf("\n Not yet created");


else

if(Isempty(L))

printf("\n List is empty");

else

printf("\n List is not empty");

break;

case 8:

if(L==NULL)

printf("\n Not yet created");

else

if(Isfull(L))

printf("\n List is FULL");

else

printf("\n List is not FULL");

break;

case 9:

if(L==NULL)

printf("\n Not yet created");

else

L=Deletelist(L);

printf("\n List is Deleted");

break;

case 10:

exit (0);

break;

default:

printf("\n\n **WRONG ENTRY**");


break;

}
OUTPUT:

OUTPUT:

1.Create

2.Insert

3.Delete

4.Display

5.MakeEmpty

6.Find

7.IsEmpty

8.IsFull

9.Deletelist

10.Exit

Enter Ur Option: 1

List is created successfully


MERGE SORT

PROGRAM:

#include <stdio.h>

#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };

int b[10];

void merging(int low, int mid, int high) {

int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {

if(a[l1] <= a[l2])

b[i] = a[l1++];

else

b[i] = a[l2++];

while(l1 <= mid)

b[i++] = a[l1++];

while(l2 <= high)

b[i++] = a[l2++];

for(i = low; i <= high; i++)

a[i] = b[i];

}
void sort(int low, int high) {

int mid;

if(low < high) {

mid = (low + high) / 2;

sort(low, mid);

sort(mid+1, high);

merging(low, mid, high);

} else {

return;

int main() {

int i;

printf("List before sorting\n");

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

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

sort(0, max);

printf("\nList after sorting\n");

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

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

}
OUTPUT:

Output

List before sorting

10 14 19 26 27 31 33 35 42 44 0

List after sorting

0 10 14 19 26 27 31 33 35 42 44
IMPLEMENTATION OF BINARY SEARCH TREE

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

struct node {

int key;

struct node *left, *right;

};

// Create a node

struct node *newNode(int item) {

struct node *temp = (struct node *)malloc(sizeof(struct node));

temp->key = item;

temp->left = temp->right = NULL;

return temp;

// Inorder Traversal

void inorder(struct node *root) {

if (root != NULL) {

// Traverse left

inorder(root->left);

// Traverse root

printf("%d -> ", root->key);


// Traverse right

inorder(root->right);

// Insert a node

struct node *insert(struct node *node, int key) {

// Return a new node if the tree is empty

if (node == NULL) return newNode(key);

// Traverse to the right place and insert the node

if (key < node->key)

node->left = insert(node->left, key);

else

node->right = insert(node->right, key);

return node;

// Find the inorder successor

struct node *minValueNode(struct node *node) {

struct node *current = node;

// Find the leftmost leaf

while (current && current->left != NULL)

current = current->left;

return current;

}
// Deleting a node

struct node *deleteNode(struct node *root, int key) {

// Return if the tree is empty

if (root == NULL) return root;

// Find the node to be deleted

if (key < root->key)

root->left = deleteNode(root->left, key);

else if (key > root->key)

root->right = deleteNode(root->right, key);

else {

// If the node is with only one child or no child

if (root->left == NULL) {

struct node *temp = root->right;

free(root);

return temp;

} else if (root->right == NULL) {

struct node *temp = root->left;

free(root);

return temp;

// If the node has two children

struct node *temp = minValueNode(root->right);

// Place the inorder successor in position of the node to be deleted

root->key = temp->key;
// Delete the inorder successor

root->right = deleteNode(root->right, temp->key);

return root;

// Driver code

int main() {

struct node *root = NULL;

root = insert(root, 8);

root = insert(root, 3);

root = insert(root, 1);

root = insert(root, 6);

root = insert(root, 7);

root = insert(root, 10);

root = insert(root, 14);

root = insert(root, 4);

printf("Inorder traversal: ");

inorder(root);

printf("\nAfter deleting 10\n");

root = deleteNode(root, 10);

printf("Inorder traversal: ");

inorder(root);

}
OUTPUT:

INORDER TRAVERSAL:1->3->4->6->7->8->10->14->

AFTER DELETING 10

INORDER TRAVERSAL:1->3->4->6->7->8->14->
DEVELEPMENT OF REAL TIME APPLICATIONS

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

int i, j, height = 30;

int width = 30, gameover, score;

void draw()

for (i = 0; i < height; i++) {

for (j = 0; j < width; j++) {

if (i == 0 || i == width - 1 || j == 0

|| j == height - 1) {

printf("#");

else {
printf(" ");

printf("\n");

int main()

draw();

return 0;

}
OUTPUT:

#############

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

#############
APPLICATION OF STACK,QUEUE AND LIST

PROGRAM:

STACK:

#include <stdio.h>

int MAXSIZE = 8;

int stack[8];

int top = -1;

int isempty() {

if(top == -1)

return 1;

else

return 0;

int isfull() {

if(top == MAXSIZE)

return 1;

else

return 0;

int peek() {

return stack[top];

}
int pop() {

int data;

if(!isempty()) {

data = stack[top];

top = top - 1;

return data;

} else {

printf("Could not retrieve data, Stack is empty.\n");

int push(int data) {

if(!isfull()) {

top = top + 1;

stack[top] = data;

} else {

printf("Could not insert data, Stack is full.\n");

int main() {

// push items on to the stack

push(3);

push(5);

push(9);

push(1);
push(12);

push(15);

printf("Element at top of the stack: %d\n" ,peek());

printf("Elements: \n");

// print stack data

while(!isempty()) {

int data = pop();

printf("%d\n",data);

printf("Stack full: %s\n" , isfull()?"true":"false");

printf("Stack empty: %s\n" , isempty()?"true":"false");

return 0;

}
OUTPUT:

Output

Element at top of the stack: 15

Elements:

15

12

Stack full: false

Stack empty: true

You might also like