關於程式c++的指標運算..請各位大大幫幫我..
老師出了兩題程式給大家做...不過..下面這一題..時在不知道如何下手...請各位大大幫忙一下ˇ
試撰寫一程式,利用指標的算術運算來找出一維整數陣列中,元素最大值的索引值與最小值的索引值(一維整數陣列的元素值請自行設定)。
1 個解答
評分
- ☆ Minarsih 。。。★Lv 51 0 年前最佳解答
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int num[10]={11,34,22,12,44,35,11,22,10,29};
int size=10;
int *ptr=num;
int maxIndex=0, minIndex=0;
for (int i=1; i<size;i++)
{
if (*(ptr+maxIndex) < *(ptr+i)) maxIndex=i;
if (*(ptr+minIndex) > *(ptr+i)) minIndex=i;
}
//索引值從1算起,則答案是 maxIndex+1, minIndex+1
cout << "最大值的索引值(從0算起): " << maxIndex <<endl;
cout << "最小值的索引值(從0算起): " << minIndex <<endl;
system("PAUSE");
return 0;
}
還有問題?馬上發問,尋求解答。