-8

int の配列とそのサイズが渡されるプログラムを作成する必要があります。これにより、平均を超える数値が出力されます。誰かが私のためにこの質問を解決するのを助けることができますか? 私はここに座って一体何を求めているのだろうと思っています。私はまだプログラミングに慣れていないので、何をすべきかわかりません。申し訳ありませんが、私は混乱しているだけです。助けてくれる人に感謝します。これは私がこれまで持っているすべてです:

これが更新されたコードですが、複数の平均値が表示されない理由や、正しい出力値を取得する方法がまだわかりません。
編集: average() 関数のいくつかの int 値を float に変更しましたが、最後の合計値にはまだ問題があります

#include <iostream>
using namespace std;

int average(int values[],int size);

int main(){
int size;
int values[] = {1,2,3,4,5,6};
cout << "Please input the size of the array" << endl;
cin >> size;
int output = average(values, size);
if(values[size]>output){
    cout << "The values above average are: " << output << endl;
}

return 0;
}

int average(int values[],int size){
float temp=0.0;
for(int i=0;i<size;i++){
    temp += values[i];
}
float end=temp/size;

return end;
}
4

2 に答える 2

0

これは、投稿されたものよりも複雑でないソリューションです。主な機能: 実際にすべきことを行います:

#include <iostream>

template<size_t N>
float average( const int (&value)[N] ) {
    float total( 0.0f );
    // Sum all values
    for ( size_t index = 0; index < N; ++index )
        total += value[index];
    // And divide by the number of items
    return ( total / N );
}

int main() {
    int value[]  = { 1, 2, 3, 4, 5, 6 };
    // Calculate average value
    float avg = average( value );
    const size_t count = ( sizeof( value ) / sizeof( value[0] ) );
    // Iterate over all values...
    for ( size_t index = 0; index < count; ++index )
        // ... and print those above average
        if ( value[index] > avg )
            std::cout << value[index] << std::endl;

    return 0;
}

Ideoneでの実例。出力:

4
5
6
于 2013-11-15T00:14:58.767 に答える
0

intigers の配列を作成し、それを average 関数に渡すことになっていて、サイズを渡す必要があります (ループする回数がわかるように)。Average 関数のループで、すべての値を一時的な値に追加してから、関数に入力されたカウントで割ります。

//returns the average
int average(int Values[],int Size){
    //perhaps declare a temporary value here
    for(int i=0;i<Size;i++){
        //add all the values up and store in a temporary value
    }
    //here divide by Size and return that as the average
}

これ

if(values[size]>output){
    cout << "The values above average are: " << output << endl;
}

次のようなものに置き換える必要があります。

for(int i=0;i<size;i++){
    if(values[i]>output){
        cout << "The values above average are: " << values[i] << endl;
    }
}
于 2013-11-14T23:20:45.257 に答える