Index: S.No Experiment Date of Submissio N Date of Correction Sign
Index: S.No Experiment Date of Submissio N Date of Correction Sign
Index: S.No Experiment Date of Submissio N Date of Correction Sign
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");
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
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
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
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
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");
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
Output:
SAKSHI SINGH
200384 15
Experiment – 5.d
SAKSHI SINGH
200384 16
Experiment – 6.a
Output:
SAKSHI SINGH
200384 19
Experiment – 6.b
Output:
SAKSHI SINGH
200384 22
Experiment – 7
Output:
SAKSHI SINGH
200384 24
SAKSHI SINGH
200384 25
Experiment – 11
SAKSHI SINGH
200384 26
Output:
SAKSHI SINGH
200384 27
Experiment – 12
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
Output:
SAKSHI SINGH
200384 30
SAKSHI SINGH
200384 31