在c中查找如何删除数组中的最大和第二大数组

时间:2015-03-03 03:55:31

标签: c arrays algorithm

分配如下,并在Keil中编码,这是c的ide。

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

编写函数以在阵列上执行以下操作。您必须将数组传递给函数才能获得全部功劳。

找到最大数字FindInexOfLargest()的索引。 在数组RemoveEntry()中删除给定索引的指定条目。请注意,删除条目后出现在数组中的数据将向左移动,以便data[i]变为data[i+1]。 删除数组RemoveLargestEntry()中的最大条目。数组被突变以删除最大的条目。函数返回最大条目的值。 删除数组中的第二大条目RemoveSecondLargestEntry()。该函数应返回数组中第二大条目的数值,并删除该值。 为3和4编写的函数应使用1和2中编写的函数。对于完全信用,在第3部分和第4部分中编写的函数中不应出现循环。

这是我的代码无法正常工作,我无法找出错误的位置。这是一个逻辑错误我不知道在哪里。

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

3 个答案:

答案 0 :(得分:0)

FindIndexOfLargest函数中有一个错误将其更改为:

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
}

如函数名称所示,如果要返回最大数字的索引,请将其更改为:

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
}

答案 1 :(得分:0)

正如Vagish指出的那样,函数FindIndexOfLargest中有一个错误

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 
}

所以这样做

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
}

“删除条目”功能中也存在此问题

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
}

那么还有最后一个功能,请尝试在那里找到逻辑错误,如果不能让我们知道:) 希望这些答案有所帮助

答案 2 :(得分:-1)

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

功能4仅适用于您提供的阵列。如果阵列中的数字小于100(例如30)的情况,则功能4将是无用的。