Question

Solve this problem using pointer variables wherever possible. to be completed in C Problem -  sort 3...

Solve this problem using pointer variables wherever possible.

to be completed in C

Problem -  sort 3 numbers

Modify the sort 3 program you previously wrote, to use pointers.  In main( ) read 3 numbers from the user into low, medium, high, then call a function to “sort” them. That is, if low = 50, medium = 10 and high = 30 before the function is called, then after the function call the variables will contain low = 10, medium = 30 and high = 50.  Print out the values of low, medium and high before and after the function call.

The “sort” function needs only 3 separate if statements (with no else).  Each if statement should call a swap function.  The first two if statements should ensure that low has the correct value.  The final if statement will ensure medium and high are correct.

The code for the “sort” function and swap function should come after main( ) so you’ll need prototypes.  All parameter passing should be done via pointers.

In main call the “sort” function with these 3 different data sets, writing out the values for low, medium and high before and after calling the “sort” function in each case:

            low = 50, medium = 10, high = 30

            low = 30, medium = 50, high = 10

            low = 50, medium = 30, high = 10

Note: think very carefully about the parameters in each function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include<stdio.h>
void swap(int* a,int* b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void sort(int* low,int* medium,int* high)
{
if (*low > *high)
swap(low, high);

if (*low > *medium)
swap(low, medium);

//Now the smallest element is the first one. Just check the 2-nd and 3-rd

if (*medium > *high)
swap(medium, high);
}
int main()
{
int low=50,medium=10,high=30;
printf("Values before function are low=%d, medium=%d and high=%d ",low,medium,high);
sort(&low,&medium,&high);
printf("Values after function are low=%d, medium=%d and high=%d ",low,medium,high);
low=30;
medium=50;
high=10;
printf("Values before function are low=%d, medium=%d and high=%d ",low,medium,high);
sort(&low,&medium,&high);
printf("Values after function are low=%d, medium=%d and high=%d ",low,medium,high);
low=50;
medium=30;
high=10;
printf("Values before function are low=%d, medium=%d and high=%d ",low,medium,high);
sort(&low,&medium,&high);
printf("Values after function are low=%d, medium=%d and high=%d ",low,medium,high);


}


Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Solve this problem using pointer variables wherever possible. to be completed in C Problem -  sort 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a function that takes as input parameters (using call by pointer) 3 integers. It...

    C++ Write a function that takes as input parameters (using call by pointer) 3 integers. It generates a random number between 25 and 50 (not including 50). It then creates an array on the memory heap of that length. It generates a random high number (mine was between 5 and 10) and a random low number (between -5 and -10) and fills in the array iteratively with random numbers between the high and the low numbers*, and it returns that...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code....

    Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference Parameters 2 // This program uses a function to swap the values in two variables . 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 using namespace std; 6 7 // Function prototype 8 void swapNums(int, int); 9 10 /***** main *****/ 11 int main() 12 { 13 int num1 = 5, 14...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • code in C++ Create a program that uses the pass by reference operator instead of the...

    code in C++ Create a program that uses the pass by reference operator instead of the return command. Your main program will need to: create empty variables call void function 1 with no input to display your name to the screen call function 2 to collect the square feet that a gallon of paint covers from user call function 2 to collect the square feet of wall that needs to be painted call function 3 to calculate the number of...

  • What I need: Create a new program named Reverse4 and declare four integer variables with the...

    What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT