Respuesta :
Answer:
#include <iostream>
#include<time.h>
using namespace std;
void randomNumbers(int *a,int *b,int *c) //function to assign random numbers to a,b,c
{
srand (time(NULL)); //used to generate random number every time
*a=rand() % 50 +1;
*b=rand() % 50 +1;
*c=rand() % 50 +1;
}
void printNumbers(int *a,int *b,int *c) //to print number and addresses
{
cout<<"\nNumbers and their addresses are :"<<endl<<*a<<"\t"<<a<<endl;
cout<<*b<<"\t"<<b<<endl;
cout<<*c<<"\t"<<c<<endl;
}
void swap(int *x,int *y) //to swap 2 values at 2 addresses
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void order(int *aPtr, int *bPtr,int *cPtr) //to order a,b,c such that a<b<c
{
int temp,temp1;
if(*aPtr>*bPtr)
{
if(*aPtr>*cPtr) //if true a is largest
{
swap(aPtr,cPtr); //swapped largest value with c
if(*aPtr>*bPtr) //to order middle one
{
swap(aPtr,bPtr);
}
}
else
{
swap(aPtr,bPtr); //c is in its right place
}
}
else
{
if(*aPtr>*cPtr)
{
swap(aPtr,cPtr);
swap(bPtr,cPtr);
}
else
{
if(*bPtr>*cPtr)
{
swap(bPtr,cPtr);
}
}
}
}
int main()
{
int a,b,c;
randomNumbers(&a,&b,&c);
printNumbers(&a,&b,&c); //unordered
order(&a,&b,&c);
cout<<"After ordering";
printNumbers(&a,&b,&c); //now they will be ordered
return 0;
}
OUTPUT :
Please find the attachment below.
Explanation:
In main method() 3 methods are called -
- randomNumbers() - This will assign random numbers to each variable using rand() function. This takes 3 pointer variables as arguments.
- printNumbers() - This will print the numbers wwith their addressses. This also takes 3 ptr variables as arguments.
- order() - This will order the variables such as a will contain the lowest value and c will contain the largest value. This calls swap() function many times to change the values between 2 variables.
