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