Questions
COSC 1437 - 15099 - Introduction to Programming
Unknown Question Type
Question textWrite a C++ function that counts how many integers in a vector<int> contain at least one occurrence of the digit given as a parameter. For example, given the following integer vector, and the function call: vector<int> numbers = {1234,4355,6541,8976};int result = count_numbers_with_digit(numbers,4); The returned integer value must be 3 (The values in the vector with at least one 4 are: 1234, 4355, and 6541). The function's header is as follows: int count_numbers_with_digit(const vector<int> & numbers, int digit) This function takes as parameters a vector<int> containing positive integers greater than zero, and a single-digit positive integer representing the digit to find in each number stored in the vector. Notes: You can safely assume that the input is always valid. You do not need to write the main function. A 100% penalty will be applied to solutions that do not compile. A 100% penalty will be applied to solutions that hardcode the output. For example:[table] Test | Result vector<int> numbers = {1234,4355,6541,8976}; int result = count_numbers_with_digit(numbers,4); if (result == 3) { cout << "Correct implementation of the count_numbers_with_digit user-defined function" << endl; } else { cout << "Incorrect implementation of the count_numbers_with_digit user-defined function" << endl; cout << "Returned value: " << result << endl; cout << "Correct value: 3" << endl; } | Correct implementation of the count_numbers_with_digit user-defined function [/table] Answer:(penalty regime: 0 %)#ifndef FUNCTIONS_H #define FUNCTIONS_H #include <iostream> using namespace std; // Write the definition and implementation of the count_numbers_with_digit user-defined function here int count_numbers_with_digit(const vector<int> & numbers, int digit) { int count = 0; for (size_t i = 0; i < numbers.size(); ++i) { int num = numbers[i]; int d = digit; do { if (num % 10 == d) { ++count; break; } num /= 10; } while (num > 0); } return count; } #endif 1234567891011121314151617181920212223242526272829#ifndef FUNCTIONS_H#define FUNCTIONS_H#include <iostream>using namespace std;// Write the definition and implementation of the count_numbers_with_digit user-defined function hereint count_numbers_with_digit(const vector<int> & numbers, int digit){ int count = 0; int count = 0; for (size_t i = 0; i < numbers.size(); ++i) { int num = numbers[i]; int d = digit; do { if (num % 10 == d) { ++count; break; } num /= 10; } while (num > 0); } return count;}#endif ההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Check Question 2

View Explanation
Verified Answer
Please login to view
Step-by-Step Analysis
Let me restate the task and the materials we have to work with to ground the analysis. The prompt asks for a C++ function that counts how many integers in a vector<int> contain at least one occurrence of a specified single-digit parameter. The example given uses the vector {1234, 4355, 6541, 8976} and digit 4, producing 3 because 1234, 4355, and 6541 each contain the digit 4. The function header provided is: int count_numbers_with_digit(const vector<int> & numbers, int digit). The notes specify inputs are positive integers (>0) and digit is a single-digit positive integer; main is not required, and the code must compile. In the material, there is a candidate implementation embedded within a larger HTML-like page; for this analysis I will treat the relevant implementation as the candidate solution to scrutinize.
Option/Code under evaluation:
int count_numbers_with_digit(const vector<int> & numbers, int digit) {
int count = 0;
for (size_t i = 0; i < numbers.size(); ++i) {
int num = numbers[i];
int d = digit;
do {
if (num % 10 == d) {
++count;
break;
}
num /= 10;
} while (num > 0);
}
return count;
}
Now, let’s analyze the approach and each aspect of the solution step by step.
First, the overa......Login to view full explanationLog in for full answers
We've collected over 50,000 authentic exam questions and detailed explanations from around the globe. Log in now and get instant access to the answers!
Similar Questions
Question at position 18 Which of the following produces a vector EvenVals with values [2,4,6,8,10]? Select all correct answers. EvenVals=[1:5]*2;EvenVals=linspace(2,10,5);EvenVals=[2,4:2:6,8,10];EvenVals=2:2:10;
Question textWrite the implementation of the function template compare_vectors. int compare_first_n_values(const vector<T> &vector1 , const vector<T> &vector2, int n) This function receives the following parameters: A reference to a constant vector of type T. A reference to a constant vector of type T. An int value with the number of values to compare (starting from the first position). The function compares the first n values of the vectors, returning the number of equal values. This function does a memberwise comparison (vector1.at(0) == vector2.at(0), vector1.at(1) == vector2.at(1), ..... , vector1.at(n-1) == vector2.at(n-1)). The function must return -1 if the value of n is greater than the size of any of the vectors. NOTES: You can safely assume that the input is always correct. You must use the function identifier from the specifications of the question when writing your solution. You do not need to write a main function. Solutions not compiling will receive a 100 % penalty. Hardcoding the output will translate into a 100 % penalty. For example:[table] Test | Result vector<string> string_vector1 {"Carlos", "Alberto", "Rincon", "Castro"}; vector<string> string_vector2 {"Edmundo", "Jose", "Rincon", "Castro", "Urdaneta"}; cout << compare_first_n_values(string_vector1, string_vector2,4) << endl; | 2 [/table] Answer:(penalty regime: 0 %)template <typename T> int compare_first_n_values(const std::vector<T>& vector1, const std::vector<T>& vector2, int n) { if (n > vector1.size() || n > vector2.size()) { return -1; 1234567template <typename T>int compare_first_n_values(const std::vector<T>& vector1, const std::vector<T>& vector2, int n) { if (n > vector1.size() || n > vector2.size()) { return -1; ההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Check Question 2
Assume that you just opened a new R session. If you execute the following codes one by one (from the top to the bottom), which ones of these would successfully create a vector? There is at least one option which will create a vector successfully, but there might be multiple. If so, you must select all of the ones.
In a consumer society, many adults channel creativity into buying things
More Practical Tools for Students Powered by AI Study Helper
Making Your Study Simpler
Join us and instantly unlock extensive past papers & exclusive solutions to get a head start on your studies!