in C language Project: You will create a program which contains 4 functions: 1)
ID: 3801757 • Letter: I
Question
in C language
Project: You will create a program which contains 4 functions: 1) ReadArray() 2) SortArray() 3) ReverseArray() 4) PrintArray() When the main() executes it will only have calls to 4 functions listed above. ReadArray() prompts user to enter up to 10 integers. When a 0 is entered, it will signal the end of user input. SortArray() uses the quicksort routine from the book to sort the array in ascending order. ReverseArray() keeps track of how many elements are there in the array and reverses the array and storing in another array which is also passed to it by main(). Finally, PrintArray() prints the new reversed array. You will declare your arrays in the main function, initialize to contain zeroes(0) and pass them as arguments to each function that requires to operate on them. Ensure that you provide the prototypes for each function, then write your main function followed by the 4 functions. You will ensure that the return values and the parameters for each function are well thought out and added to barebones function declaration mentioned above. Example interaction Please enter a series of numbers terminated by a 0: 34 56 78 11 4 1 99 0 The reversed array in descending order is: 99 78 56 34 11 4 1Explanation / Answer
#include <stdio.h>
void ReadArray(int a[], int size, int *count) {
int i;
printf("PLease enter a series of numbers terminated by a 0: ");
for(i=0; i<size; i++){
scanf("%d", &a[i]);
if(a[i] == 0){
break;
}
*count = *count +1;
}
}
void swap(int a[], int num1, int num2) {
int temp = a[num1];
a[num1] = a[num2];
a[num2] = temp;
}
int partition(int a[], int left, int right, int pivot) {
int leftPointer = left -1;
int rightPointer = right;
while(1) {
while(a[++leftPointer] < pivot) {
//do nothing
}
while(rightPointer > 0 && a[--rightPointer] > pivot) {
//do nothing
}
if(leftPointer >= rightPointer) {
break;
} else {
swap(a,leftPointer,rightPointer);
}
}
swap(a, leftPointer,right);
return leftPointer;
}
void SortArray(int a[], int left, int right) {
if(right-left <= 0) {
return;
} else {
int pivot = a[right];
int partitionPoint = partition(a,left, right, pivot);
SortArray(a,left,partitionPoint-1);
SortArray(a,partitionPoint+1,right);
}
}
void PrintArray(int a[], int size){
int i;
printf("The reverse array in desending order is: ");
for(i=0; i<size; i++){
printf("%d ", a[i]);
}
printf(" ");
}
void ReverseArray(int a[], int n, int b[]) {
int i,j=0;
for(i=n-1,j=0; i>=0; i--,j++){
b[i]=a[j];
}
}
int main()
{
int n=10;
int a[n];
int reverse[n];
int count = 0;
ReadArray(a, n, &count);
SortArray(a,0,count-1);
ReverseArray(a, count, reverse);
PrintArray(reverse,count);
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
PLease enter a series of numbers terminated by a 0:
34 56 78 11 1 4 99 0
The reverse array in desending order is:
99 78 56 34 11 4 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.