2016-11-26 5 views
0

私は配列内に存在する最小の値を検索し、その値を出力するためにforループを使用しています。その値が配列(0〜9)のどの位置にあるかを出力したいのですが、どうすればいいでしょうか?Cの配列内の値の位置を出力する方法

int smallest = array[0]; 

for (counter = 0; counter < 10; counter++) { 
    if (smallest > array[counter]) { 
     smallest = array[counter]; 
    } 
} 

printf("The smallest value stored within the array is %d", smallest); 

答えて

1

条件は以下のように真である場合は、ちょうどたびに「カウンタ」の値を格納する(「0」に初期化)別の変数が必要になります。高速な応答のための

int smallest = array[0]; 

int position = 0; 

for (counter = 0; counter < 10; counter++) { 
    if (smallest > array[counter]) { 
     smallest = array[counter]; 
     position = counter; 
    } 
} 

printf("The smallest value stored within the array is %d and position = %d", smallest, position); 
+0

おかげで、それが働いて、私は助けに感謝します。 – Gaarsin

+0

@ Gaarsin、Btw、私は速かったが、心配しないで! –

+0

@FarzadSalimiJazi、それは誰がより速いのではないが、OPがより良く理解するのを助ける人だ。 – StoryTeller

0

このような意味です!

int smallest = array[0]; 
int index = 0; 
for (counter = 0 ; counter < 10; counter++) { 
    if (smallest > array[counter]) { 
     smallest = array[counter]; 
     index = counter; 
    } 
} 

printf("The smallest value stored within the array is %d in %d", smallest, index); 
関連する問題