0% found this document useful (0 votes)
13 views5 pages

DSALab 04

Uploaded by

itshunainkhan121
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)
13 views5 pages

DSALab 04

Uploaded by

itshunainkhan121
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/ 5

Name :

Class (SE/ AI/ CS):


Roll No. :

Note that after 16 Nov 2024 Your marks will be deducted.


From Activity 1 to 5 Complete all lab manual with answer

DSA Lab 04

LAB Objectives: Development of Polynomial class with Input, output, Evaluate,


Add, Sub, Differentiate, Integrate, methods. Techniques for root finding.

Activity 1
Write polynomial class
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>

class Term {
public:
int coefficient;
int exponent;

Term(int coeff, int exp) : coefficient(coeff), exponent(exp) {}


};

class Polynomial {
private:
std::vector<Term> terms;

public:
// Add a term to the polynomial
void addTerm(int coefficient, int exponent) {
for (size_t i = 0; i < terms.size(); ++i) {
if (terms[i].exponent == exponent) {
terms[i].coefficient += coefficient;
if (terms[i].coefficient == 0) {
// Remove the term if the coefficient becomes zero
terms.erase(terms.begin() + i);
}
return;
}
}
// Otherwise, add a new term
terms.push_back(Term(coefficient, exponent));
}

// Display the polynomial


void display() const {
if (terms.empty()) {
std::cout << "0" << std::endl;
return;
}

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


if (i > 0 && terms[i].coefficient > 0) {
std::cout << "+";
}
std::cout << terms[i].coefficient << "x^" << terms[i].exponent << " ";
}
std::cout << std::endl;
}
};

int main() {
Polynomial polynomial;
int numTerms, coeff, exp;

std::cout << "Enter the number of terms in the polynomial: ";


std::cin >> numTerms;

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


std::cout << "Enter coefficient and exponent for term " << (i + 1) << ": ";
std::cin >> coeff >> exp;
polynomial.addTerm(coeff, exp);
}

std::cout << "The polynomial is: ";


polynomial.display();

return 0;
}
Result
Enter the number of terms in the polynomial: 3
Enter coefficient and exponent for term 1: 9 2
Enter coefficient and exponent for term 2: 5 7
Enter coefficient and exponent for term 3: 4 2
The polynomial is: 13x^2 +5x^7

Activity 2

Write a function of factorial

#include <iostream>

int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}

int main() {
int number;

std::cout << "Enter a positive integer: ";


std::cin >> number;

if (number < 0) {
std::cout << "Factorial is not defined for negative numbers." << std::endl;
} else {
std::cout << "Factorial of " << number << " is " << factorial(number) <<
std::endl;
}

return 0;
}

Activity 3

Write a function of fibonacci series

#include <iostream>

void fibonacci(int n) {
int a = 0, b = 1, nextTerm;
std::cout << "Fibonacci Sequence up to " << n << " terms:" << std::endl;
for (int i = 1; i <= n; ++i) {
std::cout << a << " ";
nextTerm = a + b;
a = b;
b = nextTerm;
}
std::cout << std::endl;
}

int main() {
int numTerms;

std::cout << "Enter the number of terms in the Fibonacci sequence: ";
std::cin >> numTerms;

if (numTerms <= 0) {
std::cout << "Please enter a positive integer." << std::endl;
} else {
fibonacci(numTerms);
}

return 0;
}

Activity 4

Write a function for Euclidean Algorithm

#include <iostream>

// Function to calculate GCD using the Euclidean algorithm


int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}

int main() {
int num1, num2;

std::cout << "Enter two integers: ";


std::cin >> num1 >> num2;

int result = gcd(num1, num2);

std::cout << "The GCD of " << num1 << " and " << num2 << " is: " << result
<< std::endl;

return 0;
}

Activity 5

Graded Lab

1. Write a program for factorial of 6


2. Write a Polynomial of y2-5y+11

You might also like