The assignment is as followed and is coded in Keil.
Cut and paste the following data into a Keil project that is based on Lab 5 (or include the UART (#include "uart.h") for simple I/O):
unsigned short data[100] = { 58473, 33594, 38638, 26741, 13018, 29262, 16377, 12354, 46079, 57240, 48949, 34054, 16212, 58485, 6198, 38678, 22525, 51012, 43489, 8861, 54291, 21524, 7166, 22698, 39899, 27113, 30443, 14888, 27935, 40035, 48710, 18067, 36008, 12644, 56319, 15852, 54685, 61789, 57030, 4763, 10655, 24656, 60363, 23712, 28474, 31274, 39647, 56166, 8219, 47413, 22201, 3129, 25630, 36027, 4499, 56525, 32743, 9380, 22102, 51009, 16309, 16589, 26322, 65279, 22780, 26002, 41101, 26082, 13389, 59504, 15784, 33416, 57970, 8519, 57819, 34406, 40864, 31575, 52154, 60214, 39910, 43107, 64825, 40284, 60148, 27287, 38245, 49930, 54062, 50668, 30553, 27904, 38960, 49407, 10508, 62147, 33019, 3047, 33750, 18024};
Write functions to preform the following operations on the array. You must pass the array to functions in order to receive full credit.
Find the index of the largest number, FindInexOfLargest. Remove a specified entry, given an index, in the array, RemoveEntry. Note that data appearing in the array after the removed entry is to be shifted to the left, so that data[i] becomes data[i+1]. Remove the largest entry in an array, RemoveLargestEntry. The array is mutated to remove the largest entry. The value of the largest entry is returned by the function. Remove the second largest entry in an array, RemoveSecondLargestEntry. The function should return the numerical value of the second largest entry in an array, and remove that value. The functions written for 3 and 4 should use the functions written in 1 and 2. For full credit, no loops should appear in the functions written in part 3 and 4.
This is my code that isn't working right and I can't find out where the mistake is. It's a logical error somewhere I just don't know where.
unsigned short int data[100] = {
58473, 33594, 38638, 26741, 13018, 29262, 16377, 12354, 46079,
57240, 48949, 34054, 16212, 58485, 6198, 38678, 22525, 51012,
43489, 8861, 54291, 21524, 7166, 22698, 39899, 27113, 30443,
14888, 27935, 40035, 48710, 18067, 36008, 12644, 56319, 15852,
54685, 61789, 57030, 4763, 10655, 24656, 60363, 23712, 28474,
31274, 39647, 56166, 8219, 47413, 22201, 3129, 25630, 36027,
4499, 56525, 32743, 9380, 22102, 51009, 16309, 16589, 26322,
65279, 22780, 26002, 41101, 26082, 13389, 59504, 15784, 33416,
57970, 8519, 57819, 34406, 40864, 31575, 52154, 60214, 39910,
43107, 64825, 40284, 60148, 27287, 38245, 49930, 54062, 50668,
30553, 27904, 38960, 49407, 10508, 62147, 33019, 3047, 33750, 18024};
// ***** 2. Global Declarations Section *****
// FUNCTION PROTOTYPES: Each subroutine defined
unsigned short int FindIndexOfLargest(unsigned short int class[], unsigned short int start, unsigned short int end);
unsigned short int RemoveEntry(unsigned short int class[], unsigned short int size, int position);
unsigned short int RemoveLargestEntry(unsigned short int class[], unsigned short int size);
unsigned short int RemoveSecondLargest(unsigned short int class[], unsigned short int start, unsigned short int end);
// ***** 3. Subroutines Section *****
int main (void) {
printf("Largest: %u\n", FindIndexOfLargest(data, 0, 100));
printf("Removing: %u\n", RemoveEntry(data, 100, 7));
printf("Removing Largest: %u\n", RemoveLargestEntry(data, 100));
printf("Removing Second Largest: %u\n", RemoveSecondLargest(data, 0, 100));
}
unsigned short int FindIndexOfLargest(unsigned short int class[],unsigned short int start, unsigned short int end){
unsigned short int largest,i;
largest = 0; // smallest possible value
for(i=0; i<end; i++){
if(class[i] > largest){
largest = class[i]; // new maximum
}
}
return (class[i]);
}
unsigned short int RemoveEntry(unsigned short int class[], unsigned short int size, int position) {
unsigned short int c;
for (c = (position - 1) ; c < (size - 1) ; c++ ) {
class[c] = class[c+1];
}
return class[c];
}
unsigned short int RemoveLargestEntry(unsigned short int class[], unsigned short int size) {
return RemoveEntry(class, size, FindIndexOfLargest(class, 0, 100));
}
unsigned short int RemoveSecondLargest(unsigned short int class[], unsigned short int start, unsigned short int end) {
unsigned short int Largest = FindIndexOfLargest(class, start, end);
unsigned short int firstLarge = FindIndexOfLargest(class, start, (Largest-1));
unsigned short int secondLarge = FindIndexOfLargest(class, (Largest+1), end);
if (firstLarge > secondLarge) {
return RemoveEntry(class, end, firstLarge);
}
else {
return RemoveEntry(class, end, secondLarge);
}
}
Aucun commentaire:
Enregistrer un commentaire