In: Computer Science
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions below main.
1. Write a function harmonicMeans that repeatedly asks the user for two int values until at least one of them is 0. For each pair, the function should calculate and display the harmonic mean of the numbers. The harmonic mean of the numbers is the inverse of the average of the inverses. The harmonic mean of x and y can be calculated as harmonic_mean = 2.0 * x * y / (x + y);. Sample output: Enter two integer values: 2 4 The harmonic mean of 2 and 4 is 2.66667 Enter two integer values: 2 6 The harmonic mean of 2 and 6 is 3 Enter two integer values: 2 9 The harmonic mean of 2 and 9 is 3.27273 Enter two integer values: 2 0
2. Write a function named capEs that takes a string reference as a parameter and changes every 'e' character to 'E'. The function should not return a value. Expected output: Hello Ernie! with cap Es: HEllo ErniE! Eeeee Eee Eeee with cap Es: EEEEE EEE EEEE
3. Write a function named binaryToDecimal that accepts an integer parameter whose digits are meant to represent binary (base-2) digits, and returns an integer of that number's representation in decimal (base-10). For example, the call of binaryToDecimal(101011) should return 43. Constraints: Do not use a string in your solution. Do not use any container such as array or vector. Also do not use any built-in base conversion functions from the system libraries.
4. Write a function named removeConsecutiveDuplicates that accepts as a parameter a reference to a vector of integers, and modifies it by removing any consecutive duplicates. For example, if a vector named v stores {1, 2, 2, 3, 2, 2, 3}, the call of removeConsecutiveDuplicates(v); should modify it to store {1, 2, 3, 2, 3}. Note: to remove an element at index i in a vector named v, use this: v.erase(v.begin() + i);
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
// Code for the problems
using namespace std;
int main() {
// problem 1 test
harmonicMeans();
cout << endl;
// problem 2 tests
string testString1 = "Hello Ernie!";
cout << testString1 << " with cap Es: ";
capEs(testString1);
cout << testString1 << endl;
testString1 = "Eeeee Eee Eeee";
cout << testString1 << " with cap Es: ";
capEs(testString1);
cout << testString1 << endl << endl;;
// problem 3 tests
int n = 101100;
cout << n << " in binary: " << binaryToDecimal(n) << endl;
n = 1000;
cout << n << " in binary: " << binaryToDecimal(n) << endl << endl;
// problem 4 tests
vector<int> v{ 1, 2, 2, 3, 2, 2, 3 };
cout << "before removeConsecutiveDuplicates: ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ' ';
}
removeConsecutiveDuplicates(v);
cout << endl << "after removeConsecutiveDuplicates: ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ' ';
}
cout << endl;
vector<int> w{ 11, 11, 11, 44, 44, 0, 29, 33, 33, 33, 33, 33 };
cout << "before removeConsecutiveDuplicates: ";
for (int i = 0; i < w.size(); ++i) {
cout << w[i] << ' ';
}
removeConsecutiveDuplicates(w);
cout << endl << "after removeConsecutiveDuplicates: ";
for (int i = 0; i < w.size(); ++i) {
cout << w[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}
Please go through code and output.
CODE:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//1
void harmonicMeans(void)
{
int x = -1, y= -1;
while(1)
{
cout << "Enter two numbers: ";
cin >> x >> y;
if(x == 0 || y == 0)
return ;
cout << "harmonic_mean = " << (2.0 * (double)x * (double)y / (double)(x + y)) << endl;
}
}
//2
void capEs(string &str)
{
for(int i=0; i<str.length(); i++)
{
if(str[i] == 'e')
{
str[i] = 'E';
}
}
}
//3
int binaryToDecimal(int binary_digits)
{
int array[32] = {0};
int number = 0;
int temp = 0;
for(int i=0; binary_digits != 0; i++) // convert bin to dec
{
number |= (binary_digits%10) << i; // add last number
binary_digits /= 10; // remove last number
}
return number;
}
//4
void removeConsecutiveDuplicates(vector <int>& v)
{
cout << endl;
for(int i=0; i<v.size(); i++)
{
for(int j=i+1; j< v.size(); j++)
{
if(v[i] == v[j]) // find duplicate element
{
v.erase(v.begin() + j); // remove element
j--;
}
}
}
}
int main() {
// problem 1 test
harmonicMeans();
cout << endl;
// problem 2 tests
string testString1 = "Hello Ernie!";
cout << testString1 << " with cap Es: ";
capEs(testString1);
cout << testString1 << endl;
testString1 = "Eeeee Eee Eeee";
cout << testString1 << " with cap Es: ";
capEs(testString1);
cout << testString1 << endl << endl;;
// problem 3 tests
int n = 101100;
cout << n << " in binary: " << binaryToDecimal(n) << endl;
n = 1000;
cout << n << " in binary: " << binaryToDecimal(n) << endl << endl;
// problem 4 tests
vector<int> v{ 1, 2, 2, 3, 2, 2, 3 };
cout << "before removeConsecutiveDuplicates: ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ' ';
}
removeConsecutiveDuplicates(v);
cout << endl << "after removeConsecutiveDuplicates: ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ' ';
}
cout << endl;
vector<int> w{ 11, 11, 11, 44, 44, 0, 29, 33, 33, 33, 33, 33 };
cout << "before removeConsecutiveDuplicates: ";
for (int i = 0; i < w.size(); ++i) {
cout << w[i] << ' ';
}
removeConsecutiveDuplicates(w);
cout << endl << "after removeConsecutiveDuplicates: ";
for (int i = 0; i < w.size(); ++i) {
cout << w[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}
OUTPUT: