Write a main method to: Set up two values in main, a and b. read in two variable
ID: 3923595 • Letter: W
Question
Write a main method to:
Set up two values in main, a and b.
read in two variables using a Read function
while (a != 0)
print two variables using a Print function
switch the data in the two variables, using a Swap function
print two variables again using the Print function
read in two variables using Read Function
stop
Read Function – pass by reference
Code a function that reads in two variables.
Print Function – pass by value
Code a function that prints out two variables with documentation, such as:
“The first value is “ ______
“The second value is “______
Swap Function – pass by reference
Code a function that will switch the values of two memory positions in main
Main Function
Code a main method/function that will accomplish the 4 steps given above.
Remember that the data variable names defined in main can be different from the data variable names used in the functions.
Run:
Run this program at least twice – once with two integer values and a second time with two floating point values. So how will you define your variables in main?
Explanation / Answer
#include<iostream>
int readinput(int &a, int &b)
{
cout <<"Enter vale for a ";
cin >> a;
cout <<"Enter vale for b ";
cin >> b;
}
void printvalues(int a, int b)
{
cout << "Value of a: "<<a<<" and Value of b: "<<b<<endl;
}
void swap(int &a, int &b)
{
int c=a;
a=b;
b=c;
}
int main()
{
int a, b;
readinput(a,b);
printvalues(a,b);
swap(a,b);
cout <<"After Swap ";
printvalues(a,b);
}//end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.