Write a program that will read two floating point numbers (the first read into a
ID: 3778930 • Letter: W
Question
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). Your main function should print out the values of these two numbers immediately after they are read. Your main functions should then call a function named swap with the actual parameters first and second. The swap function having formal parameters number1 and number2 should swap the value of the two variables. Your main function should print the values of first and second again after the swap function returns. Sample Run: Enter the first number Then hit enter 80 Enter the second number Then hit enter 70 You input the numbers as 80 (first) and 70 (second). After swapping, the first number has the value of 70 and the second number has the value of 80.Explanation / Answer
#include <iostream>
using namespace std;
void swap(double *number1,double *number2)
{
double *temp,i; // temp variable
temp=&i;
*temp=*number1; // assigning number1 to temp
*number1=*number2;// number2 to number1
*number2=*temp;// temp to number2
}
int main()
{
double first, second;
cout << "Enter the first number" << endl;
cout << "Then hit enter" << endl;
cin >> first ; // first number input from user
cout << "Enter the second number" << endl;
cout << "Then hit enter" << endl;
cin >> second ; // second number input from user
cout<<"You input the number as "<<first<<"(first) and "<<second<<"(second)"<<endl;
swap(&first,&second);
cout<<"After swapping the first number has the value of "<<first<<" and second number has the value of "<<second<<endl;
return 0;
}
----------------------------------------------------------------output-------------------------------------------------------
Enter the first number
Then hit enter
60
Enter the second number
Then hit enter
50
You input the number as 60(first) and 50(second)
After swapping the first number has the value of 50 and second number has the value of 60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.