+ 2
Pass by Reference with Pointers
I cant get the two values to multiply You operate a mobile provider running a promotion that multiplies a user's internet bandwidth. Fix the program by completing the function and calling it, so that the given megabyte outputs before and after the promotion work correctly. The multiplier is taken as input inside the multiplier function. Sample Input 5 2 Sample Output Before the promotion: 5 After the promotion: 10 Explanation The first input is the count of megabytes, the second is multiplier. The first outputted line represents the count of megabytes before the function-multiplier call, and the second one - after.
7 odpowiedzi
+ 4
// TITLE: test.cpp
// Date:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
/*complete the function to multiple the megabytes,
don't forget to set the parameter*/
int promotion(int *n)
{
//taking multiplier as input
int multiplier;
cin>>multiplier;
return *n*=multiplier;
}
int main() {
//getting initial count of megabytes
int megabytes,multiplier;
cin >> megabytes;
//printing the count of megabytes before the promotion
cout << "Before the promotion: " << megabytes << endl;
//complete the function call
promotion(&megabytes);
//printing the count of megabytes after the promotion
cout << "After the promotion: " << megabytes << endl;
return 0;
}
+ 3
This is my incorrect code
#include <iostream>
using namespace std;
/*complete the function to multiple the megabytes,
don't forget to set the parameter*/
void promotion(int *x) {
int multiplier;
cin>>multiplier;
*x = multiplier ;
}
int main() {
//getting initial count of megabytes
int megabytes;
cin >> megabytes;
//printing the count of megabytes before the promotion
cout << "Before the promotion: " << megabytes << endl;
//complete the function call
promotion(&megabytes);
//printing the count of megabytes after the promotion
cout << "After the promotion: " << megabytes << endl;
return 0;
}
+ 3
In your multiplier function just do
*x *= multiplier;
+ 3
Here is the code that works for me
#include <iostream>
using namespace std;
/*complete the function to multiple the megabytes,
don't forget to set the parameter*/
void promotion(int *mb) {
//taking multiplier as input
int multiplier;
cin>>multiplier;
*mb *= multiplier;
}
int main() {
//getting initial count of megabytes
int megabytes;
cin >> megabytes;
//printing the count of megabytes before the promotion
cout << "Before the promotion: " << megabytes << endl;
//complete the function call
promotion(&megabytes);
//printing the count of megabytes after the promotion
cout << "After the promotion: " << megabytes << endl;
return 0;
}
+ 1
How are you writing the function?
Have you a copy of the code in SoloLearn? you may have better chance for answer when the people can see your code.
Follow this guide to share links
https://www.sololearn.com/post/75089/?ref=app
0
This work for me
#include <iostream>
using namespace std;
/*complete the function to multiple the megabytes,
don't forget to set the parameter*/
void promotion(int *m) {
//taking multiplier as input
int multiplier;
cin>>multiplier;
*m = *m * multiplier;
}
int main() {
//getting initial count of megabytes
int megabytes;
cin >> megabytes;
//printing the count of megabytes before the promotion
cout << "Before the promotion: " << megabytes << endl;
//complete the function call
promotion(&megabytes);
//printing the count of megabytes after the promotion
cout << "After the promotion: " << megabytes << endl;
return 0;
}
0
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
/*complete the function to multiple the megabytes,
don't forget to set the parameter*/
int promotion(int *n)
{
//taking multiplier as input
int multiplier;
cin>>multiplier;
return *n*=multiplier;
}
int main() {
//getting initial count of megabytes
int megabytes,multiplier;
cin >> megabytes;
//printing the count of megabytes before the promotion
cout << "Before the promotion: " << megabytes << endl;
//complete the function call
promotion(&megabytes);
//printing the count of megabytes after the promotion
cout << "After the promotion: " << megabytes << endl;
return 0;
}
Good Luck