HELP Intro to Programming C Pseudocode assignment: This assignment is about pseu
ID: 3778813 • Letter: H
Question
HELP Intro to Programming C Pseudocode assignment:
This assignment is about pseudocode. A scaffold is provided and only one small section of code is needed to be filled in to complete.
Pseudocode
Pseudocode is a high-level description of a computing process. It usually looks like code because pseudocode follows some of the common conventions of programming languages. It is not code, however, as it does not follow any particular programming language syntax. Instead, it is intended to be read by humans instead of computers. Many common algorithms are described by pseudocode. This allows us to have a common descriptor for an algorithm that can then be implemented in any programming language. It provides an outline for the solution that a programmer can then fill in.
The Problem Statement and Program Setup
You have a collection of gnomes. Each gnome has a number so that they can be placed in numerical order. You wish to write a program to sort these gnomes.
Program Setup
A scaffold of the solution has been created for you and will be posted after this description. Do not modify the code that is already present. Instead, fill in the GnomeSort function marked like this: /*** ... ***/ This function will perform a gnome sort to order the gnomes based on their assigned values. Implement the pseudocode below by translating it to C syntax in your function.
Function Prototype
You must use this prototype to receive credit for the assignment.
// Pre-condition: length is the size of the array gnomes.
// Post-condition: gnomes will be sorted in numerical order, from smallest to largest.
void GnomeSort(int gnomes[], int length);
Gnome Sort Pseudocode
procedure gnomeSort(a[])
pos := 1
while pos < length(a)
if (a[pos] >= a[pos-1])
pos := pos + 1
else
swap a[pos] and a[pos-1]
if (pos > 1)
pos := pos - 1
end if
end if
end while
end procedure
PROGRAM SCAFFOLD
#include
#define SIZE 5
#define PRINT 1
void swap(int* ptrA, int*ptrB);
void print(int array[], int length);
void fillArray(int array[], int length);
void GnomeSort(int gnomes[], int length);
int isSorted(int array[], int length);
int main() {
int a[SIZE];
fillArray(a, SIZE);
if (PRINT) print(a, SIZE);
GnomeSort(a, SIZE);
if (PRINT) print(a, SIZE);
// Print out the final result.
if (isSorted(a, SIZE))
printf("The sort worked properly. ");
else
printf("There was a problem with the sort. ");
return 0;
}
// Prints out values in array[0]...array[length-1].
void print(int array[], int length) {
int i;
for (i=0; i printf("%d ", array[i]);
printf(" ");
return;
}
// Sorts the items array[0] through array[length-1] using the Gnome Sort algorithm.
void GnomeSort(int gnomes[], int length) {
/*** FILL IN YOUR CODE HERE ***/
}
// Swaps the variables pointed to by ptrA and ptrB.
void swap(int* ptrA, int* ptrB) {
int temp = *ptrA;
*ptrA = *ptrB;
*ptrB = temp;
return;
}
// Fills array[0] through array[length-1] with integers from a specified file
void fillArray(int array[], int length) {
int i;
char filename[20];
FILE * ifp = NULL;
printf("What is the name of the file? ");
scanf("%s", filename);
ifp = fopen(filename, "r");
for (i=0; i fscanf(ifp, "%d", &array[i]);
fclose(ifp);
return;
}
// Returns 1 iff array[0] through array[length-1] are in sorted order from smallest to largest
int isSorted(int array[], int length) {
int i;
// Return false if a pair of consecutive values is out of order.
for (i=0; i if (array[i] > array[i+1])
return 0;
return 1;
}
Explanation / Answer
void GnomeSort(int gnomes[], int length) {
int pos=1;
while(pos<length){
if (gnomes[pos] >= gnomes[pos-1]){
pos=pos-1;
}
else {
swap(&gnomes[pos],&gnomes[pos-1]);
if(pos>1){
pos=pos-1;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.