0

また、別の 10 要素配列を使用して、5% 増加した後の各高さを計算して表示する必要があります。何か案は?申し訳ありません。配列を使用するのはこれが初めてです。

#include <iostream>

using namespace std;

int main()
{
    int MINheight = 0;
    double height[10];
    for (int x = 0; x < 10; x = x + 1)
    {
        height[x] = 0.0;
    }

    cout << "You are asked to enter heights of 10 students. "<< endl;
    for (int x = 0; x < 10; x = x + 1)
    {
        cout << "Enter height of a student: ";
        cin >> height[x];  
    }

    system("pause"); 
    return 0;
}
4

2 に答える 2

3

次のように単純にループします。

MINheight = height[0];
for (int x = 1; x < 10; x++)
{
   if (height[x] < MINheight)
   {
      MINheight = height[x];
   } 
}
std::cout << "minimum height " << MINheight <<std::endl;

補足: 大文字で始まるローカル変数に名前を付けるべきではありません。x配列インデックスとして使用するのもちょっと奇妙ですが、どちらもうまく機能しますが、スタイルは良くありません。

std::min_element次のように使用することもできます。

std::cout << *std::min_element(height,height+10) << std::endl; 
                               //^^using default comparison

高さを増やした別の配列に要素を配置して表示するには、次の操作を行います。

float increasedHeights[10] = {0.0};
for (int i = 0; i < 10;  ++i)
{
   increasedHeights[i] = height[i] * 1.05;
}

//output increased heights
for (int i = 0; i < 10;  ++i)
{
   std::cout << increasedHeights[i] << std::endl;
}
于 2013-04-17T03:16:37.147 に答える
1

基本的に、入力されている最小値を追跡できるため、次のようになります。

cout << "You are asked to enter heights of 10 students. "<< endl;

MINheight = numerical_limits<int>::max
for (int x = 0; x < 10; x = x + 1)
{
    cout << "Enter height of a student: ";
    cin >> height[x];  
    if(height[x] < MINheight)MINheight = height[x];
}
cout << "Minimum value was: " << MINheight << "\n";

これが行うことは、その値が可能な最大値を持つ変数を作成し、ユーザーが新しい値を入力するたびに、現在の最小値よりも小さいかどうかを確認し、そうであれば保存します。次に、最後に現在の最小値を出力します。

于 2013-04-17T03:16:26.587 に答える