C语言编程
如何查找一个数组第二大数字的下标,假设数组是{0 1 3 4 5 2 8}
#include int array[] = { 11, 1, 3, 4,119, 12, 119, 5, 2, 8 }; int main(int argc, char** argv) { int max_index = 0; int sec_index = 0; int i = 0; /* search for the largest one */ for( i=1; iarray[max_index]) { max_index = i; } } /* the initial sec_index should not equal to max_index。
*/ if( max_index == 0 ) sec_index = 1; /* search for the second largest one */ for( i=0; iarray[sec_index]) && (array[i] != array[max_index]) ) { sec_index = i; } } printf("theindexoflargestnumberinthearrayis% lueis:%d\n",max_index,array[max_index]); printf("theindexofsecondlargestnumberinthearrayis% lueis:%d\n",sec_index,array[sec_index]); getch(); }。
用冒泡排序, 循环两次就可以了
答:一、一维数组的定义 类型说明符 数组名[常量表达式]; 说明: 1、类型说明符用来定义数组中各个数据元素的类型。在任何一个数组中,数据 , 元素的类型都是一致...详情>>