Pass by Reference with Pointers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

15th Dec 2020, 2:16 PM
Dyllan
7 Answers
+ 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; }
27th Apr 2021, 12:04 AM
Premium World
+ 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; }
15th Dec 2020, 2:17 PM
Dyllan
+ 3
In your multiplier function just do *x *= multiplier;
15th Dec 2020, 2:28 PM
Ipang
+ 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; }
6th Sep 2021, 6:35 PM
Ed_python
+ 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
15th Dec 2020, 2:22 PM
Ipang
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; }
19th Jan 2022, 7:57 AM
Daro Sim
Daro Sim - avatar
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
25th Jan 2022, 5:40 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar