Question
C++ question
why I have these errors
Problem statement
In this question first we will practice function overloading and then function templates. Please follow below instructions.
- We can extend multiplication easily for string types if we interpret the operation as repetition. For example "code" * 3 may be interpreted as "codecodecode". In fact, languages like PythonLinks to an external site. already support this operation. Write a C++ function named multiply that can multiply(repeat) an std::string by a given integer number and return the repeated string.
- Write another C++ function named multiply that can multiply two given integer (int type) numbers and return the product as an integer.
- Write another C++ function with the same name that can multiply a floating point number (double type) by a given integer number and return the product as a floating point number.
- We defined three functions with the same name without a problem. It is either because they have a different number of parameters, or because any of their parameters are of a different type. This allows the compiler to recognize which one to consider; each function is unique though the names are same.
- What if we can write a single function that can handle all three data types and give the results accordingly? We can do so using C++ function templates. Write a C++ function template named multiply_type that can take any floating point number(double type) or integer(int type) or an std::string as the first parameter and any integer number as the second and returns the respective product of the two.
Do the following
- Write your algorithm as code comments. I recommend to follow UMPIRE technique
- Implement your functions
- In your driver program, test your function for the criteria given below.
Criteria
Test # | input | Output |
---|---|---|
1 | 10 10 |
100 |
2 | 4 6 |
24 |
3 | 2.8 10 |
28.0 |
5 | "C++" 10 |
"C++C++C++C++C++C++C++C++C++C++" |
6 | "Rain" 2 |
"RainRain" |
![### C++ Code Implementation Tutorial
#### Code Implementation in `multiply.cpp`
Below are the definitions of three different `multiply` functions using C++ template programming. They are defined for strings, integers, and doubles.
```cpp
// Replace ... with your code. Hit enter key to get more vertical space
// IMPLEMENT
#include "multiply.h"
std::string multiply(std::string string1, int number){
return_value = T();
for (int i = 0; i < number; ++i) {
return_value += string1;
}
return return_value;
}
int multiply(int number1, int number2){
return number1 * number2;
}
double multiply(double number1, int number2){
return number1 * number2;
}
```
#### Header File `multiply.h`
The `multiply.h` file contains declarations for the previously defined functions, along with a template function for generic types.
```cpp
// IMPLEMENT
#ifndef MULTIPLY_H
#define MULTIPLY_H
#include <string>
std::string multiply(std::string string1, int number);
int multiply(int number1, int number2);
double multiply(double number1, int number2);
// In templates, we can't separate the declaration from the definition; We have to write it in a single location.
template<class T>
T multiply_type(T entity1, int number){
// Write your code below
// You may need to initialize your return value like,
// T return_value = T();
T return_value = T();
for (int i = 0; i < number; ++i) {
return_value += entity1;
}
return return_value;
}
#endif // MULTIPLY_H
```
#### Driver File `multiply_driver.cpp`
Below is a driver program for testing the `multiply` functions defined earlier.
```cpp
// REVIEW
#include <iostream>
#include "multiply.h"
// Replace ... with your code. Hit enter key to get more vertical space
int main(int argc, char* argv[]){
// Testing multiply
}
```
### Explanation
1. **`multiply.cpp`**:
- This file contains the implementation of the `multiply` functions.
- The first function multiplies a string by an integer.
- The second function multiplies two integers.
- The third function multiplies a double by an integer.
2. **`multiply.h`**:
-](https://content.bartleby.com/qna-images/question/69991f76-d6e5-4f32-9883-cbbc5a2c7e26/0d72cd75-5fa9-4f8c-91b3-b11dab353912/xl9oouo_thumbnail.png)
Transcribed Image Text:### C++ Code Implementation Tutorial
#### Code Implementation in `multiply.cpp`
Below are the definitions of three different `multiply` functions using C++ template programming. They are defined for strings, integers, and doubles.
```cpp
// Replace ... with your code. Hit enter key to get more vertical space
// IMPLEMENT
#include "multiply.h"
std::string multiply(std::string string1, int number){
return_value = T();
for (int i = 0; i < number; ++i) {
return_value += string1;
}
return return_value;
}
int multiply(int number1, int number2){
return number1 * number2;
}
double multiply(double number1, int number2){
return number1 * number2;
}
```
#### Header File `multiply.h`
The `multiply.h` file contains declarations for the previously defined functions, along with a template function for generic types.
```cpp
// IMPLEMENT
#ifndef MULTIPLY_H
#define MULTIPLY_H
#include <string>
std::string multiply(std::string string1, int number);
int multiply(int number1, int number2);
double multiply(double number1, int number2);
// In templates, we can't separate the declaration from the definition; We have to write it in a single location.
template<class T>
T multiply_type(T entity1, int number){
// Write your code below
// You may need to initialize your return value like,
// T return_value = T();
T return_value = T();
for (int i = 0; i < number; ++i) {
return_value += entity1;
}
return return_value;
}
#endif // MULTIPLY_H
```
#### Driver File `multiply_driver.cpp`
Below is a driver program for testing the `multiply` functions defined earlier.
```cpp
// REVIEW
#include <iostream>
#include "multiply.h"
// Replace ... with your code. Hit enter key to get more vertical space
int main(int argc, char* argv[]){
// Testing multiply
}
```
### Explanation
1. **`multiply.cpp`**:
- This file contains the implementation of the `multiply` functions.
- The first function multiplies a string by an integer.
- The second function multiplies two integers.
- The third function multiplies a double by an integer.
2. **`multiply.h`**:
-
![**Understanding multiply_driver.cpp**
```cpp
// multiply_driver.cpp
#include <iostream>
#include "multiply.h"
// Replace ... with your code. Hit enter key to get more vertical space
int main(int argc, char *argv[]) {
// Testing multiply
std::cout << multiply(10, 10) << std::endl;
/* By exactly following the pattern above, test your function below for rest of the test cases
in the order they are given*/
std::cout << multiply(4, 6) << std::endl;
std::cout << multiply(2.8, 10) << std::endl;
std::cout << multiply("C++", 10) << std::endl;
std::cout << multiply("Rain", 2) << std::endl;
std::cout << "Testing template function to prove the ability to write generic (type independent) code " << std::endl;
std::cout << multiply_type<int>(10, 10) << std::endl;
/* By exactly following the pattern above, test your function below for rest of the test cases
in the order they are given*/
std::cout << multiply_type<int>(4, 6) << std::endl;
std::cout << multiply_type<double>(2.8, 10) << std::endl;
std::cout << multiply_type<std::string>("C++", 10) << std::endl;
std::cout << multiply_type<std::string>("Rain", 2) << std::endl;
return 0;
}
```
### Running multiply_driver.cpp
### Error:
```
In file included from multiply_driver.cpp:3:
multiply.h: In function ‘T multiply_type(T, int)’:
multiply.h:18:12: error: ‘value’ was not declared in this scope
18 | return value += string1;
| ^~~~~
multiply.h:18:21: error: ‘string1’ was not declared in this scope; did you mean ‘stdin’?
18 | return value += string1;
| ^~~~~~~
| stdin
multiply.h:20:9: error: expected primary-expression before ‘return’
20 | return return value;
| ^~~~~~~
```
### Explanation:
The provided C++ code, `multiply_driver.cpp`, tests two functions: `multiply` and](https://content.bartleby.com/qna-images/question/69991f76-d6e5-4f32-9883-cbbc5a2c7e26/0d72cd75-5fa9-4f8c-91b3-b11dab353912/h9tsauh_thumbnail.png)
Transcribed Image Text:**Understanding multiply_driver.cpp**
```cpp
// multiply_driver.cpp
#include <iostream>
#include "multiply.h"
// Replace ... with your code. Hit enter key to get more vertical space
int main(int argc, char *argv[]) {
// Testing multiply
std::cout << multiply(10, 10) << std::endl;
/* By exactly following the pattern above, test your function below for rest of the test cases
in the order they are given*/
std::cout << multiply(4, 6) << std::endl;
std::cout << multiply(2.8, 10) << std::endl;
std::cout << multiply("C++", 10) << std::endl;
std::cout << multiply("Rain", 2) << std::endl;
std::cout << "Testing template function to prove the ability to write generic (type independent) code " << std::endl;
std::cout << multiply_type<int>(10, 10) << std::endl;
/* By exactly following the pattern above, test your function below for rest of the test cases
in the order they are given*/
std::cout << multiply_type<int>(4, 6) << std::endl;
std::cout << multiply_type<double>(2.8, 10) << std::endl;
std::cout << multiply_type<std::string>("C++", 10) << std::endl;
std::cout << multiply_type<std::string>("Rain", 2) << std::endl;
return 0;
}
```
### Running multiply_driver.cpp
### Error:
```
In file included from multiply_driver.cpp:3:
multiply.h: In function ‘T multiply_type(T, int)’:
multiply.h:18:12: error: ‘value’ was not declared in this scope
18 | return value += string1;
| ^~~~~
multiply.h:18:21: error: ‘string1’ was not declared in this scope; did you mean ‘stdin’?
18 | return value += string1;
| ^~~~~~~
| stdin
multiply.h:20:9: error: expected primary-expression before ‘return’
20 | return return value;
| ^~~~~~~
```
### Explanation:
The provided C++ code, `multiply_driver.cpp`, tests two functions: `multiply` and
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 4 images

Knowledge Booster
Similar questions
- Question 2: multiply Problem statement In this question first we will practice function overloading and then function templates. Please follow below instructions. We can extend multiplication easily for string types if we interpret the operation as repetition. For example "code" * 3 may be interpreted as "codecodecode". In fact, languages like Python already support this operation. Write a C++ function named multiply that can multiply(repeat) an std::string by a given integer number and return the repeated string. Write another C++ function named multiply that can multiply two given integer (int type) numbers and return the product as an integer. Write another C++ function with the same name that can multiply a floating point number (double type) by a given integer number and return the product as a floating point number. We defined three functions with the same name without a problem. It is either because they have a different number of parameters, or because any of their…arrow_forwardIn C++ please and thank you!arrow_forwardRegex, APIs, BeautifulSoup: python import requests, refrom pprint import pprintfrom bs4 import BeautifulSoup complete the missing bodies of the functions below: def mathmatic(target):"""Question 1You are doing fun math problems. Given a string of combination of '{}', '[]','()', you are required to return the substring included by the outermost '{}'.You need to write your code in one line.Args:target (str): the string to search inReturns:str>>> mathmatic('{[[[[]()]]]}')'[[[[]()]]]'>>> mathmatic('[(){([{}])}]')'([{}])'"""pass test code: # print(mathmatic('{[[[[]()]]]}'))# print(mathmatic('[(){([{}])}]'))arrow_forward
- Please Help In JAVA Here is the skeleton of a code for insertion sorting in an imperative language. You have to add right lines of codes for the language you choose (C, C++, C#, JAVA etc,). The following sample is for C++. #include <iostream>#include <array>#include <stdio.h>#include <stdlib.h>#include <ctime>using namespace std; void insertionSort(int A[], int n){for (int i = n-2; i >= 0; i--){ int j; int v = A[i]; for (j = i + 1; j <= n-1; j++) { if (A[j] > v) break; else A[j-1] = A[j]; } A[j-1] = v;}} int *randomArray(int n){srand((unsigned) time(0));int * A = new int [n];for (int i = 0; i < n; i++){ A[i] = rand();}return A;} Sample function calls (call these functions in main driver function):int n = 100000;int * A = randomArray(n);insertionSort(A, n);delete [] A; Note: delete memory of first array before sorting second array. After you modify the code: (1) Generate 100, 000 random numbers and sort them (2) Generate 300000…arrow_forwardThe input string is provided as a parameter to the compress_string function. The Challenge: You must build a reference to a function that accepts a single integer parameter and returns an array of pointers to functions that accept a single string input and return an integer.Whew! You have to read the specification at least three times before you realise you don't comprehend it.How do you handle something so complicated.arrow_forwardIt’s due in C++ Please tell me which code is for StudentMaim.c and which for studentInfo.h and studentInfo.carrow_forward
arrow_back_ios
arrow_forward_ios