Menu

[Solved] Need Program Sort Array Pointers Instead Works Need Know Swap Pointers Include Define Maxs Q37191945

I need this program to sort an array but with pointers instead.It works as is I just need to know what to swap out forpointers.

#include<stdio.h>
#define MAXSIZE 10

int readElements(int arr[]);      //Function Prototype

void printElements(const int arr[], const int n);      //Function Prototype

void selectionSort(int arr[], int n);      //Function Prototype

void swap(int arr[], int i, int j);          //Function Prototype

int main()
{
int arr[MAXSIZE];        //this declaresthe array
printf(“Enter the elemnts of the array and then enter EOFn”);
int n = readElements(arr);        //readsin inputs for the array
printf(“nBefore Sorting: n”);
printElements(arr, n);    //prints original array
selectionSort(arr, n);   //sorts the array
printf(“After Sorting: n”);
printElements(arr, n);   //prints the sorted array
return 0;
}

int readElements(int arr[])      //Function to read in array
{
int n;   //used for index
int s;   //what scanf equals
while(s != EOF && n <= MAXSIZE)
{
    s = scanf(“%d”, &arr[n]);
    if(s == 0)   //Tests for BadData
    {
    printf(“Invalid data. Please enter ints only.n”);
    while (getchar() != ‘n’);
   }
   else
   n++;
}
if(n >= MAXSIZE)       //Error given ifthe size is > 10
printf(“Error, Max size is 10, will only input first 10 elemntsinto array”);
return n-1;          //Returns the finished array, -1 is required as it adds an extra 0to the array without it
}

void printElements(const int arr[], const int n)  //prints array, values are constant so array is not modified
{
for(int i=0; i<n; i++)
{
printf(“%d “, arr[i]);
}
printf(“n”);
}

void selectionSort(int arr[], int n)      //Sorting function, used to sort array
{
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(arr[i]>arr[j])
swap(arr, i, j);       //swap functionswaps elements if the first is larger
}
}
}

void swap(int arr[], int i, int j)      //Swap function, used in sort
{
int temp = arr[i];       //sets up tempint to use for swap
arr[i] = arr[j];         
arr[j] = temp;
}

Expert Answer


Answer to I need this program to sort an array but with pointers instead. It works as is I just need to know what to swap out for… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *