Question: Question 1 1. A letter's frequency Develop a function named oneLetterFrequency that: takes in a word (std::string type) and a letter (char type) as parameters,
Question 1
1. A letter's frequency
Develop a function named oneLetterFrequency that:
- takes in a word (std::string type) and a letter (char type) as parameters,
- returns number of occurrence of that letter in the given word.
For example:
- oneLetterFrequency("hello", 'l') should return 2, as letter l appears twice in word "hello".
- oneLetterFrequency("hello, how are you Oliver007?") should return 3, as letter o appears three times in the given word.
- oneLetterFrequency("abcdefg", 'h') should return 0, as letter h is not in the given word.
- Please use provided source code to finish each step, then mark the lab question as completed.
-
step 1
Expected output:
2
Given by Sir :
// file: letterFreq.cpp
#include
#include
// TODO: define the prototype of required function.
int main(){
// step 1: uncomment following code snippet to call function oneLetterFrequency()
// expected output: 2
//std::cout << oneLetterFrequency("hello", 'l') << std::endl;
// step 2: uncomment following code snippet to call function oneLetterFrequency()
// expected output: 3
// std::cout << oneLetterFrequency("hello, how are you Oliver007?", 'o') << std::endl;
// // step 3: uncomment following code snippet to call function oneLetterFrequency()
// // expected output: 0
// std::cout << oneLetterFrequency("abcdefg", 'h') << std::endl;
return 0;
}
// TODO: implment require function.
Question 2:
2. Debug Exercise
Your instructor deliberately placed several syntax errors and/or logical errors in the given source code. These bugs need to be fixed so the program can convert a non-negative decimal integer to its corresponding binary number.
Please fix these bugs, then use Check it! to harvest points, and mark the question as completed.
step 1
Expected output:
110001 1011110 1001101
Given by Sir:
// File: bugs.cpp
// Name: Huabo Lu
// Date: 01/26/2022
#include
#include
#include
std::string D2BConverter(int num) {}
int main(){
// expected output : 110001
std::cout << D2BConverter(49) << std::endl;
// expected output : 1011110
std::cout << D2BConverter(94) << std::endl;
// expected output : 1001101
std::cout << D2BConverter(77) << std::endl;
return 0;
}
std::string D2BConverter(int num){
std:::string r_result{};
while(num != 1){
r_result = std::to_string(num / 2);
num /= 2;
}
return std::reverse(r_result.begin(), r_result.end());
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
