0% found this document useful (0 votes)
11 views4 pages

1:40 PM On 28/11/2024 Do Not Copy!!!: Instructions: Please Read Carefully Teams

ds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

1:40 PM On 28/11/2024 Do Not Copy!!!: Instructions: Please Read Carefully Teams

ds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Instructions: Please read carefully

• Please rename this file as only your ID number (e.g. 18-*****-1.docx or 18-*****-1.pdf).
• Submit the file before 1:40 PM on 28/11/2024 in the Teams labeled Mid_Lab Exam.

Do not Copy!!!

Question 1:
Mr. Alam has some banknotes in his wallet, which he wants to bring out from there. After bringing it out he noticed
that he had ten banknotes of several values (like 100, 20, 10, 500, 50 etc. taka). There were multiple notes of the
same values (like two 50 taka notes etc). He used your code to input the ten banknote values. Now i) your code
should sort the bank notes in descending order, ii) find out the total amount of taka, iii) give an amount less than
the total amount, and find out how many notes needs to make that value. Solve this problem without using a vector.

Sample Input: 20 10 20 500 100 100 200 50 1000 50


Sample Output:
After Sort: 1000 500 200 100 100 50 50 20 10 10
Total Amount: 2040 taka
Provide an amount less than total: 2000
Total notes needed: 7 which are 1000 500 200 100 100 50 50

#include <iostream>
using namespace std;

void swap(int &a , int &b)


{
int temp = a;
a = b;
b = temp;
}

void bubbleSort(int *arr,int size)


{
bool flag;
for (int i = 0; i < size-1; i++)
{
flag = false;
for (int j = 0; j < size-i-1; j++)
{
if (arr[j]<arr[j+1])
{
swap(arr[j],arr[j+1]);
flag = true;
}
}
if(!flag)
{
break;
}
}
}

int main() {
int taka[10];
int targetAmount, totalAmount;

cout << "Enter 10 banknote:";


for (int i = 0; i < 10; i++)
{
cin >> taka[i];
}
bubbleSort(taka, sizeof(taka)/sizeof(int));

cout << "After Sort: ";


for (int i = 0; i < 10; i++)
{
cout << taka[i] << " ";
}
cout << endl;

totalAmount = 0;
for (int i = 0; i < 10; i++)
{
totalAmount += taka[i];
}
cout << "Total Amount: " << totalAmount << " taka" << endl;

cout << "Provide an amount less than total: ";


cin >> targetAmount;

int currentSum = 0, noteCount = 0;


cout << "Total notes needed: ";
for (int i = 0; i < 10 && currentSum < targetAmount; i++)
{
if (currentSum + taka[i] <= targetAmount)
{
currentSum += taka[i];
noteCount++;
}
}
cout<<noteCount<<" whice are ";

currentSum = 0;
for (int i = 0; i < 10 && currentSum < targetAmount; i++)
{
if (currentSum + taka[i] <= targetAmount)
{
currentSum += taka[i];
cout << taka[i] << " ";
}
}
}
Your whole Screenshot here: (Console Output):

1. Write a code to implement Insertion Sort for the following list


50 60 44 222 15 24 63 57 59 88
Your code here:
#include <iostream>
using namespace std;

void insertionSort(int arr[], int size)


{
for (int i = 1; i < size; i++)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

int main()
{
int arr[] = {50, 60, 44, 222, 15, 24, 63, 57, 59, 88};
int size = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, size);

cout << "Sorted Array: ";


for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
Your whole Screenshot here: (Console Output):

You might also like