简体   繁体   中英

Finding how to remove largest and second largest in array in c

The assignment is as followed and is coded in Keil which is an ide for c.

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);
    }
}

There is one mistake in FindIndexOfLargest function Change it to:

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 (largest);          //NOTE CHANGE HERE:Return largest number
}

As the function name suggest if you want to return the index of largest number then change it to:

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 = i;  // NOTE CHANGE HERE:new maximum index
    }
  }
  return (largest);          //NOTE CHANGE HERE:Return largest index
}

As pointed out by Vagish There is a mistake in the function FindIndexOfLargest you are doing this

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 (largest); // This is returning the largest no rather than returning the index 
}

so do it like this

unsigned short int FindIndexOfLargest(unsigned short int class[],unsigned short int start, unsigned short int end){
unsigned short int largest,i,index;
  largest = 0;  // smallest possible value
  for(i=0; i<end; i++){
    if(class[i] > largest){
      largest = class[i];  // new maximum
       index=i;
    }
  }
  return (index);          //This is returning the index of the largest
}

There is also the problem in the Remove Entry function do it like this

unsigned short int RemoveEntry(unsigned short int class[], unsigned short int size, int position) {
unsigned short int c;
    for (c =position; c <size;++c ) {
                 class[c] = class[c+1];
     }
     size-=1;
     return size; //Return size of new array after removing the data
}

Well then there remains the last function, please try to find the logical error there and if not able let us know :) Hope these answer helps

unsigned short int FindIndexOfLargest(unsigned short int tempArr[],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(tempArr[i] > tempArr[largest]){
          largest = i;  // new maximum
        }
    }
    return (largest); //Change return value class[i] to largest;
}

void 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];   <<< array pass by pointer,so no need to return;  *change unsigned short int to void.

void RemoveLargestEntry(unsigned short int tempArr[], unsigned short int size) {
    RemoveEntry(class, size, FindIndexOfLargest(tempArr, 0, 100)); // This is the same with RemoveEntry function;
}

unsigned short int RemoveSecondLargest(unsigned short int tempArr[],unsigned short int start, unsigned short int end) {
    unsigned short int Largest,PositionLargest,SecondLargest,PositionSecondLargest;
    Largest = tempArr[FindIndexOfLargest(tempArr[],0,end)];
    RemoveLargestEntry(tempArr,end);      /*Remove largest and sencond  largest number in the Array */
    SecondLargest = FindIndexOfLargest(tempArr,0,end);
    RemoveLargestEntry(tempArr,end-1);                                
    tempArr[99] = Largest;  // Add number at the end of the array;
    return(SecondLargest);
}

Function 4 is only for your provide array. If the situation where the number in the array is less than 100(eg 30),function 4 will be useless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM