Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to input 3 integer values into the compute

ID: 3761243 • Letter: W

Question

Write a program that prompts the user to input 3 integer values into the computer. Write a function that re-orders the values in the three integer variables such that the values occur in ascending order.

Specifications:

Use the following function prototype for your sorting function:

void ptr_sort(int *a, int *b, int *c);

Use the following three variables to store your numbers in the main() function:

int x, y, z;

Use the following three pointers to point to the three variables in the main() function:

int *i, *j, *k;

After you have your integer variables and pointers declared, prompt the user to enter 3 numbers (1 number at a time). Scan in the numbers into the variables using your pointers, and call your ptr_sort() function – whose purpose is to sort the numbers so that x contains the smallest number, y contains the middle number, and z contains the largest number. Then print the results to the screen. You MUST use pointers to do ALL operations relating to scanning, sorting, and printing the numbers.

If you execute the program, the following information should be displayed:

~> hw20.o

Please enter the first number: 3

Please enter the second number: 1

Please enter the third number: 2

The sorted numbers are: 1, 2, and 3.

~> hw20.o

Please enter the first number: -15

Please enter the second number: -30

Please enter the third number: 212

The sorted numbers are: -30, -15, and 212.

Explanation / Answer

#include<stdio.h>

void ptr_sort(int *a, int *b, int *c)

{

int i,j,k;

i = *a;

j = *b;

k = *c;

  

if(i>j)

{

if(i>k)

{

if(k>j)

{

*a = j;

*b = k;

*c = i;

}

else

{

*a = k;

*b = j;

*c = i;

}

}

else

{

*a = j;

*b = i;

*c = k;

}

}

else

{

if(j>k)

{

if(k>i)

{

*a = i;

*b = k;

*c = j;

}

else

{

*a = k;

*b = i;

*c = j;

}

}

else

{

*a = i;

*b = j;

*c = k;

}

}

  

  

}

int main()

{

int x,y,z;

  

printf("Input 3 numbers : ");

  

printf("Input number 1 : ");

scanf("%d",&x);

  

printf("Input number 2 : ");

scanf("%d",&y);

printf("Input number 3 : ");

scanf("%d",&z);

  

ptr_sort(&x, &y, &z);

  

printf("The sorted numbers are : %d, %d, %d",x,y,z);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote