Write a complete program that will have a main function and a function called tw
ID: 642566 • Letter: W
Question
Write a complete program that will have a main function and a function called twice.
Write a function named twice that only accepts an integer pointer as parameter (or argument).
This function doubles the value stored at the location pointed by the pointer parameter.
In the main method, declare an integer variable num and initialize it to 5. Call the function twice from main such that the function will double the value of num.
In the main method and after calling the function, print the variable num.
In C++
Explanation / Answer
The code would look something like this
#include<iostream>
using namespace std;
void twice(int *num){
/*
a function named twice that only accepts an integer pointer as parameter (or argument).
*/
int value_Stored_At_The_Location_Initially = *num;
int new_Value = value_Stored_At_The_Location_Initially * 2;
*num = new_Value;
}
int main(){
int num ; //declare an integer variable num
num = 5; //initialize it to 5
twice(&num); //Call the function twice
cout<<num; //, print the variable num.
return 0;
}
Hope the comments clarify what is happening. I have copied the instructions in the question near the places where they are happening. If you have any dounts/ clarifications, feel free to contact. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.