· web views.no name of experiment page no. date of performance date of submission grade awarded...
TRANSCRIPT
SESSION: 2014-2015
Submitted By Submitted to
Anup Patel Vipin Pal SirBranch-CSRoll no: 13TEC2CS002
INDEX
S.no
Name of experiment
Page no.
Date of performance
Date of submission
Grade awarded
Teachers signature
1 Array implementation-insertion ,deletion,traversing
3
2 Linear search,binary search 7
3 Bubble sort,selection sort 114 Matrix
addition,subtraction and multiplication
13
5 Insert an element in sorted array 17
6 Linked list – append,insertion,deletion,display
19
7 Sparse matrices 258 Stack using array 27
1)C program to insert element in array#include <stdio.h> int main(){ int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (c = 0; c < n; c++)
scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (c = n - 1; c >= position - 1; c--) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n"); for (c = 0; c <= n; c++) printf("%d\n", array[c]); return 0;}
C program to delete an element from an array#include <stdio.h> int main(){ int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1];
printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0;}
C PROGRAM TO TRANVERSE AN ARRAY#include<stdio.h>
#include<conio.h>
void input(int *p);
void display(int *q);
int main()
{
int a[10];
input(a);
display(a);
getch();
}
void input(int *p)
{
int i,b;
printf("enter the values in array\n");
for(i=0;i<10;i++)
{
scanf("%d",&b);
*p=b;
p++;
}
}
void display(int *q)
{
int i;
printf("value in array is\n");
for(i=0;i<10;i++)
{
printf("%d\n",*q);
q++;
}
}
2)C program for linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,p,q=0,k=0;
printf("enter the terms\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("enter the value you want to search\n");
scanf("%d",&p);
for(i=0;i<10;i++)
{
if(p==a[i])
{
k=1;
q=q+1;
}
if(k==0)
{
printf("item is not found\n");
}
if(k==1)
{
printf("item is found \n");
printf("number of times items is repeated =%d",q);
}
getch();
}
C programming code for binary search#include <stdio.h> int main(){ int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break;
} else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); return 0; }
3)Bubble sort algorithm in c/* Bubble sort code */ #include <stdio.h> int main(){ int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; }
} } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0;}
Selection sort algorithm implementation in c#include <stdio.h> int main(){ int array[100], n, c, d, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:\n");for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0;}
4)C programming code for addition of matrices
#include <stdio.h> int main(){ int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; printf("Sum of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t", sum[c][d]); printf("\n"); } return 0;}
C program to subtract matrices#include <stdio.h> int main(){ int m, n, c, d, first[10][10], second[10][10], difference[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++) for (d = 0 ; d < n; d++) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &second[c][d]); for (c = 0; c < m; c++) for (d = 0; d < n; d++) difference[c][d] = first[c][d] - second[c][d]; printf("difference of entered matrices:-\n"); for (c = 0; c < m; c++) { for (d = 0; d < n; d++) printf("%d\t",difference[c][d]); printf("\n"); } return 0;}
Matrix multiplication in c language#include <stdio.h> int main(){ int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n");
else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; } multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t", multiply[c][d]); printf("\n"); } } return 0;}
5)INSERT AN ELEMENT IN SORTED ARRAY
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],i,k,n;
printf("enter the number of items in array\n");
scanf("%d",&n);
printf("enter the terms of array in ascending order\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the value you want to insert\n");
scanf("%d",&k);
if(k>a[n-1]|| k==a[n-1])
{
a[n]=k;
}
for(i=n-1;i>=0;i--)
{
if(k<a[i])
{
a[i+1]=a[i];
a[i]=k;
}
}
printf("new array after insertion is\n");
for(i=0;i<n+1;i++)
{
printf("%d\n",a[i]);
}
getch();
}
6)LINKED LIST #include<stdio.h>#include<stdlib.h>#include<string.h>
struct info
{
char name[30];
int eno;
struct info *next;
};
struct info *head=NULL,*temp,*disp;
void addrecord();
void deleterecord();
void disrecord();
void main()
{
int ch;
clrscr();
while (1)
{
printf("\n 1. To add records\n");
printf("\n 2. To delete a records\n");
printf("\n 3. To view the records\n");
printf("\n 4. To exit\n");
printf("\n Enter your choice\n");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:addrecord();
break;
case 2:deleterecord();
break;
case 3: disrecord();
break;
case 4:exit(0);
}
}
}
void addrecord()
{
struct info *add;
char ans='y';
while (ans=='y')
{
add=(struct info*)malloc(sizeof(struct info));
printf("\n Enter the names:\n");
gets(add->name);
fflush(stdin);
printf("\n Enter the enrollment number:\n");
scanf("%d",&add->eno);
fflush(stdin);
if (head==NULL)
{
head=add;
add->next=NULL;
temp=add;
}
else
{
temp->next=add;
add->next=NULL;
temp=add;
}
printf("\n Would you like to enter another name(y\\n): \n");
ans = getchar();
fflush(stdin);
}
}
void deleterecord()
{
struct info *delete;
int teno, present=0;
if (head==NULL)
{
printf("\n No records to delete\n");
return;
}
printf("\n Enter the enrollment number to be deleted \n");
scanf("%d",&teno);
fflush(stdin);
for (delete=head;delete!=NULL;delete=delete->next)
{
if (delete->eno==teno)
{
if (head->eno==teno)
{
delete=head;
head=head->next;
free(delete);
return;
}
else
{
temp->next=delete->next;
free(delete);
return;
}
}
temp=delete;
}
if (present==0)
printf("\nNo such enrollment number present\n");
}
void disrecord()
{
if (head==NULL)
{
printf("\n No records to view\n");
return;
}
for (disp=head;disp!=NULL;disp=disp->next)
{
printf("\n\n Name : %s",disp->name);
printf("\n\n Number : %d",disp->eno);
}
}
7) * C program to determine if a given matrix is a sparse matrix. * Sparse martix has more zero elements than nonzero elements.
*/
#include <stdio.h>
void main ()
{
static int array[10][10];
int i, j, m, n;
int counter = 0;
printf("Enter the order of the matix \n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
if (array[i][j] == 0)
{
++counter;
}
}
}
if (counter > ((m * n) / 2))
{
printf("The given matrix is sparse matrix \n");
}
else
printf("The given matrix is not a sparse matrix \n");
printf("There are %d number of zeros", counter);
}
8) Stack using array#include <stdio.h>
#define MAXSIZE 5
int stack[MAXSIZE];
int top=0; //index pointing to the top of stack
void main()
{
void push();
void pop();
void display();
int will=1,i;
clrscr();
while(1)
{
printf("\n\n\nMAIN MENU:\n\n1.PUSH\n2.POP\n3.EXIT\n\nENTER YOUR CHOICE: ");
scanf("%d",&will);
switch(will)
{
case 1:
push();
display();
break;
case 2:
pop();
display();
break;
case 3:
exit(0);
break;
default:
printf("Invalid Choice . ");
}
} //end of outer while
} //end of main
void push()
{
int num;
if(top>=MAXSIZE)
{
printf("\nSTACK FULL");
return;
}
else
{ if(top<0)
top=0;
printf("\n\nENTER THE STACK ELEMENT : ");
scanf("%d",&num);
stack[top++]=num;
}
}
void pop()
{
if(top>=0)
top--;
}
void display()
{
int i;
if(top<=0)
printf("\n\nSTACK EMPTY");
else
for(i=top-1;i>=0;i--)
{
printf("\n\n%d ",stack[i]);