Index: S.No Experiment Date of Submissio N Date of Correction Sign

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

S.

No Experiment Date of Date of Sign


Submissio correction
n

Index

SAKSHI SINGH
200384 1
S.No Experiment Date of Date of Sign
Submissio correction
n

SAKSHI SINGH
200384 2
Experiment – 1.a

Aim: Find the maximum among given numbers and count no. of steps.
Source Code:
#include <stdio.h>
int main(){
int i,max,a[20],size,count;
count=0;
max=0;
printf("Enter size of array:");
scanf("%d",&size);
printf("Enter %d elements:",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<size;i++){
if(a[i]>max)
max=a[i];
count++;
}
printf("Maximum element:%d\n",max);
printf("No. of steps:%d",count);
}

Output:

SAKSHI SINGH
200384 3
Experiment – 1.b

Aim: To multiply and add 2 matrices of given sizes and count no. of steps taken.
Source Code:
#include <stdio.h>
int main(){
int a[20][20],b[20][20],c[20][20],i,j,k,r1,c1,r2,c2,count1,count2,tot;
count1=0;
count2=0;
tot=0;
printf("For first matrix:\nEnter no. of rows:");
scanf("%d",&r1);
printf("Enter no. of columns:");
scanf("%d",&c1);
printf("Enter elements:\n");
for (i = 0; i < r1; i++) {
for (j = 0 ; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("For second matrix:\nEnter no. of rows:");
scanf("%d",&r2);
printf("Enter no. of columns:");
scanf("%d",&c2);
printf("Enter elements:\n");
for (i = 0; i < r2; i++) {
for (j = 0 ; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
if(r1!=r2 || c1!=c2){
printf("Addition can't be performed!");
}
else{
printf("Sum of entered matrices:\n");

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


for (j = 0 ; j < c1; j++) {
c[i][j] = a[i][j] + b[i][j];
printf("%d\t", c[i][j]);
count1++;
}
printf("\n");
}
SAKSHI SINGH
200384 4
}
printf("No. of steps:%d\n",count1);
if(c1!=r2)
printf("The matrix cannot multiplied!");
else{
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < r2; k++) {
tot = tot + a[i][k] * b[k][j];
count2++;
}
c[i][j] = tot;
tot = 0;
}
}
printf(" Product of entered matrices:\n ");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++)
printf("%d \t", c[i][j] );
printf(" \n ");
}
}
printf("No. of steps:%d\n",count2);
}

Output:

SAKSHI SINGH
200384 5
Experiment – 1.c

Aim: Sum of n given numbers of a list (recursive) and count no. of steps taken..
Source Code:
#include <stdio.h>
int count=0;
int sum(int *a,int size);
int main(){
int a[20],size,i,s;
printf("Enter size for the array:");
scanf("%d",&size);
printf("Enter %d elements:",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
s=sum(a,size);
printf("Sum:%d\nNo. of steps:%d",s,count);
}
int sum(int *a,int size){
count++;
if(size<=0)
return 0;
else
return a[size-1]+sum(a,a[size-2]);
}

Output:

SAKSHI SINGH
200384 6
Experiment – 1.d

Aim: Sum of n given numbers of a list (non-recursive) and count no. of steps taken..
Source Code:
#include <stdio.h>
int main(){
int a[20],size,sum,i,count;
count=0;
sum=0;
printf("Enter size for the array:");
scanf("%d",&size);
printf("Enter %d elements:",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
sum=sum+a[i];
count++;
}
printf("Sum:%d",sum);
printf("\nNo. of steps:%d",count);
}

Output:

SAKSHI SINGH
200384 7
Experiment – 2

Aim: To compare performance of various functions:


n, log n, n2, n3, 2n, 3n
(Inputs: 102, 103, 104, 105, 106)

Source Code:
#include <stdio.h>
#include <math.h>
int main(){
int n[20], i, s;
printf("Enter number of inputs: ");
scanf("%d", &s);
printf("Enter value of n: ");
for(i=0; i<s; ++i){
scanf("%d", &n[i]);
}
for(i=0; i<s; ++i)
{
printf("\n%d. n: %d", i+1, n[i]);
printf("\n nlog(n): %.3f", n[i]*log10(n[i]));
printf("\n n^2: %d", n[i]*n[i]);
printf("\n n^3: %d", n[i]*n[i]*n[i]);
printf("\n 2^n: %.0f", pow(2, n[i]));
printf("\n 3^n: %.0f\n\n", pow(3, n[i]));
}
}

Output:

SAKSHI SINGH
200384 8
Experiment – 3

Aim: Write a program to perform linear search on a given set of an array.

Source Code:
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("Enter size for array:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index:%d",i);
else
printf("Element not found");
}

