Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programs: Data Structures, Array, Matrix Mult., Binary Search, Sorting, Linked Lists, Exams of Computer Science

C programs for various data structures including arrays, matrix multiplication, binary search, sorting algorithms (bubble sort, insertion sort, merge sort, quick sort), and linked lists (queue). The programs demonstrate how to input elements, search, sort, and manipulate data structures using c.

What you will learn

  • What is the purpose of the program for inputting an element into a 1-D array?
  • What is the purpose of the program for bubble sort?
  • How does the program for binary search work?
  • What is the purpose of the program for multiplication of two matrices?
  • How does the program for addition of two arrays work?

Typology: Exams

2017/2018

Uploaded on 09/02/2018

PANKAJ928
PANKAJ928 🇮🇳

1 document

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Session-2016-17
Haldwani, Nainital
Data Structure
Program File
Submitted To- Submitted By-
Mr. Shekhar Kumar Deepak Kishor Bhatt
(Professor) Class-B.Sc.(C.S.) II Sem.
Roll No.- 1613200138
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download C Programs: Data Structures, Array, Matrix Mult., Binary Search, Sorting, Linked Lists and more Exams Computer Science in PDF only on Docsity!

Session- 2016 - 17

Haldwani, Nainital

Data Structure Program File

Submitted To- Submitted By-

Mr. Shekhar Kumar Deepak Kishor Bhatt

(Professor) Class-B.Sc.(C.S.) II Sem.

Roll No.- 1613200138

Sr.

No. Contents^

Page

No.

Program to input element into 1-

D Array

Program for addition of two

Arrays

Program for multiplication of two

Matrices

Program to Implement Linear

Search

Program to Implement Binary

Search

Program for Bubble Sort

Program for Insertion Sort

Program to implement Merge

Sort

Program for Quick Sort

Program for Implementation of

Stack by Array

Program to implement Queue

using Linked list

Program for Single Linked list

INDEX

3).Program for multiplication of two Matrices

#include<stdio.h> int main() { int a[10][10], b[10][10], c[10][10], i, j, k; int sum = 0; printf("\nEnter First Matrix : n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &a[i][j]); } } printf("\nEnter Second Matrix:n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &b[i][j]); } } printf("The First Matrix is: \n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(" %d ", a[i][j]); } printf("\n"); } printf("The Second Matrix is : \n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(" %d ", b[i][j]); } printf("\n"); } //Multiplication Logic for (i = 0; i <= 2; i++) { for (j = 0; j <= 2; j++) { sum = 0; for (k = 0; k <= 2; k++) { sum = sum + a[i][k] * b[k][j]; } c[i][j] = sum; } } printf("\nMultiplication Of Two Matrices : \n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(" %d ", c[i][j]); } printf("\n"); } Output

return (0); }

4).Program to Implement Linear Search

/* Implementation of Linear Search*/ #include<stdio.h> #include<conio.h> void main() { char ch; int arr[50],n,i,item; clrscr(); printf("\nHow many elements you want to enter in the array:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter element %d:",i+1); scanf("%d",&arr[i]); } printf("\n\nPress any key to continue....."); getch(); do { clrscr(); printf("\nEnter the element to be searched:"); scanf("%d",&item); for(i=0;i < n;i++) { if(item == arr[i]) { printf("\n%d found at position %d\n",item,i+1); break; } } if (i == n) printf("\nItem %d not found in array\n",item); printf("\n\nPress (Y/y) to continue: "); fflush(stdin); scanf("%c",&ch); }while(ch == 'Y'|| ch == 'y'); }

5).Program to Implement Binary Search

/* Implementation of Binary Search */ #include<stdio.h> #include<conio.h> void main() { char ch; int arr[20],start,end,mid,n,i,data; Output

if(x[j]>x[j+1]) { tmp=x[j]; x[j]=x[j+1]; x[j+1]=tmp; } } } } void main() { int x[MAXSIZE],n,i; clrscr(); printf("Enter the number of elements: "); scanf("%d",&n); printf("\nEnter the elements: \n"); for(i=0;i<n;i++) scanf("%d",&x[i]); bubble(x,n); printf("\nThe sorted output:\n"); for(i=0;i<n;i++) printf("\n%d",x[i]); getch(); }

7).Program for Insertion Sort

#include<stdio.h> #include<conio.h> void insertion(int x[],int n) { int i,j,temp; for(i=0;i<n;i++) { temp=x[i]; for(j=i-1;j>=0;j--) { if(temp<x[j]) x[j+1]=x[j]; else break; } Output

x[j+1]=temp; } } void main() { int x[10],n,i; clrscr(); printf("Enter the number of elements: "); scanf("%d",&n); printf("\nEnter the elements: \n"); for(i=0;i<n;i++) scanf("%d",&x[i]); insertion(x,n); printf("\nThe sorted output:\n"); for(i=0;i<n;i++) printf("\n%d",x[i]); getch(); }

8).Program to implement Merge Sort

#include<stdio.h> #include<conio.h> #define MAX 20 int array[MAX]; void merge(int low, int mid, int high ) { int temp[MAX]; int i = low; int j = mid +1 ; int k = low ; while( (i <= mid) && (j <=high) ) { if (array[i] <= array[ j]) temp[k++] = array[i++] ; else temp[k++] = array[ j++] ; } while( i <= mid ) temp[k++]=array[i++]; while( j <= high ) temp[k++]=array[j++]; for (i= low; i <= high ; i++) array[i]=temp[i]; } void merge_sort(int low, int high ) { int mid; if ( low != high ) { Output

if (low < high) { j = partition(a,low, high); quicksort (a, low, j-1); quicksort(a, j+1, high); } } int partition(int a[], int low, int high) { int i, j, temp, key; key = a[low]; i = low + 1; j = high; while (1) { while (i < high && key >= a[i]) i++; while (key < a[j]) j--; if (i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } else { temp = a[low]; a[low] = a[j]; a[j] = temp; } return j; } }

10).Program for Implementation of Stack by

Array

/* Implementation of stack by Array */ #include <stdio.h> #include <conio.h> #define MAX 50 int stack [MAX+1], top = 0; void main ( ) { clrscr(); void create ( ), traverse ( ), push ( ), pop ( ); create ( ); printf("\n Stack is :\n"); Output

traverse ( ); push ( ); printf("After Push an element the stack is:\n"); traverse ( ); pop ( ); printf("After pop the element the stack is:\n"); traverse ( ); getch ( ); } void create ( ) { char ch; do { top ++; printf ("Input Element"); scanf ("%d", &stack[top]); printf ("Press for more element \n"); ch = getch ( ); } while (ch=='Y'); } void traverse ( ) { int i; for (i=top; i>0; --i) printf ("%d\n", stack[i]); } void push ( ) { int m; if (top==MAX) { printf ("Stack is overflow"); return; } printf ("Input New Element to Insert"); scanf ("%d", &m); top++; stack[top]=m; } void pop ( ) { if (top==0) { printf ("Stack is underflow\n"); return; } stack[top]='\0'; top--; } Output

struct node ptr; ptr = (struct node) malloc (sizeof (struct node)); int item; printf ("Input the element for inserting :\n"); scanf ("%d", &item); ptr-> info = item; ptr->link = NULL; if (front==NULL) /* queue is empty*/ front = ptr; else rear->link = ptr; rear = ptr; void delet( ) { struct node *ptr; if (front==NULL) { printf ("Queue is underflow \n"); return; } if (front==rear) { free (front); rear = NULL; } else { ptr = front; front = ptr->link; free (ptr); } } void display ( ) { struct node *ptr; ptr = front; if (front==NULL) printf("Queue is empty\n"); else { printf("\nElements in the Queue are:\n"); while(ptr!=NULL) { printf(" %d\n",ptr->info); ptr=ptr->link; } printf("\n"); } } Output Output

12).Program for Single Linked List

/* Single linklist */ #include <stdio.h> #include <conio.h> #include <alloc.h> struct node { int info; struct node *link; }; node first; void main ( ) { void create ( ); clrscr ( ); create ( ); getch ( ); } void create ( ) { struct node * ptr, cpt; char ch; ptr = (struct node) malloc (sizeof (struct node)); printf("Input first node information"); scanf("%d", &ptr->info); first=ptr; do { cpt=(struct node) malloc(sizeof(struct node)); printf("Input next node information"); scanf("%d",&cpt->info); ptr->link = cpt; ptr = cpt; printf ("Press <Y/N> for more node"); ch = getch ( ); } while (ch=='Y'); ptr->link = NULL; } Output