BCSL-032 C++Programming Lab
BCSL-032 C++Programming Lab
Write a program in C++ for addition of two sparse Polynomials using Pointers
Ans
Code C++
#include <iostream>
struct Node {
newNode->coeff = coeff;
newNode->power = power;
newNode->next = nullptr;
return newNode;
// If the list is empty or power is greater than the first node's power
if (!poly || poly->power < power) {
newNode->next = poly;
poly = newNode;
} else {
temp = temp->next;
if (temp->power == power) {
temp->coeff += coeff;
} else {
newNode->next = temp->next;
temp->next = newNode;
poly1 = poly1->next;
} else if (poly2->power > poly1->power) {
poly1 = poly1->next;
poly2 = poly2->next;
}
while (poly1) {
poly1 = poly1->next;
}
while (poly2) {
poly2 = poly2->next;
return result;
while (poly) {
if (poly) {
cout << " + ";
}
// Main function
int main() {
insertTerm(poly1, 2, 0);
insertTerm(poly2, 3, 3);
insertTerm(poly2, 4, 1);
insertTerm(poly2, 6, 0);
printPolynomial(poly1);
printPolynomial(poly2);
return 0;
}
Explanation:
1. Node Structure: Represents each term in the polynomial, containing the coefficient,
power, and a pointer to the next node.
2. createNode Function: Allocates memory for a new node and initializes it with the
given coefficient and power.
3. insertTerm Function: Inserts a term in the correct position in the polynomial. If the
term with the same power exists, it adds the coefficients.
Sample Output:
This code will work correctly for adding sparse polynomials using pointers.
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's a C++ program to generate ranks for candidates based on the marks they secured in an
entrance examination:
Code C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
};
cout << candidate.rank << "\t" << candidate.id << "\t" << candidate.name << "\t" <<
candidate.marks << endl;
// Main function
int main() {
int numCandidates;
vector<Candidate> candidates(numCandidates);
cout << "Enter details for candidate " << i + 1 << ":\n";
printCandidates(candidates);
return 0;
}
Explanation:
1. Candidate Structure:
2. compareByMarks Function:
3. assignRanks Function:
o First, it sorts the list of candidates based on their marks using the
compareByMarks function.
o Then, it assigns ranks to the candidates. If two or more candidates have the
same marks, they are given the same rank.
4. printCandidates Function:
o Prints the list of candidates along with their rank, ID, name, and marks in a
tabular format.
5. Main Function:
o Prompts the user to input the number of candidates and their details (ID, name,
marks).
o Calls the assignRanks function to determine the ranks.
Sample Output:
ID: 101
Name: Alice
Marks: 95
ID: 102
Name: Bob
Marks: 88
Enter details for candidate 3:
ID: 103
Name: Charlie
Marks: 95
ID: 104
Name: David
Marks: 82
ID: 105
Name: Eva
Marks: 88
1 101 Alice 95
1 103 Charlie 95
3 102 Bob 88
3 105 Eva 88
5 104 David 82
In this example:
• Alice and Charlie both have the highest marks, so they share the 1st rank.
• Bob and Eva have the next highest marks, so they share the 3rd rank.
This program assumes that the number of candidates and their details (ID, name, and marks)
are input by the user. The program handles ties by assigning the same rank to candidates with
equal marks.
Q3. Write a program in C++ to create a book of 10 input pages. Make necessary assumptions.
Ans
Here's a C++ program that simulates creating a book of 10 pages. Each page will have a page
number and some content entered by the user.
Code C++
#include <iostream>
#include <string>
class Page {
public:
int pageNumber;
string content;
pageNumber = num;
content = cont;
void displayPage() {
cout << "Page " << pageNumber << ": " << content << endl;
};
class Book {
public:
void createBook() {
string content;
cout << "Enter content for page " << i + 1 << ": ";
getline(cin, content); // Get page content from the user
pages[i].setPage(i + 1, content);
void displayBook() {
pages[i].displayPage();
};
int main() {
Book myBook;
Explanation:
1. Page Class:
2. Book Class:
o createBook() method asks the user for content for each of the 10 pages.
Assumptions:
• The book is displayed with the page number and corresponding content.