C++ assignment 3 (Deepti)
C++ assignment 3 (Deepti)
Submitted by:
DEEPTI
(230421)
Section-A
1. Copy the contents of one text file to another file, after removing
all whitespaces.
#include <iostream>
#include <fstream>
#include <string>
int main() {
// Input and output filenames
string inputFile, outputFile;
// Open files
ifstream inputStream(inputFile);
ofstream outputStream(outputFile);
if (!outputStream.is_open()) {
cerr << "Error opening output file: " << outputFile << endl;
return 1;
}
string line;
string newLine;
return 0;
}
2. Count the number of words in the given text file. Words can be
separated by space(s), or tab(s).
#include <iostream>
#include <fstream>
#include <string>
int main() {
// Input filename
string filename;
string line;
int wordCount = 0;
// Close file
file.close();
cout << "The text file contains " << wordCount << " words." << endl;
return 0;
}
#include <iostream>
#include <string>
class Person {
public:
Person(const std::string& name) : name(name) {}
void display() {
std::cout << "Name: " << name << std::endl;
}
private:
std::string name;
};
private:
std::string course;
int marks;
};
int main() {
Student student("Alice", "Computer Science", 85);
student.display();
return 0;
}
4. Create a Triangle class. Add exception handling statements to
ensure the following conditions: all sides are greater than 0, and
the sum of any two sides is greater than the third. The class
should also have overloaded functions for calculating the area of a
right-angled triangle and using Heron’s formula to calculate the
area of any type of triangle.
#include <iostream>
#include <cmath>
class Triangle {
public:
Triangle(double side1, double side2, double side3) {
validateSides(side1, side2, side3);
sides[0] = side1;
sides[1] = side2;
sides[2] = side3;
}
double sides[3];
};
int main() {
try {
Triangle triangle(3, 4, 5);
std::cout << "Area (Heron's formula): " << triangle.calculateArea() << std::endl;
return 0;
}
#include <iostream>
#include <string>
// Triangle class
class Triangle : public Shape {
public:
Triangle(double base, double height) : m_base(base), m_height(height) {}
private:
double m_base, m_height;
};
// Rectangle class
class Rectangle : public Shape {
public:
Rectangle(double width, double height) : m_width(width), m_height(height) {}
private:
double m_width, m_height;
};
// Circle class
class Circle : public Shape {
public:
Circle(double radius) : m_radius(radius) {}
double area() const override { return 3.14159 * m_radius * m_radius; }
private:
double m_radius;
};
int main() {
Triangle triangle(5, 3);
Rectangle rectangle(4, 6);
Circle circle(2);
triangle.printInfo();
cout << "Triangle Area: " << triangle.area() << endl;
rectangle.printInfo();
cout << "Rectangle Area: " << rectangle.area() << endl;
circle.printInfo();
cout << "Circle Area: " << circle.area() << endl;
return 0;
}
6. Create a template function findPos() that accepts an array, the
size of the array and a key element. The function finds the key in
the given array and returns the index value if the key is found else
returns -1. Show the implementation with int, double and string
datatypes.
#include <iostream>
#include <string>
int main() {
// Example with int array
int intArr[] = {10, 20, 30, 40, 50};
int intKey = 30;
int intPos = findPos(intArr, 5, intKey);
if (intPos != -1) {
cout << "Key " << intKey << " found at index " << intPos << " in intArr." << endl;
} else {
cout << "Key " << intKey << " not found in intArr." << endl;
}
#include <iostream>
#include <vector>
// Function to add two vectors (assuming they have the same size)
Vector<T> operator+(const Vector<T>& other) const {
if (data.size() != other.data.size()) {
std::cerr << "Error: Vectors must have the same size for addition." << std::endl;
return Vector<T>(0); // Return empty vector on error
}
Vector<T> result(data.size());
for (int i = 0; i < data.size(); ++i) {
result.data[i] = data[i] + other.data[i];
}
return result;
}
private:
std::vector<T> data;
};
int main() {
int size;
std::cout << "Enter size of the vectors: ";
std::cin >> size;
// Vector of integers
Vector<int> intVec(size);
std::cout << "Enter elements for integer vector:\n";
intVec.input();
// Vector of doubles
Vector<double> doubleVec(size);
std::cout << "Enter elements for double vector:\n";
doubleVec.input();
if (intResult.data.size() > 0) {
std::cout << "Sum of integer vectors: ";
intResult.display();
}
if (doubleResult.data.size() > 0) {
std::cout << "Sum of double vectors: ";
doubleResult.display();
}
return 0;
}