c語言實(shí)現(xiàn)找最大值最小值位置查找
找最大值最小值位置
從鍵盤任意輸入10個(gè)整數(shù),計(jì)算并輸出最大值和最小值及其它們?cè)跀?shù)組中的下標(biāo)位置。
程序運(yùn)行結(jié)果示例1:
Input 10 numbers:1 2 3 4 5 6 7 8 9 10↙
max=10,pos=9
min=1,pos=0
程序運(yùn)行結(jié)果示例2:
Input 10 numbers:2 4 5 6 8 10 1 3 5 7 9↙
max=10,pos=5
min=1,pos=6
程序:
#include <stdio.h> int FindMax(int a[], int n, int *pMaxPos); int FindMin(int a[], int n, int *pMinPos); int main() { int a[10], maxValue, maxPos, minValue, minPos, i; printf("Input 10 numbers:"); for (i=0; i<10; i++) { scanf("%d", &a[i]); // 輸入10個(gè)數(shù) } maxValue = FindMax(a, 10, &maxPos); // 找最大值及其所在下標(biāo)位置 minValue = FindMin(a, 10, &minPos); // 找最小值及其所在下標(biāo)位置 printf("max=%d,pos=%d\n", maxValue, maxPos); printf("min=%d,pos=%d\n", minValue, minPos); return 0; } //函數(shù)功能:求有n個(gè)元素的整型數(shù)組a中的最大值及其所在下標(biāo)位置,函數(shù)返回最大值 int FindMax(int a[], int n, int *pMaxPos) { int i, max; max = a[0]; //假設(shè)a[0]為最大值 *pMaxPos = 0; //假設(shè)最大值在數(shù)組中的下標(biāo)位置為0 for (i=1; i<n; i++) { if (a[i] > max) { max = a[i]; *pMaxPos = i; //pMaxPos指向最大值數(shù)組元素的下標(biāo)位置 } } return max ; } //函數(shù)功能:求有n個(gè)元素的整型數(shù)組a中的最小值及其所在下標(biāo)位置,函數(shù)返回最小值 int FindMin(int a[], int n, int *pMinPos) { int i, min; min = a[0]; //假設(shè)a[0]為最小 *pMinPos = 0; //假設(shè)最小值在數(shù)組中的下標(biāo)位置為0 for (i=1; i<10; i++) { if (a[i] < min) { min = a[i]; *pMinPos = i; //pMinPos指向最小值數(shù)組元素的下標(biāo)位置 } } return min ; }
到此這篇關(guān)于c語言實(shí)現(xiàn)找最大值最小值位置查找的文章就介紹到這了,更多相關(guān)c語言 最大值最小值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C++實(shí)現(xiàn)MySQL數(shù)據(jù)庫連接池
這篇文章主要為大家詳細(xì)介紹了如何使用C++實(shí)現(xiàn)MySQL數(shù)據(jù)庫連接池,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以了解下2024-03-03Linux下Select多路復(fù)用實(shí)現(xiàn)簡(jiǎn)易聊天室示例
大家好,本篇文章主要講的是Linux下Select多路復(fù)用實(shí)現(xiàn)簡(jiǎn)易聊天室示例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12從txt中讀入數(shù)據(jù)到數(shù)組中(fscanf)的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄獜膖xt中讀入數(shù)據(jù)到數(shù)組中(fscanf)的實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12QT實(shí)現(xiàn)自定義Http客戶端的示例代碼
這篇文章主要為大家詳細(xì)介紹了QT如何實(shí)現(xiàn)自定義Http客戶端的,可以實(shí)現(xiàn)支持get,post請(qǐng)求方式;支持連接超時(shí)處理;支持網(wǎng)絡(luò)錯(cuò)誤,嘗試重連等功能,感興趣的小伙伴可以學(xué)習(xí)一下2022-11-11