-1

私は一生、次の何が問題なのかを理解することはできませんが、findLargest および findSmallest (および findAverage も同様) 関数が正常に機能せず、正しくない値を返します。どうしたの?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int findLargest(int array[], int arraySize){
int largest = array[0]; //I set the largest to the first member of the array initially
for (int i = 0; i < arraySize; ++i){
    if (array[i] > largest){
        largest = i;
    }
}
return largest;
}

int findSmallest(int array[], int arraySize){
int smallest = array[0]; //I set the smallest to the first member of the array initially
for (int i = 0; i < arraySize; ++ i){
    if (array[i] < smallest){
        smallest = i;
    }
 }
return smallest;
}

int findAverage(int array[], int arraySize){
int total = 0;
for (int i = 0; i < arraySize; ++i){
    total += array[i];
}
int average = total/arraySize;

return average;
}

void display(int array[], int arraySize){

cout << "\nThe values for the array are: \n\n";

for (int i = 0; i < arraySize; ++i){
    cout << array[i] << endl;
   }
}



int main(){

const int size = 50;
int taker[size];
srand(time(NULL));

for (int i = 0; i < size; ++i){
    taker[i] = rand() % 100;  //generate 50 random numbers for the array taker
}

int largest = findLargest(taker, size);
int smallest = findSmallest(taker, size);
int average = findAverage(taker, size);

cout << "The largest entry was " << largest << endl;
cout << "The smallest entry was " << smallest << endl;
cout << "The average for all the entries is " << average << endl;

display(taker, size);

}
4

4 に答える 4

2

インデックスを返したい場合。

int findLargest(int array[], int arraySize){
int largest = array[0]; //I set the largest to the first member of the array initially
int largestindex=0;
for (int i = 0; i < arraySize; ++i){
    if (array[i] > largest){
        largestindex=i;
        largest = array[i];
    }
}
return largestindex;
}

値を返したい場合。

int findLargest(int array[], int arraySize){
int largest = array[0]; //I set the largest to the first member of the array initially
for (int i = 0; i < arraySize; ++i){
    if (array[i] > largest){
        largest = array[i];
    }
}
return largest;
}
于 2013-06-30T02:06:09.403 に答える
1

平均の関数は見栄えがしますが、最大と最小の場合は、インデックスだけでなくインデックスの値を使用する必要があります。

最大:

int findLargest(int array[], int arraySize){
   int largest = array[0]; //I set the largest to the first member of the array initially
   for (int i = 0; i < arraySize; ++i){
      if (array[i] > largest){
          largest = array[i];
      }
   }
    return largest;
}

最小:

int findSmallest(int array[], int arraySize){
    int smallest = array[0]; //I set the smallest to the first member of the array initially
    for (int i = 0; i < arraySize; ++ i){
        if (array[i] < smallest){
           smallest = array[i];
        }
    }
    return smallest;
}

平均の関数は良さそうです。

于 2013-06-30T02:07:12.400 に答える
0

では、しないようにfindLargest設定largestしますiarray[i]

于 2013-06-30T02:06:13.673 に答える
0

smallest = array[i];largest=array[i];それぞれの機能で設定する必要があります。

于 2013-06-30T02:08:15.963 に答える