Output:

SAKSHI SINGH
200384 9
Experiment – 4.a

Aim: Write a program to perform binary search on a given set of an array.


Source Code:
#include <stdio.h>
int main(){
int c, first, last, middle, n, search, array[100];
printf("Enter size of array:");
scanf("%d", &n);
printf("Enter %d elements:", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find:");
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("Element found at index: %d", middle);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
}

Output:

SAKSHI SINGH
200384 10
Experiment – 4.b

Aim: Write a program to perform binary search on a given set of an array using recursion.
Source Code:
#include <stdio.h>
int recursiveBinarySearch(int array[], int start_index, int end_index, int element){
if (end_index >= start_index){
int middle = start_index + (end_index - start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] > element)
return recursiveBinarySearch(array, start_index, middle-1, element);
return recursiveBinarySearch(array, middle+1, end_index, element);
}
}
int main(){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 9;
int found_index = recursiveBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
}

Output:

SAKSHI SINGH
200384 11
Experiment – 5.a

Aim: To calculate time taken to find maximum among n given numbers.


Source Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(){
int i,max,a[20],size;
clock_t t;
max=0;
printf("Enter size of array:");
scanf("%d",&size);
printf("Enter %d elements:",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
t = clock();
for(int i=0;i<size;i++){
if(a[i]>max)
max=a[i];
}
sleep(1);
t = clock() - t;
printf("Maximum element:%d",max);
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nProgram took %f seconds to execute \n", time_taken-1);
}

Output:

SAKSHI SINGH
200384 12
Experiment – 5.b

Aim: To calculate time taken to multiply and add two matrices of given size.
Source Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(){
int a[20][20],b[20][20],c[20][20],i,j,k,r1,c1,r2,c2,tot;
tot=0;
clock_t t;
printf("For first matrix:\nEnter no. of rows:");
scanf("%d",&r1);
printf("Enter no. of columns:");
scanf("%d",&c1);
printf("Enter elements:\n");
for (i = 0; i < r1; i++) {
for (j = 0 ; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("For second matrix:\nEnter no. of rows:");
scanf("%d",&r2);
printf("Enter no. of columns:");
scanf("%d",&c2);
printf("Enter elements:\n");
for (i = 0; i < r2; i++) {
for (j = 0 ; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
t = clock();
if(r1!=r2 || c1!=c2){
printf("Addition can't be performed!");
}
else{
printf("Sum of entered matrices:\n");

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


for (j = 0 ; j < c1; j++) {
c[i][j] = a[i][j] + b[i][j];
printf("%d\t", c[i][j]);
}
printf("\n");

SAKSHI SINGH
200384 13
}
}
if(c1!=r2)
printf("The matrix cannot multiplied!");
else{
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < r2; k++) {
tot = tot + a[i][k] * b[k][j];
}
c[i][j] = tot;
tot = 0;
}
}
printf(" Product of entered matrices:\n ");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++)
printf("%d \t", c[i][j] );
printf(" \n ");
}
}
sleep(1);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nProgram took %f seconds to execute \n", time_taken-1);
}

Output:

SAKSHI SINGH
200384 14
Experiment – 5.c

Aim: To calculate time taken to find sum of n numbers in an array.


Source Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(){
int a[20],size,sum,i;
sum=0;
clock_t t;
printf("Enter size for the array:");
scanf("%d",&size);
printf("Enter %d elements:",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
t = clock();
for(i=0;i<size;i++){
sum=sum+a[i];
}
printf("Sum:%d",sum);
sleep(1);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nProgram took %f seconds to execute \n", time_taken-1);
}

Output:

SAKSHI SINGH
200384 15
Experiment – 5.d

Aim: To calculate time taken to find sum of n numbers in an array(recursive).


Source Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int sum(int *a,int size);
int main(){
int a[20],size,i,s;
clock_t t;
printf("Enter size for the array:");
scanf("%d",&size);
printf("Enter %d elements:",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
t = clock();
s=sum(a,size);
printf("Sum:%d",s);
sleep(1);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nProgram took %f seconds to execute \n", time_taken-1);
}
int sum(int *a,int size){
if(size<=0)
return 0;
else
return a[size-1]+sum(a,a[size-2]);
}
Output:

SAKSHI SINGH
200384 16
Experiment – 6.a

Aim: To compare insertion in linked list and array.


Source Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
int count=0,size;
int createList();
int traverseList();
int insertion();
struct node{
int data;
struct node *next;
}*head;
int main(){
int i,a[20],n;
clock_t t;
t=clock();
printf("Enter size:");
scanf("%d",&size);
printf("Enter elements for the array:");
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
printf("Enter element to be inserted:");
scanf("%d",&n);
for(i=0;i<size;i++){
count++;
}
a[i]=n;
size++;
printf("Element Inserted!\nArray:");
for(i=0;i<size;i++){
printf("%d ",a[i]);
}
sleep(1);
t=clock()-t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nNo. of steps:%d\nProgram took %f seconds to execute\n",count,time_taken-
1);
createList();
t=clock();
SAKSHI SINGH
200384 17
insertion();
traverseList();
sleep(1);
t=clock()-1;
time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nNo. of steps:%d\nProgram took %f seconds to execute",count,time_taken-1);
}
int createList(){
struct node *temp, *newNode;
int data,i;
printf("Enter size:");
scanf("%d",&size);
head=(struct node*)malloc(sizeof(struct node));
if(head == NULL)
printf("Cannot allocate memory");
else{
printf("Enter data for list:");
scanf("%d",&data);
head->data = data;
head->next = NULL;
temp = head;
for(i=0;i<size-1;i++){
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL){
printf("Cannot allocate memory");
return 0;
}
else{
scanf("%d",&data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
}
}
int traverseList(){
struct node *temp;
if(head == NULL){
printf("List is empty");
return 0;
}
else{
printf("List:");
temp=head;
SAKSHI SINGH
200384 18
while(temp!=NULL){
printf("%d ",temp->data);
temp = temp->next;
}
}
}
int insertion()
{
struct node *temp,*newNode;
int data;
count=0;
temp=head;
newNode = (struct node*)malloc(sizeof(struct node*));
if(newNode == NULL)
{
printf("Cannot allocate memory");
}
else
{
printf("Enter element to be inserted:");
scanf("%d",&data);
newNode->data = data;
newNode->next = NULL;
}
while(temp->next!=NULL)
{
temp=temp->next;
count++;
}
temp->next=newNode;
printf("Element Inserted!\n");
}

Output:

SAKSHI SINGH
200384 19
Experiment – 6.b

Aim: To compare deletion in linked list and array.


Source Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
int count=0,size;
int createList();
int traverseList();
int deletion();
struct node{
int data;
struct node *next;
}*head;
int main(){
int i,a[20];
clock_t t;
t=clock();
printf("Enter size:");
scanf("%d",&size);
printf("Enter elements for the array:");
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
for(i=0;i<size;i++){
count++;
}
size--;
printf("Element Deleted!\nArray:");
for(i=0;i<size;i++){
printf("%d ",a[i]);
}
sleep(1);
t=clock()-t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nNo. of steps:%d\nProgram took %f seconds to execute\n",count,time_taken-
1);
createList();
t=clock();
deletion();
traverseList();
sleep(1);
SAKSHI SINGH
200384 20
t=clock()-1;
time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nNo. of steps:%d\nProgram took %f seconds to execute",count,time_taken-1);
}
int createList(){
struct node *temp, *newNode;
int data,i;
printf("Enter size:");
scanf("%d",&size);
head=(struct node*)malloc(sizeof(struct node));
if(head == NULL)
printf("Cannot allocate memory");
else{
printf("Enter data for list:");
scanf("%d",&data);
head->data = data;
head->next = NULL;
temp = head;
for(i=0;i<size-1;i++){
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL){
printf("Cannot allocate memory");
return 0;
}
else{
scanf("%d",&data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
}
}
int traverseList(){
struct node *temp;
if(head == NULL){
printf("List is empty");
return 0;
}
else{
printf("List:");
temp=head;
while(temp!=NULL){
printf("%d ",temp->data);
temp = temp->next;
SAKSHI SINGH
200384 21
}
}
}
int deletion(){
struct node *temp;
temp=head;
while(temp->next->next!=NULL)
{
temp = temp->next;
}
temp->next=NULL;
temp=temp->next;
free(temp);
printf("Element Deleted!\n");
}

Output:

SAKSHI SINGH
200384 22
Experiment – 7

Aim: To perform linear search using linked list


Source Code:
#include <unistd.h>
#include <stdlib.h>
int count=0,size;
int createList();
int traverseList();
int search();
struct node{
int data;
struct node *next;
}*head;
int main(){
createList();
clock_t t;
t=clock();
search();
traverseList();
sleep(1);
t=clock()-1;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("\nNo. of steps:%d\nProgram took %f seconds to execute",count,time_taken-1);
}
int createList(){
struct node *temp, *newNode;
int data,i;
printf("Enter size:");
scanf("%d",&size);
head=(struct node*)malloc(sizeof(struct node));
if(head == NULL)
printf("Cannot allocate memory");
else{
printf("Enter data for list:");
scanf("%d",&data);
head->data = data;
head->next = NULL;
temp = head;
for(i=0;i<size-1;i++){
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL){
printf("Cannot allocate memory");
return 0;
SAKSHI SINGH
200384 23
}
else{
scanf("%d",&data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
}
}
int traverseList(){
struct node *temp;
if(head == NULL){
printf("List is empty");
return 0;
}
else{
printf("List:");
temp=head;
while(temp!=NULL){
printf("%d ",temp->data);
temp = temp->next;
}
}
}
int search(){
struct node *temp;
int n;
printf("Enter element to be searched:");
scanf("%d",&n);
temp=head;
while(temp->data!=n){
temp = temp->next;
count++;
}
printf("Element found at:%d",count);
}

Output:

SAKSHI SINGH
200384 24
SAKSHI SINGH
200384 25
Experiment – 11

Aim: Write a program to perform fractional knapsack problem.


Source Code:
#include <stdio.h>
int n = 5;
int c[10] = {12, 1, 2, 1, 4};
int v[10] = {4, 2, 2, 1, 10};
int W = 15;
void simple_fill() {
int cur_w;
float tot_v;
int i, maxi;
int used[10];
for (i = 0; i < n; ++i)
used[i] = 0;
cur_w = W;
while (cur_w > 0) {
maxi = -1;
for (i = 0; i < n; ++i)
if ((used[i] == 0) &&
((maxi == -1) || ((float)v[i]/c[i] > (float)v[maxi]/c[maxi])))
maxi = i;
used[maxi] = 1;
cur_w -= c[maxi];
tot_v += v[maxi];
if (cur_w >= 0)
printf("Added object %d (%d$, %dKg) completely in the bag. Space left: %d.\n",
maxi + 1, v[maxi], c[maxi], cur_w);
else {
printf("Added %d%% (%d$, %dKg) of object %d in the bag.\n", (int)((1 +
(float)cur_w/c[maxi]) * 100), v[maxi], c[maxi], maxi + 1);
tot_v -= v[maxi];
tot_v += (1 + (float)cur_w/c[maxi]) * v[maxi];
}
}
printf("Filled the bag with objects worth %.2f$.\n", tot_v);
}
int main(int argc, char *argv[]) {
simple_fill();
}

SAKSHI SINGH
200384 26
Output:

SAKSHI SINGH
200384 27
Experiment – 12

Aim: Write a program to perform job sequencing.


Source Code:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Job {
char id;
int dead;
int profit;
} Job;
int compare(const void* a, const void* b){
Job* temp1 = (Job*)a;
Job* temp2 = (Job*)b;
return (temp2->profit - temp1->profit);
}
int min(int num1, int num2){
return (num1 > num2) ? num2 : num1;
}
void printJobScheduling(Job arr[], int n){
qsort(arr, n, sizeof(Job), compare);

int result[n];
bool slot[n];
for (int i = 0; i < n; i++)
slot[i] = false;
for (int i = 0; i < n; i++) {
for (int j = min(n, arr[i].dead) - 1; j >= 0; j--) {
if (slot[j] == false) {
result[j] = i;
slot[j] = true;
break;
}
}
}
for (int i = 0; i < n; i++)
if (slot[i])
printf("%c ", arr[result[i]].id);
}
int main(){
Job arr[] = { { 'a', 2, 100 },

SAKSHI SINGH
200384 28
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
int n = sizeof(arr) / sizeof(arr[0]);
printf(
"Following is maximum profit sequence of jobs \n");
printJobScheduling(arr, n);
return 0;
}

Output:

SAKSHI SINGH
200384 29
Experiment – 13

Aim: Write a program to perform matrix multiplication.


Source Code:
#include <limits.h>
#include <stdio.h>
int MatrixChainOrder(int p[], int n){
int m[n][n];
int i, j, k, L, q;
for (i = 1; i < n; i++)
m[i][i] = 0;
for (L = 2; L < n; L++) {
for (i = 1; i < n - L + 1; i++) {
j = i + L - 1;
m[i][j] = INT_MAX;
for (k = i; k <= j - 1; k++) {
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n - 1];
}
int main(){
int arr[] = { 1, 2, 3, 4 };
int size = sizeof(arr) / sizeof(arr[0]);
printf("Minimum number of multiplications is %d ",
MatrixChainOrder(arr, size));
}

Output:

SAKSHI SINGH
200384 30
SAKSHI SINGH
200384 31

You might also like