In function main declare three integers a, b, and c. Call a function randomNumbers to assign a random number between 1 and 50 to each and return using pointer parameters. Print the integers and their addresses in a function printNumbers.

Then call the function order to place the integers in numerical order using pass by address. Function order should have three parameters: int *aPtr, int *bPtr, and int *cPtr. The purpose of function order is to place the three integers in order so that a
The integers should be printed in order with addresses from printNumbers. When the steps of the function order have been completed the smallest value will be stored in a, the middle in b, and the largest in c.

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.
Ver imagen flightbath