Question

In: Computer Science

Using a programming language of your choice, write a complete and fully functional program that uses...

  1. Using a programming language of your choice, write a complete and fully functional program that uses reference and pointer types to swap two double precision floating-point numbers. The two numbers are read in by the program’s user. Use a proper prompt for each number.
    1. Use one function that uses formal parameter reference types to swap the two numbers
    2. Use another function that uses formal parameter pointer types to swap the two numbers.
    3. In the main or driver function, call these two functions with the actual arguments received from the program’s user
    4. Display immediately the result of the swapping after each function call in the calling function (the main function or the driver) before the next function call to see whether the numbers are swappe Use a proper display message.

Solutions

Expert Solution

C++ CODE:

#include<iostream>
#include<iomanip>

using namespace std;

// function to swap variables using reference
void swap_ref(int& n1, int& n2)
{
    int temp;
    temp = n1;
    n1 = n2;
    n2 = temp;
}

//function to swap variables using pointers
void swap_ptr(double *p,double *q)
{
   double temp;
   temp=*p;
   *p = *q;
   *q=temp;
}

int main()
{
   //variable to store the input values
   double x,y;
  
   //temporary variables
   double t1,t2;
  
   // user input from the user
   cout<<"Enter a floating point number: ";
   cin>> x;
  
   cout<<"\nEnter another floating point number: ";
   cin>>y;
  
   //copying the input values to temporary variables
   t1=x;
   t2=y;
  
   cout<<"\n\nSwapping the variables using reference\n";
  
   // the value of x and y before swapping
   cout<<"\n\nBefore Swapping\n"<<endl;
   cout<<"The value of x is : "<<setprecision(2)<<fixed<<x<<endl;
   cout<<"The value of y is : "<<setprecision(2)<<fixed<<y;
  
   //calling the function
   swap(x,y);
  
   //the value of x,y after swapping
   cout<<"\n\nAfter Swapping\n"<<endl;
   cout<<"The value of x is : "<<setprecision(2)<<fixed<<x<<endl;
   cout<<"The value of y is : "<<setprecision(2)<<fixed<<y;
  
   // reinitialize the value of x and y to user input values
   x=t1;
   y=t2;
  
   cout<<"\n\nSwapping the variables using pointers\n";
   // the value of x and y before swapping
   cout<<"\nBefore Swapping\n"<<endl;
   cout<<"The value of x is : "<<setprecision(2)<<fixed<<x<<endl;
   cout<<"The value of y is : "<<setprecision(2)<<fixed<<y;
  
   //calling the function
   swap_ptr(&x,&y);
  
   //the value of x,y after swapping
   cout<<"\n\nAfter Swapping\n"<<endl;
   cout<<"The value of x is : "<<setprecision(2)<<fixed<<x<<endl;
   cout<<"The value of y is : "<<setprecision(2)<<fixed<<y;
  
   return 0;
}

SCREENSHOT FOR OUTPUT:


Related Solutions

Write a program in a language of your choice to perform a search using the A*...
Write a program in a language of your choice to perform a search using the A* algorithm for the eight puzzle problem, in which numbers may be shifted one space at a time to transform the initial state into the goal state (see p. 103 – 3rd Ed., pp. 105-106 – 2nd Ed. of the text). 2. a) Use the start state-goal state combination given in pp. 103, Figure 3.28 (3rd Ed.), [pp. 105, Figure 4.7 (2rd Ed.)], as (start_1,...
Write a program, using your favourite programming language, to parse time log files to report how...
Write a program, using your favourite programming language, to parse time log files to report how much time in total spent on project. The time log file TimeLogCarbon.txt. Time Log: 2/23/12: 9:10pm - 11:40pm getting familiar with Flash 2/29/12: 12:50pm - 2:00pm getting familiar with Flash 3/1/12: 6:00pm - 11:40pm getting familiar with Flash 3/3/12: 3:00pm - 7:00pm step-debug Energy Game code 3/4/12: 8:00pm - 11:40pm start carbon game 3/5/12: 2:00pm - 3:00pm, 4:00pm - 4:30pm carbon game 3/6/12: 11:30am...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
Using a programming language of your choice: 1) generate a range of wavelengths, 2) compute corresponding...
Using a programming language of your choice: 1) generate a range of wavelengths, 2) compute corresponding monochromatic blackbody intensity using a) Planck function, b) Rayleigh-Jeans simplification, and c) Wien simplification, and 3) plot the three resulting spectra (i.e., a diagram that shows how B(λ) changes with λ). Using this figure explain the phenomenon of “ultraviolet catastrophe”. Please include the code (not only figure) in your answer.
Write a function in any functional programming language that will reverse a general list. For example,...
Write a function in any functional programming language that will reverse a general list. For example, if an input is (A (B C (D E)) F), output is (F ((E D) C B) A).  Please note that any built-in/pre-defined function, e.g., reverse, cannot be used in your answer. Please DO NOT hard-code any input values, output values in your code. Please submit a screenshot of where your code got compiled, executed, showing the execution result
Write the simplest program possible in your language of choice containing your 'main' and any other...
Write the simplest program possible in your language of choice containing your 'main' and any other functions you may need that will: A. Read a sequence of words up to a maximum of 64 words from std input. Before reading the input, prompt the user to enter the words. B. Check to see if there any duplicate words in the input read. C. If there are no duplicates, print out to std output the message 'No Duplicates'. D. If there...
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing...
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing on your experience from program 1, read an integer, n from keyboard (standard input). This integer represents the number of integers under consideration. After reading that initial integer in, read n integers in, and print the minimum and maximum of all the integers that are read in. Example: Input Output 7 1 5 3 6 9 22 2 Min: 1 Max: 22 C++ preferred
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT