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 } } ...
Friday, March 17, 2023
Write a function template for finding the minimum value contained in an array in C++ program.
Niraj shrestha
March 17, 2023
0
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...
Wednesday, March 15, 2023
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.
Niraj shrestha
March 15, 2023
0
(adsbygoogle = window.adsbygoogle || []).push({});
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...
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.
Niraj shrestha
March 15, 2023
0
(adsbygoogle = window.adsbygoogle || []).push({});
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; ...
Write a program to find the value of e in the following series: e=1+1/1!+1/2!+......... upto acc = 0.0001
Niraj shrestha
March 15, 2023
0
(adsbygoogle = window.adsbygoogle || []).push({});
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;...
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.
Niraj shrestha
March 15, 2023
0
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)...
Write a program to compute the area of a triangle and a circle by overloading the area() function.
Niraj shrestha
March 15, 2023
0
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 trianglefloat area(float base, float height) { return 0.5 * base * height;}// Function to calculate area of circlefloat area(float radius) { return 3.14 * radius * radius;}int main() { float base, height, radius; //...