Question

Hello I need help with this program. Should programmed in C!

Program 2: Sorting with Pointers Sometimes were given an array of data that we need to be able to view in sorted order while

Input For our input into this problem lets use the data in the file “Array Input Pointer Sort” found in the Homework Input

Swap Function We will need a swap function that can swap the values of two argument pointers. In this case, our original data

A swapIntPtr() function. When called on two pointer arguments, this function should swap the values of the pointer arguments.

Suggestions for implementation Until you get used to them, pointers can be very confusing to work with. Therefore, it is very

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

#include <stdio.h>

#include <stdlib.h>

#define SIZE 150

void swapIntPtr(int **x, int **y){

    uintptr_t a = (uintptr_t)*x;

    uintptr_t b = (uintptr_t)*y;

    a = a ^ b;

    b = a ^ b;

    a = a ^ b;

    *x = (int*)a;

    *y = (int*)b;

}

void BubbleSort(int ** arr, int size)

{

    for(int i = 0; i < size; i++)

    for(int j = 0; j < size - 1 - i; j++)

    if(*arr[j] > *arr[j + 1])

        swapIntPtr(&arr[j], &arr[j + 1]);

}

char * convert(int i)

{

    char * c = (char *)malloc(7);

    int j = 6;

    do{

        c[j--] = (char)('0' + i % 10);

        i /= 10;

    } while (i);

    while(j >=0 ) c[j--] = ' ';

    return c;

}

void printArr(int data[], int size)

{

    for(int i = 0; i < size; i++)

    {

        printf("%s", convert(data[i]));

        if((i + 1) % 10 == 0) printf("\n");

    }

    printf("\n");

}

void printArrPtr(int **data, int size)

{

    for(int i = 0; i < size; i++)

    {

        printf("%s", convert(*data[i]));

        if((i + 1) % 10 == 0) printf("\n");

    }

    printf("\n");

}

int main()

{

    int data[SIZE];

    for(int i = 0; i < SIZE; i++)

        data[i] = rand() % 3001;

    data[0] = 0;

    int * pdata[SIZE];

    for(int i = 0; i < SIZE; i++)

        pdata[i] = &data[i];

    

    BubbleSort(pdata, SIZE);

    printf("Now displaying data in original order\n");

    printArr(data, SIZE);

    printf("Now displaying data in sorted order\n");

    printArrPtr(pdata, SIZE);

    printf("Now displaying data in original order\n");

    printArr(data, SIZE);

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...
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
  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • The program defined in insertionSort.cpp gets a list of numbers as input from the user and...

    The program defined in insertionSort.cpp gets a list of numbers as input from the user and calls the insertion sort algorithm to sort them. It also displays the list before and after the sorting. The insertion sort algorithm should be defined in the InsertionSort.h file, but this definition has been omitted and it is your task to provide it. The appropriate insertion sort function is to be defined in the InsertionSort.h file. The InsertionSort.h file performs an include of Swap.h,...

  • 11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...

    11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude <stdio.h> 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i])...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least...

    HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it...

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