21BCE2209 DSALab (A1.2)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Name: Saksham Bajaj Reg No: 21BCE2209

Course: BCSE202P-DSA Lab (L9+L10)


Assignment 1.2

Q1) Two friends like to pool their money and go to the ice cream parlour.

They always choose two distinct flavours and they spend all of their money.

Given a list of prices for the flavours of ice cream, select the two that will cost

all of the money or closest to the money they have.

Algorithm IceCreamFlavours(n,flavours[n],m)

Input Integer n, array Flavours of length n, Integer m

Output Combination of two flavours that sum up to entered cost

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

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

if(flavours[i]+flavours[j]==m)

printf("Combination found at index: %d and %d\n",i,j);

else

printf("No combination found");

return 0;

Code

#include<stdio.h>

int main()

{
int i,j,m,n;

printf("Number of flavours:\n");

scanf("%d",&n);

int flavours[n];

printf("Enter cost of different flavours:\n");

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

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

printf("Enter total cost:\n");

scanf("%d",&m);

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

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

if(flavours[i]+flavours[j]==m)

printf("Combination found at index: %d and %d\n",i,j);

return 0;

printf("No combination found");

return 0;

}
Output
Q2) The median of a list of numbers is essentially its middle element after

sorting. The same number of elements occur after it as before. Given a list of

numbers with an odd number of elements, find the median?

Algorithm MedianOfArray(n,arr[n])

Input Integer n, array arr of length n

Output Median of sorted array

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

j = i;

while (j > 0 && arr[j - 1] > arr[j]){

temp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = temp;

j--;

return 0;

Code

#include <stdio.h>

int main()

int n, i, j, temp,;

printf("Enter number of elements\n");


scanf("%d", &n);

int arr[n];

printf("Enter %d integers\n", n);

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

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

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

j = i;

while (j > 0 && arr[j - 1] > arr[j])

temp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = temp;

j--;

printf("Median of sorted array:\n");

printf("%d",arr[n/2]);

return 0;

}
Output

You might also like