Skip to main content

Posts

Write a function template to perform linear search in an array in C++ program.

  here's an example program in C++ that uses a function template to perform linear search on an array: #include <iostream> using namespace std; template <typename T> int linearSearch(T arr[], int size, T key) {     for (int i = 0; i < size; i++) {         if (arr[i] == key) {             return i; // Return the index where key was found         }     }     return -1; // Key not found in array } int main() {     int intArr[] = { 1, 2, 3, 4, 5 };     int intSize = sizeof(intArr) / sizeof(intArr[0]);     int intKey = 3;     cout << "Index of " << intKey << " in intArr: " << linearSearch(intArr, intSize, intKey) << endl;     double doubleArr[] = { 1.2, 2.3, 3.4, 4.5, 5.6 };     int doubleSize = sizeof(doubleArr) / sizeof(doubleArr[0]);     double doubleKey = 4.5;   ...

Write a function template for finding the minimum value contained in an array in C++ program.

 here's an example of a function template for finding the minimum value contained in an array in C++: #include <iostream> using namespace std; template<typename T> T findMin(T arr[], int size) {     T minVal = arr[0];     for(int i = 1; i < size; i++) {         if(arr[i] < minVal) {             minVal = arr[i];         }     }     return minVal; } int main() {     int intArr[] = {4, 8, 2, 10, 5};     double doubleArr[] = {2.5, 6.7, 1.2, 4.5};          cout << "Minimum value in intArr: " << findMin(intArr, 5) << endl;     cout << "Minimum value in doubleArr: " << findMin(doubleArr, 4) << endl;     return 0; } In this example, we define a function template findMin that takes an array of any type (T) and its size as parameters. The function iterates through th...

Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.

 To solve this problem, we can follow these steps: Open the input file for reading and the output file for writing. Read the input file line by line. For each line, replace every sequence of consecutive blank spaces with a single space using the std::regex_replace function from the regex library. Write the modified line to the output file. Here is the code: #include <iostream> #include <fstream> #include <regex> using namespace std; int main() {     // Open the input file for reading and the output file for writing     ifstream infile("input.txt");     ofstream outfile("output.txt");     if (!infile) {         cerr << "Error: Could not open input file.\n";         return 1;     }     if (!outfile) {         cerr << "Error: Could not open output file.\n";         return 1;     }     string lin...

Write a program which reads a text from the keyboard and displays the following information on the screen: a) Number of lines b) Number of words c) Number of characters Strings should be left-justified and numbers should be right-justified in suitable field width.

 To solve this problem, we can read the text input from the keyboard using the getline function and then process the text to count the number of lines, words, and characters. We can use the istringstream class to separate the text into words. Here's the program: #include <iostream> #include <sstream> #include <string> using namespace std; int main() {     string text;     int num_lines = 0, num_words = 0, num_chars = 0;     // Read text from keyboard     cout << "Enter text: " << endl;     getline(cin, text);     // Process text     istringstream iss(text);     string word;     while (iss >> word) {         num_words++;         num_chars += word.length();     }     // Count lines     for (char c : text) {         if (c == '\n') {           ...

Write a program to find the value of e in the following series: e=1+1/1!+1/2!+......... upto acc = 0.0001

 To find the value of e in the given series, we need to sum up the terms until the accuracy reaches 0.0001. We can use a loop to add the terms and check for the accuracy. Here's the program to find the value of e: #include <iostream> #include <cmath> using namespace std; int main() {     double e = 1.0, term = 1.0;     int i = 1;     const double acc = 0.0001; // desired accuracy     while (fabs(term) > acc) {         term /= i; // i! = i * (i-1)!, so divide by i to get (i-1)!         e += term;         i++;     }     cout << "The value of e is: " << e << endl;     return 0; } In this program, we initialize the value of e and term to 1.0 and i to 1. We also set the desired accuracy acc to 0.0001. The loop continues until the absolute value of term is less than or equal to acc . In each iteration, we divide term b...

Write a program to read a list containing item name, item code, and cost interactively and produce a three column output. Note that the name and code are left-justified with a precision of two digits.

 Here's a program to read a list containing item name, item code, and cost interactively and produce a three column output: #include <iostream> #include <iomanip> #include <string> using namespace std; int main() {     string name, code;     double cost;     cout << "Enter item name, code, and cost: " << endl;     cin >> name >> code >> cost;     cout << setw(20) << left << "Item Name" << setw(10) << left << "Item Code" << setw(10) << left << "Cost" << endl;     cout << setw(20) << left << name << setw(10) << left << code << setprecision(2) << fixed << cost << endl;     return 0; } Example output: Enter item name, code, and cost: Shirt SH-01 25.99 Item Name           Item Code Cost Shirt        ...

Write a program to compute the area of a triangle and a circle by overloading the area() function.

 Here's an example program that overloads the area() function to calculate the area of a triangle and a circle: #include <iostream> using namespace std; // Function to calculate area of triangle float area(float base, float height) {     return 0.5 * base * height; } // Function to calculate area of circle float area(float radius) {     return 3.14 * radius * radius; } int main() {     float base, height, radius;     // Read input for triangle     cout << "Enter base and height of the triangle: ";     cin >> base >> height;     // Calculate and display area of triangle     float tri_area = area(base, height);     cout << "Area of triangle: " << tri_area << endl;     // Read input for circle     cout << "Enter radius of the circle: ";     cin >> radius;     // Calculate and display area of circle ...