DSA ASSIGNMENT-1
Name: Anubhav Kaushik
Enrolment No: -22102035
Batch: -A2
Ans1.
#include <iostream>
#include <cmath>
void decimalToBinary(int num) {
int binary[32];
int *ptr = binary;
int i = 0;
while (num > 0) {
*(ptr + i) = num % 2;
num /= 2;
i++;
for (int j = i - 1; j >= 0; j--) {
std::cout << *(ptr + j);
std::cout << std::endl;
void decimalToOctal(int num) {
int octal[32];
int *ptr = octal;
int i = 0;
while (num > 0) {
*(ptr + i) = num % 8;
num /= 8;
i++;
for (int j = i - 1; j >= 0; j--) {
std::cout << *(ptr + j);
std::cout << std::endl;
void decimalToHex(int num) {
char hex[32];
char *ptr = hex;
int i = 0;
while (num > 0) {
int remainder = num % 16;
*(ptr + i) = (remainder < 10) ? remainder + '0' : remainder - 10 + 'A';
num /= 16;
i++;
for (int j = i - 1; j >= 0; j--) {
std::cout << *(ptr + j);
std::cout << std::endl;
void menu() {
int choice, num;
std::cout << "1. Decimal to Binary\n2. Decimal to Octal\n3. Decimal to Hexadecimal\nEnter
choice: ";
std::cin >> choice;
std::cout << "Enter decimal number: ";
std::cin >> num;
switch(choice) {
case 1: decimalToBinary(num); break;
case 2: decimalToOctal(num); break;
case 3: decimalToHex(num); break;
default: std::cout << "Invalid choice"; break;
int main() {
menu();
return 0;
Ans2.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct Student {
std::string name;
int enrollment;
int marks[5];
};
bool compareByName(const Student &a, const Student &b) {
return a.name < b.name;
bool compareByEnrollment(const Student &a, const Student &b) {
return a.enrollment < b.enrollment;
void inputStudents(std::vector<Student> &students, int n) {
for (int i = 0; i < n; i++) {
Student s;
std::cout << "Enter name: ";
std::cin >> s.name;
std::cout << "Enter enrollment number: ";
std::cin >> s.enrollment;
std::cout << "Enter marks for 5 subjects: ";
for (int j = 0; j < 5; j++) std::cin >> s.marks[j];
students.push_back(s);
void sortByName(std::vector<Student> &students) {
std::sort(students.begin(), students.end(), compareByName);
for (const auto &s : students) {
std::cout << s.name << " " << s.enrollment << "\n";
}
void sortByEnrollment(std::vector<Student> &students) {
std::sort(students.begin(), students.end(), compareByEnrollment);
for (const auto &s : students) {
std::cout << s.name << " " << s.enrollment << "\n";
void menu(std::vector<Student> &students) {
int choice;
std::cout << "1. Sort by Name\n2. Sort by Enrollment\nEnter choice: ";
std::cin >> choice;
if (choice == 1) sortByName(students);
else if (choice == 2) sortByEnrollment(students);
int main() {
std::vector<Student> students;
int n;
std::cout << "Enter number of students: ";
std::cin >> n;
inputStudents(students, n);
menu(students);
return 0;
}
Ans3.
#include <iostream>
#include <vector>
std::pair<int, int> findMissingAndRepeating(std::vector<int> &arr, int n) {
int missing, repeating;
std::vector<int> count(n + 1, 0);
for (int num : arr) {
count[num]++;
for (int i = 1; i <= n; i++) {
if (count[i] == 0) missing = i;
else if (count[i] == 2) repeating = i;
return {repeating, missing};
int main() {
int n;
std::cout << "Enter size of array: ";
std::cin >> n;
std::vector<int> arr(n);
std::cout << "Enter elements of array: ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
auto result = findMissingAndRepeating(arr, n);
std::cout << "Repeating: " << result.first << ", Missing: " << result.second << std::endl;
return 0;
Ans4.
#include <iostream>
#include <iomanip>
#include <string>
struct Fruit {
std::string name;
double pricePerKg;
};
void displayReceipt(Fruit *fruits, double *quantities, int n) {
double totalCost = 0.0;
std::cout << "********** Only Fresh Fruit Shop Receipt **********\n";
std::cout << std::setw(20) << "Fruit" << std::setw(15) << "Price per kg (RM)" <<
std::setw(15) << "Quantity (kg)" << std::setw(15) << "Cost (RM)\n";
for (int i = 0; i < n; i++) {
double cost = fruits[i].pricePerKg * quantities[i];
totalCost += cost;
std::cout << std::setw(20) << fruits[i].name
<< std::setw(15) << std::fixed << std::setprecision(2) << fruits[i].pricePerKg
<< std::setw(15) << quantities[i]
<< std::setw(15) << cost << "\n";
}
std::cout << "--------------------------------------------------\n";
std::cout << std::setw(50) << "Total Cost (RM): " << totalCost << "\n";
std::cout << "**************************************************\n";
int main() {
Fruit fruits[5] = {
{"Sunkist Orange", 2.00},
{"Strawberry", 22.00},
{"Papaya", 5.00},
{"Star fruit", 6.00},
{"Kiwi", 10.00}
};
double quantities[5];
std::cout << "Enter quantities (kg) for each fruit:\n";
for (int i = 0; i < 5; i++) {
std::cout << fruits[i].name << " (RM " << fruits[i].pricePerKg << " per kg): ";
std::cin >> quantities[i];
displayReceipt(fruits, quantities, 5);
return 0;