0% found this document useful (0 votes)
17 views6 pages

Bsee21036 Assignment 03

Uploaded by

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

Bsee21036 Assignment 03

Uploaded by

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

CS152T - Object Oriented Programming

Section: Assignment # 3 Total marks: 100


Name : __________________ Roll number : __________________

Submission:
• Email instructor if there are any questions. You cannot look at others’ solution or use others’
solution, however, you can discuss it with each other. Plagiarism will lead to a straight zero
with additional consequences as well.
• Submission after due time will not be accepted.
• Make a menu driven program (compulsory). Declare in function.h. Define functions in
function.cpp. Call these functions from main.cpp file using menu driven approach.(else if or
switch case). Create function.h, function.cpp, and main.cpp files by your self.

Task 1
University wants you to write a program that can generate seating arrangement of exam, so you
need to take as an input from them number of rows and columns where the students need to be
seated.
Hint : Create an M x N 2-Dimensional array of students using DMA.
Students information available are roll number, name and CNIC number
The program should, then, perform the following operations:
• Write a function to take the information of students from the user, based on number of
students.
• Write a function to take input from user for the number of rows and columns available in
the room
• Handle all boundary cases for capacity versus seating requirement
• Write a function to sort the students in decreasing order based on their roll number in
columns.
• Write a function to print all elements, separated by a “ - “ for with in a row and next row
should come in next line.
• Write a function to sort the students in alphabetical order based on their names in
columns.
Task 2
Recursive functions are the functions which are calling the same function in the definition of the
body in at least one possible flow. There is a supplier of water, who has containers of m liters and
you have containers of n liters. Write a program through a recursive function that will take in m
and n from the user and determine how many minimum m liters containers he can bring in
supply so you can collect through x number of n liters tanks. You cannot use partially filled
containers or return them either.

Task 3
Sets: The collection of well-defined distinct objects is known as a set. The word well-defined
refers to a specific property which makes it easy to identify whether the given object belongs to
the set or not. The word ‘distinct’ means that the objects of a set must be all different.

Sets and arrays have several features in common. They both store a collection of values of the
same type. A set is unordered and each element can only appear once in a set. While an array can
contain duplicate elements, each value contained in a set is unique.

Example: arr below is an array which does not ensure that data is not duplicated
therefore it is not a set, while arr2 is a set.

int arr [8] = {16, 2, 2, 2, 40, 77, 40, 12071};

int arr2 [5] = {16, 2, 77, 40, 12071};

All the functions should be created in q1.h and q1.cpp files.


- Write a menu function that gives following options:
* Press 1 for creating new array of size N
* Press 2 to check if current array is a set
* Press 3 to add new element to array ensuring that it remains a set
* Press 4 to display array (Recursion)
* Press 5 to extract set from an array
- Explanation for 1:
Take the size from user, and create and array of that size, all data values should be set to -1, to
show that user haven’t provided the data yet.
- Explanation for 2:
Check only the elements that have been added by user, to verify if the properties of set are being
maintained or not.
- Explanation for 3:
Check whether a new element can be added to the array? If so verify if the property of set would
be maintained? then only add it to the array.
- Explanation for 4:
Using recursion, display the elements of the array that have been filled by the user.
- Explanation for 5:
if an array is sent to this function, it verifies if it is already a set, if not, we will create a new
array, copy data while maintaining set property of uniqueness, and return that array.
// Paste your code here
// task 01
#include <iostream>// library
#include <string>// library
#include <algorithm>// library
using namespace std;// cout library

struct Student {// struct


int Rollnumber;// declaration
string NAME;// declaration
string CNIC;// declaration
};

void getStudentInformation(Student** students, int numRows, int numCols) {// declaring


function
int totalStudents = numRows * numCols;
*students = new Student[totalStudents];

for (int i = 0; i < totalStudents; i++) {// for loop condition


Student& student = (*students)[i];
cout << "Enter details for student " << i + 1 << ":" << endl;// displaying the output and
moving to next line
cout << "Roll Number: ";// displaying the output
cin >> student.Rollnumber;// input from user
cin.ignore(); // Ignore the newline character left in the input stream

cout << "Name: ";// displaying the output


getline(cin, student.NAME);

cout << "CNIC: ";// displaying the outpu


getline(cin, student.CNIC);

cout << endl;// displaying the output and moving to next line
}
}

void sortStudentsByRoll(Student* students, int numRows, int numCols) {// declaring function
int totalStudents = numRows * numCols;
sort(students, students + totalStudents,
[](const Student& x, const Student& y) {
return x.Rollnumber > y.Rollnumber;// returning
});
}

void sortStudentsByName(Student* students, int numRows, int numCols) {


int totalStudents = numRows * numCols;
sort(students, students + totalStudents,
[](const Student& x, const Student& y) {
return x.NAME < y.NAME;
});
}

void SeatingArrangement(const Student* students, int numRows, int numCols) {// declaring the
function
int totalStudents = numRows * numCols;

for (int i = 0; i < totalStudents; i++) {// for loop condition


const Student& student = students[i];
cout << student.Rollnumber << " - " << student.NAME << " - " << student.CNIC << endl;//
displaying the output and moving to next line
if ((i + 1) % numCols == 0) {
cout << endl; // Start new line for the next row
}
}
}

int main() {// main function


int numRows, numCols;// declaration

cout << "Enter the number of rows: ";// displaying the output
cin >> numRows;// input from user

cout << "Enter the number of columns: ";// displaying the output
cin >> numCols;// input from user

if (numRows <= 0 || numCols <= 0) {// for loop condition


cout << "Invalid number of rows or columns." << endl;// displaying the output
return 0;// end of main function
}

Student* students;
getStudentInformation(&students, numRows, numCols);

sortStudentsByRoll(students, numRows, numCols);


cout << "Students sorted by roll number:" << endl;// displaying the output and moving to next
line
SeatingArrangement(students, numRows, numCols);
cout << endl;// displaying the output and moving to next line

sortStudentsByName(students, numRows, numCols);


cout << "Students sorted by name:" << endl;// displaying the output and moving to next line
SeatingArrangement(students, numRows, numCols);

delete[] students; // Free the allocated memory

return 0;// end of the main


}

// task02
#include <iostream>// preproccessar
using namespace std;// cout library

int findingMinimumContainers(int x, int y) {// declaration


// Base cases
if (x == 0 || y == 0)// using if condition
return 0;//returning
if (x < y)//using if condition
return -1; // Invalid input, cannot fill n-liter tanks with m-liter containers

int count = x / y; // condition


int rem = x % y; // Remaining liters after filling one tank

if (rem == 0)// if condition


return count;// returning
else {//else
int subCount = findingMinimumContainers(rem, y);
if (subCount == -1)// if condition
return -1; // Not possible to fill the tanks
else
return count + subCount;//returning
}
}

int main() {// start of main function


int x, y;// declaration
cout << "Enter the capacity of the m-liter containers: ";// displaying the output
cin >> x;// input from user
cout << "Enter the capacity of the n-liter tanks: ";// displaying the output
cin >> y;// input from user

int minContainers = findingMinimumContainers(x, y);

if (minContainers == -1)// if condition


cout << "It is not possible to fill the n-liter tanks with the given containers." << endl;//
displaying the output
else
cout << "Minimum number of m-liter containers required: " << minContainers << endl;//
displaying the output

return 0;// return


}

//Paste your output here

You might also like