Question

In: Computer Science

Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions...

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;

}

Solutions

Expert Solution

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:


Related Solutions

C PROGRAM ONLY! You should make two functions. Prototypes are included in loops.h. : #ifndef FUNCTIONS_H...
C PROGRAM ONLY! You should make two functions. Prototypes are included in loops.h. : #ifndef FUNCTIONS_H #define FUNCTIONS_H void mult_table(int n); int num_digits(int n); #endif mult_table should accept an integer. It should PRINT the multiplication table for that number up to 10. Example: if N was 3, your function should PRINT 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 ... 3 * 10 = 30 num_digits should accept an integer. It should RETURN...
C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list operations int getLength() const {return length;} void insertNode(College); bool deleteNode(string); void displayList() const; bool searchList(string, College &) const; }; main.cpp /*   Build and procees a sorted linked list of College objects. The list is sorted in ascending order by the college code. Assume that the college code is unique. */ #include <iostream> #include <fstream> #include <string> #include "LinkedList.h" using namespace std; void buildList(const string...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following is a partial implementation of Set class. You are required to enhance and implement the following missing functions from the implementation: A) UNION B) reset C) intersection D) difference A SAMPLE driver program : int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2); Set s4 = s1.intersection(s2); Set s5 = s1.difference(s2); s3.print("s3"); s4.print("s4");...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them...
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them on your stack. Also, print them. Read a character from the keyboard. If the character is an “o” then pop from the stack but don’t print. If the character is a “p”, then pop from the stack and print that number. If the character is an “e” or the stack is empty, end the program.
C++ program to perform each of the area calculations in separate functions. Your program will take...
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop...
Use the Excel time value of money functions to complete the following problems. Highlight your answers....
Use the Excel time value of money functions to complete the following problems. Highlight your answers. Upload your solution on blackboard. 1) Manny’s grandparents gave him $1,800 for his birthday. He opened a savings account that pays 4% annually. How much money will he have in 7 years if he does not make any withdrawals? N= I= PV= PMT= FV= 2) Your parents will retire in 25 years. They currently have $100,000 in savings. They think they will need $1,000,000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT