BCSL-032 Solved Free Assignment 2024-25 Sem 3




1. Write a program in C++ for addition of two sparse Polynomials using Pointers. 

Ans:-  Here is a C++ program to add two sparse polynomials using pointers. In this implementation, each term of the polynomial is represented by a node in a linked list.


 C++ Program: Sparse Polynomial Addition Using Pointers


```cpp

#include <iostream>

using namespace std;


// Node structure representing a term in the polynomial

struct Node {

    int coefficient;

    int exponent;

    Node* next;

};


// Function to create a new node for a term in the polynomial

Node* createNode(int coefficient, int exponent) {

    Node* newNode = new Node();

    newNode->coefficient = coefficient;

    newNode->exponent = exponent;

    newNode->next = nullptr;

    return newNode;

}


// Function to insert a new term into the polynomial

void insertTerm(Node*& poly, int coefficient, int exponent) {

    Node* newNode = createNode(coefficient, exponent);

    if (!poly) {

        poly = newNode;

    } else {

        Node* temp = poly;

        while (temp->next) {

            temp = temp->next;

        }

        temp->next = newNode;

    }

}


// Function to display the polynomial

void displayPolynomial(Node* poly) {

    Node* temp = poly;

    while (temp) {

        if (temp->coefficient > 0 && temp != poly) {

            cout << " + ";

        }

        cout << temp->coefficient << "x^" << temp->exponent;

        temp = temp->next;

    }

    cout << endl;

}


// Function to add two sparse polynomials

Node* addPolynomials(Node* poly1, Node* poly2) {

    Node* result = nullptr;

    Node* temp1 = poly1;

    Node* temp2 = poly2;


    while (temp1 && temp2) {

        if (temp1->exponent > temp2->exponent) {

            insertTerm(result, temp1->coefficient, temp1->exponent);

            temp1 = temp1->next;

        } else if (temp1->exponent < temp2->exponent) {

            insertTerm(result, temp2->coefficient, temp2->exponent);

            temp2 = temp2->next;

        } else {

            int sumCoeff = temp1->coefficient + temp2->coefficient;

            if (sumCoeff != 0) {  // Avoid inserting terms with coefficient 0

                insertTerm(result, sumCoeff, temp1->exponent);

            }

            temp1 = temp1->next;

            temp2 = temp2->next;

        }

    }


    // Add remaining terms from poly1

    while (temp1) {

        insertTerm(result, temp1->coefficient, temp1->exponent);

        temp1 = temp1->next;

    }


    // Add remaining terms from poly2

    while (temp2) {

        insertTerm(result, temp2->coefficient, temp2->exponent);

        temp2 = temp2->next;

    }


    return result;

}


// Driver program to demonstrate the addition of two sparse polynomials

int main() {

    Node* poly1 = nullptr;

    Node* poly2 = nullptr;


    // Inserting terms for the first polynomial: 5x^3 + 4x^2 + 2x^1

    insertTerm(poly1, 5, 3);

    insertTerm(poly1, 4, 2);

    insertTerm(poly1, 2, 1);


    // Inserting terms for the second polynomial: 3x^3 + 1x^2 + 7

    insertTerm(poly2, 3, 3);

    insertTerm(poly2, 1, 2);

    insertTerm(poly2, 7, 0);


    cout << "First Polynomial: ";

    displayPolynomial(poly1);


    cout << "Second Polynomial: ";

    displayPolynomial(poly2);


    // Adding the two polynomials

    Node* result = addPolynomials(poly1, poly2);


    cout << "Resultant Polynomial after addition: ";

    displayPolynomial(result);


    return 0;

}

```

 Explanation:

- **Node Structure**: Each term of the polynomial is stored as a node. Each node has a coefficient, an exponent, and a pointer to the next node.

- **insertTerm**: This function inserts a new term into the polynomial linked list.

- **addPolynomials**: This function traverses both polynomials and adds the terms with the same exponents. The result is a new polynomial represented as a linked list.

- **displayPolynomial**: This function displays the polynomial in a readable format.


 Sample Output:

```

First Polynomial: 5x^3 + 4x^2 + 2x^1

Second Polynomial: 3x^3 + 1x^2 + 7x^0

Resultant Polynomial after addition: 8x^3 + 5x^2 + 2x^1 + 7x^0

``


Q2. Write a program in C++ to generate ranks for the candidates based on the marks secured by them in an entrance examination. Make necessary assumptions.\

Ans:-  Here is a C++ program that assigns ranks to candidates based on the marks they secured in an entrance examination. The program will sort the candidates by their marks in descending order and assign ranks accordingly.


 C++ Program: Rank Generation Based on Marks


```cpp

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;


// Structure to represent a candidate

struct Candidate {

    int id;

    string name;

    float marks;

    int rank;

};


// Comparator function to sort candidates by marks in descending order

bool compareByMarks(const Candidate& a, const Candidate& b) {

    return a.marks > b.marks;

}


// Function to assign ranks to candidates based on their marks

void assignRanks(vector<Candidate>& candidates) {

    sort(candidates.begin(), candidates.end(), compareByMarks);


    candidates[0].rank = 1;

    for (size_t i = 1; i < candidates.size(); i++) {

        if (candidates[i].marks == candidates[i-1].marks) {

            candidates[i].rank = candidates[i-1].rank; // Same rank for same marks

        } else {

            candidates[i].rank = i + 1;

        }

    }

}


// Function to display the candidates with their ranks

void displayCandidates(const vector<Candidate>& candidates) {

    cout << "ID\tName\tMarks\tRank" << endl;

    for (const auto& candidate : candidates) {

        cout << candidate.id << "\t" << candidate.name << "\t" << candidate.marks << "\t" << candidate.rank << endl;

    }

}


// Driver function

int main() {

    int n;

    

    // Taking the number of candidates

    cout << "Enter the number of candidates: ";

    cin >> n;

    

    vector<Candidate> candidates(n);


    // Input details for each candidate

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

        cout << "Enter details for candidate " << i + 1 << endl;

        cout << "ID: ";

        cin >> candidates[i].id;

        cout << "Name: ";

        cin >> candidates[i].name;

        cout << "Marks: ";

        cin >> candidates[i].marks;

    }


    // Assign ranks to candidates

    assignRanks(candidates);


    // Display the ranked list of candidates

    cout << "\nCandidates sorted by ranks:\n";

    displayCandidates(candidates);


    return 0;

}

```

 Explanation:

- **Candidate Structure**: Each candidate has an `id`, `name`, `marks`, and `rank`. The `id` represents a unique identifier, `name` is the candidate's name, `marks` are the marks obtained in the exam, and `rank` is the rank assigned based on their marks.

- **assignRanks Function**: The function sorts the candidates based on their marks in descending order. After sorting, ranks are assigned. If two candidates have the same marks, they receive the same rank.

- **compareByMarks Function**: A comparator used to sort the candidates by marks in descending order.

- **displayCandidates Function**: Displays the list of candidates along with their ranks.


### Sample Output:

```

Enter the number of candidates: 5

Enter details for candidate 1

ID: 101

Name: John

Marks: 85.5

Enter details for candidate 2

ID: 102

Name: Alice

Marks: 90.0

Enter details for candidate 3

ID: 103

Name: Bob

Marks: 88.0

Enter details for candidate 4

ID: 104

Name: Eve

Marks: 90.0

Enter details for candidate 5

ID: 105

Name: Charlie

Marks: 75.5


Candidates sorted by ranks:

ID      Name    Marks   Rank

102     Alice   90      1

104     Eve     90      1

103     Bob     88      3

101     John    85.5    4

105     Charlie 75.5    5

```

 Assumptions:

- The input is taken from the user for each candidate, including their ID, name, and marks.

- Two or more candidates with the same marks receive the same rank.

- Sorting is done in descending order of marks.


Q3. Write a program in C++ to create a book of 10 input pages. Make necessary assumptions. 

Ans:-   Here’s a C++ program to simulate the creation of a "book" with 10 pages. Each page will contain some text, which the user can input. We'll assume each page is stored as a string and that the book is represented as an array or vector of strings.


 C++ Program: Book with 10 Pages


```cpp

#include <iostream>

#include <vector>

#include <string>

using namespace std;


// Function to display the book content

void displayBook(const vector<string>& book) {

    cout << "\nDisplaying the Book with 10 Pages:\n";

    for (size_t i = 0; i < book.size(); i++) {

        cout << "Page " << i + 1 << ":\n";

        cout << book[i] << endl << endl;

    }

}


// Driver function

int main() {

    vector<string> book(10);  // A vector of 10 pages (strings)

    

    cout << "Enter the content of the book (10 pages):\n";

    

    // Taking input for each page

    for (int i = 0; i < 10; i++) {

        cout << "Enter content for Page " << i + 1 << ":\n";

        cin.ignore(); // Ignore newline character from previous input

        getline(cin, book[i]);  // Input a full line of text (for each page)

    }


    // Display the book content

    displayBook(book);


    return 0;

}

```

Explanation:

- **book**: A vector of 10 strings, where each string represents a page of the book.

- **getline**: Used to input multi-word text for each page.

- **displayBook Function**: Displays each page's content in the book.

  

 Sample Output:


```

Enter the content of the book (10 pages):

Enter content for Page 1:

Once upon a time...

Enter content for Page 2:

In a distant land...

Enter content for Page 3:

A hero emerged from the shadows...

...

Enter content for Page 10:

And they lived happily ever after.


Displaying the Book with 10 Pages:

Page 1:

Once upon a time...


Page 2:

In a distant land...


Page 3:

A hero emerged from the shadows...


...

Page 10:

And they lived happily ever after.

```

 Assumptions:

- The book consists of exactly 10 pages.

- Each page can contain multi-line text.

- Users input the content for each page in sequence.



No comments: