fix sort function!

This is a discussion on fix sort function! within the C Programming forums, part of the General Programming Boards category; Code: Make the following work by changing only the sort function and its prototype. (Work means that the elements in ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    fix sort function!

    Code:
    Make the following work by changing only the sort function and its prototype. (Work means that the elements in the original array of floats are not changed but remain in their original order. Only the array of pointers to floats are changed.)
     
    #include <stdio.h>
    #include <stdlib.h>
    int getData(float** arr);
    float** getPointers(float* arr, int);
    void sortSelect(float arr[], int num);
    int main(void)
    {
    //Declare variables
    float* arr; float** ptrf;
    int i;
    int num;
    /*
    printf("Enter the number of students that took the midterm ");
    scanf ("%d", &num);
     
    arr = (float*)calloc(num,sizeof(float));
    if(arr == NULL) {printf("Memory Overflow"); system("pause"); exit(100);}
     
    for(i = 0; i < num; i++)
    scanf("%f", arr + i);
    */
    num = getData(&arr);
     
    for(i = 0; i < num; i++)
    printf("%.1f ", *(arr + i));
     
    printf("\n\n");
    ptrf = getPointers(arr,num);
    sortSelect(ptrf, num);
    for(i = 0; i < num; i++)
    printf("%.1f ", **(ptrf + i));
     
    free(arr);
    arr = NULL;
    if(arr)
    printf("\n\n%.1f", *(arr + 0));
    int stop; scanf(" %d", &stop);
    return 0;
    }
    int getData(float** arr)
    {
    int num; int i;
    printf("Enter the number of students that took the midterm ");
    scanf ("%d", &num);
     
    *arr = (float*)calloc(num,sizeof(float));
    if(*arr == NULL) {printf("Memory Overflow"); system("pause"); exit(100);}
     
    for(i = 0; i < num; i++)
    scanf("%f", *arr + i);
    return num;
    }
     
    float** getPointers(float* arr, int num)
    {
    float** ptrf;
     
    ptrf = (float**)calloc(num, sizeof(float*));
     
    for ( int i = 0; i < num; i++)
    *(ptrf + i) = arr + i;
     
    return ptrf;
    }
    void sortSelect(float arr[], int num)
    {
    int current; int walker;
    int smallestIndex;
    float temp;
     
    for (current = 0; current < num - 1; current++)
    {
    smallestIndex = current;
    for (walker = current; walker < num; walker ++)
    {
    if (arr[walker] < arr[smallestIndex])
    smallestIndex = walker;
    }//for walker
     
    //Swap to position smallest at what is the current position
    temp = arr[current];
    arr[current] = arr[smallestIndex];
    arr[smallestIndex] = temp;
    }//for current
    return;
    }
    this is my new sort function
    Code:
    void sortSelect(float *arr[], int num)
    {
    int current; int walker;
    int smallestIndex;
    float temp;
     
    for (current = 0; current < num - 1; current++)
    {
    smallestIndex = current;
    for (walker = current; walker < num; walker ++)
    {
    if (*arr[walker] < *arr[smallestIndex])
    smallestIndex = walker;
    }//for walker
     
    //Swap to position smallest at what is the current position
    temp = *arr[current];
    *arr[current] = *arr[smallestIndex];
    *arr[smallestIndex] = temp;
    }//for current
    return;
    }
    did i do what i was suppose to do according to the question from the original code or did i do something wrong if so how do i fix it
    and sorry for the indentation as it was not my code i just copy and pasted over
    Last edited by khoavo123; 02-21-2012 at 02:02 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    No. You were asked to sort only the pointers. Not the actual array. So the swap part should be:
    Code:
    float *tmp;
    ...
    //Swap to position smallest at what is the current position 
    tmp = arr[current];
    arr[current] = arr[smallestIndex];
    arr[smallestIndex] = tmp;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sort function
    By compnub1 in forum C Programming
    Replies: 21
    Last Post: 05-01-2010, 03:30 PM
  2. sort function
    By compnub1 in forum C Programming
    Replies: 1
    Last Post: 04-30-2010, 09:57 AM
  3. Sort Function
    By Taka in forum C++ Programming
    Replies: 16
    Last Post: 05-23-2009, 02:18 PM
  4. C# function for sort
    By farzad in forum C# Programming
    Replies: 8
    Last Post: 01-16-2009, 07:27 AM
  5. sort function help
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 04:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21