Question

Implement the sort procedure from Assignment 18 as a void function. Pass the address of the...

Implement the sort procedure from Assignment 18 as a void function. Pass the address of the first array element to the function along with the number of elements in the array. Use pointers in your function to reference the array elements. The sort function should do nothing but sort the elements of the array. DO NOT pass the entire array as an argument to the function. As in Assignment 18, print out the array before and after sorting. Use the same test data. All output should be in main(), not in the function. Prog18:Consider the following sort procedure written in pseudo code: Exchange Sort of Array X[] For I = 0 to (number of array elements - 2), increment by 1 For J = (I + 1) to (number of array elements - 1), increment by 1 If X[I] is greater than X[J] then temp = X[I] X[I] = X[J] X[J] = temp Next J Next I Implement the sort procedure in main() without using pointers. Declare the array as follows using the test data shown: int x[] = { 28, 87, -3, 45, 19 }; Use a for loop to print out the array before sorting and again after sorting. There should be no output within the sort procedure.

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

#include<iostream.h>
#include<conio.h>
void sort(int*[],int); //function declaration

void sort(int *a[],int x) //sort function definition
{
int temp;
for (int i=0;i<x;i++)

{
for(int j=i+1;j<x;j++)
{
if(*a[i]>*a[j])
{
temp=*a[i];
*a[i]=*a[j];
*a[j]=temp;
}

}
}

}

void main()

{
clrscr();
int *p=0;
int arr[20],n,tmp;
cout<<"Enter the array size: ";
cin>>n;
cout<<"Enter array elements:\n"; //input for array
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"so our array before sort is:\n"; //array before sorting
for(int j=0;j<n;j++)
{
cout<<arr[j]<<" ";
}
cout<<"\n";
p=&arr[0];

sort(&p,n); //passing first element of array as reference and size

for(int k=0;k<n;k++) //sorting procedure
{
for(int l=k+1;l<n;l++)
{
if(arr[k]>arr[l])
{
tmp=arr[k];
arr[k]=arr[l];
arr[l]=tmp;
}
}
}

cout<<"Now the array after sorting process is: "; //printing sorted array
for(int m=0;m<n;m++)
{
cout<<arr[m]<<" ";
}
cout<<"\n";
}

OUTPUT:

ile Edit Search Run Is1 Enter the array size: 5 Enter array elements: 28 87 -3 45 19 so our array before sort is: 28 87-3 45

Add a comment
Know the answer?
Add Answer to:
Implement the sort procedure from Assignment 18 as a void function. Pass the address of the...
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
  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

    create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...

    In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...

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

  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

  • C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show...

    C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show super and sub class relationships with an array of super media pointers to sub class objects and dynamic binding. The < operator will be overloaded in the super class so all subclasses can use it. The selection sort method will be in the main .cpp file because it will sort the array created in main. The final .cpp file, the three .h header class...

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