C++ Programming Windows Console Application Write a program that will read two f
ID: 3805518 • Letter: C
Question
C++ Programming Windows Console Application
Write a program that will read two floating point numbers (the first read into a variable called first and the second read into a variable called second) and then calls the function swap with the actual parameters (arguments) first and second. the swap function having formal parameters number 1 and number 2 should swap the value of the two variables. Enter the first number Then hit enter 80 Enter the second number Then hit enter 70 You input the numbers as 80 and 70. After swapping, the first number has the value of 70 which was the value of the second number. the second number has the value of 80 which was the value of the first number. Compile the program and correct it if necessary until you get no syntax errors. Run the program with the sample data above and see if you get the same results. the swap parameters must be passed by _. Why?(Assume that main produces the output)Explanation / Answer
#include <iostream>
using namespace std;
void swap(int &number1, int &number2) {
number1 = number1+ number2;
number2 = number1 - number2;
number1 = number1 - number2;
}
int main()
{
int first, second;
cout<<"Enter the first number: ";
cin >> first;
cout<<"Enter the second number: ";
cin >> second;
cout<<"You input the numbers as "<<first<<" and "<<second<<"."<<endl;
swap(first, second);
cout<<"After Swapping, the numbers as "<<first<<" and "<<second<<"."<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the first number: 80
Enter the second number: 70
You input the numbers as 80 and 70.
After Swapping, the numbers as 70 and 80.
Excercise 3:
Answer:
The swap parameters must be passed by reference becuase we change the values in swap functions that should modify the actual values in main method.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.