Question

Improve the sorting example given by writing a choose_direction function that returns a pointer to one...

Improve the sorting example given by writing a choose_direction function that returns a pointer to one of compare_greater and compare_less, depending on whether the user inputs an A or not. The if-then-else construct in the main function can then be replaced by : sort(data,number_of_numbers,choose_direction());.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C CODE:

#include <stdio.h>
#include <stdlib.h>
#define NUMBERS 100
#define TRUE 1
#define FALSE 0
typedef int DataType[NUMBERS];
int CompareGreater(int This,int That) {
    return(This>That);
}
int CompareLess(int This,int That) {
    return(This<That);
}
void Swap(int *This,int *That) {
    int Temporary;
    Temporary = *This;
    *This = *That;
    *That = Temporary;
}
void Sort(DataType Data,int Size,int (*CompareFunction)(int,int)) {
    int Index,SwapOccured;
    SwapOccured = TRUE;
    while (SwapOccured) {
        SwapOccured=FALSE;
        for (Index=0; Index<Size-1; Index++)
            if ((*CompareFunction)(Data[Index],Data[Index+1])) {
                Swap(&Data[Index],&Data[Index+1]);
                SwapOccured = TRUE;
            }
    }
}
int GenerateData(DataType Data) {
    int HowMany,Index;
    printf("How many numbers (max %d) : ",NUMBERS);
    scanf("%d",&HowMany);
    for (Index=0; Index < HowMany; Index++) {
        Data[Index] = rand();
    }
    return(HowMany);
}
void OutputData(DataType Data,int HowMany) {
    int Index;
    printf("The numbers are\n");
    for (Index=0; Index < HowMany; Index++) {
        printf("%d ",Data[Index]);
    }
    printf("\n");
}
char InputDirection(void) {
    char Direction;
    printf("(A)scending or (D)ecending : ");
    scanf(" %c",&Direction);
    return(Direction);
}
// Function which returns a function pointer to a function
// which takes two ints and returns an int
int (*choose_direction(void))(int, int){
    char Direction=InputDirection();
    if (Direction=='A' || Direction=='a')
        return &CompareGreater;
    return &CompareLess;
}
int main(void) {
    DataType Data;
    int NumberOfNumbers;
    char Direction;
    NumberOfNumbers=GenerateData(Data);
    OutputData(Data,NumberOfNumbers);
    
    Sort(Data,NumberOfNumbers,choose_direction());
    
    OutputData(Data,NumberOfNumbers);
    return(EXIT_SUCCESS);
}

SAMPLE OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Improve the sorting example given by writing a choose_direction function that returns a pointer to one...
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
  • currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to...

    currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to a linked list and a value, and if the value is in the linked list, it is deleted and the function returns true. If it is not in the list, the function returns false. 8. Expand your menu to test your three new functions. When choosing to test the find or findAndDelete functions, the user will be first asked to enter the value to...

  • 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...

  • One way of obtaining a pointer to a variable is by writing something like: line 1:...

    One way of obtaining a pointer to a variable is by writing something like: line 1: int a, *p; line 2: p = &a; Another way of doing this is to write something like: line 3: int *t = new int; This allocates a memory location to which the variable t will point. Write a function that includes the 3 lines of code above and returns the size of the memory location occupied by variable t. First, discuss how to...

  • 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...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts...

    C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts it based on the users sort method selection. Please provide a separate .cpp file wit hthe code containing the sort method. The sorting method uses the text file below and sorts it accordingly. Seperate the sorting method into an additional C++ file. ********************************* Text File Below is a text file called classes.txt. This text file lists, by course number and section number a series...

  • a) Make a function which returns x/3 with a given integer x. double third(int x) {...

    a) Make a function which returns x/3 with a given integer x. double third(int x) { //Complete your code below. ______________________________________ return y; } b) Make a program which shows 2 ∗ n with a given user input n. #include<iostream> using namespace std; // Complete the blank. _______________________ int main(){ int n; cout<<"Enter:"; cin>>n; cout<<twice(n); return 0; } int twice(int x){ return 2*x; } c) Make a function which returns true if n is positive, otherwise returns false. ________ positive(int...

  • Write a function to calculate the normalized sinc This example demonstrates writing test cases for function-based...

    Write a function to calculate the normalized sinc This example demonstrates writing test cases for function-based problems. Assessment tests are designed to capture the problem requirements as well as common student errors. The rectangular function (or square-pulse) is commonly used in signal processing applications. The Fourier transform of the rectangular function is the normalized sinc function given by: Write a function normsinc that returns . Your function must: Calculate for each element of the input. If the input is a...

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