1
Program 1
Aim (Write on the ruler page)
Write a function in C++ that extends the data (passing by reference) using the swap function to interchange the given two numbers.
Program (Start from new page after AIM)
#include<iostream.h> // This line includes the header file iostream.h, which provides input/output operations.
#include<conio.h> // This line includes the header file conio.h, which provides console input/output operations.
void swap(float &x,float &y) // This line defines the swap function, which takes two float variables by reference.
{
float t=x; // This line creates a temporary variable t and assigns it the value of x.
x=y; // This line assigns the value of y to x.
y=t; // This line assigns the value of t to y.
}
void main() // This line defines the main function, which is the entry point of the program.
{
void swap(float &,float &); // This line declares the swap function.
float a,b; // This line declares two float variables called a and b.
cin>>a>>b; // This line reads two float values from the console and assigns them to a and b.
cout<<"a="<<a<<"b="<<b<<endl; // This line displays the initial values of a and b.
swap(a,b); // This line calls the swap function, passing a and b as arguments.
cout<<"a="<<a<<"b="<<b<<endl; // This line displays the new values of a and b.
getch(); // This line waits for the user to press a key before closing the console window.
}
Output (Stick on blank page opposite to code)
Download Output