最低的数组值

时间:2014-11-08 22:21:57

标签: c++

我的程序应该得到随机数并将它们放入数组中。那么它应该找到最低,最高,总和和平均值。一切都有效,除了最低功能。请帮忙! 编辑:发布整个程序

  int main()
{
    int nums[SIZE] = { 0 };
    int smallest;
    int highest;
    int sum;
    double avg;

    putNums(nums);
    getNums(nums, SIZE);
    getLowest(nums, SIZE, smallest);
    getHighest(nums, SIZE, highest);
    getSum(nums, SIZE, sum);
    getAvg(nums, SIZE, sum, avg);
    cout << "Smallest number of array: " << nums[smallest] << endl;
    cout << " Highest number of array: " << nums[highest] << endl;
    cout << "Sum of the array: " << sum << endl;
    cout << "Average of the array: " << setprecision(2) << fixed << showpoint << avg << endl;
}
void putNums(int nums[])
{
    ofstream outFil;    // output file object
    string filNam;      // output file name
    srand(static_cast<unsigned int>(time(0))); // seed random number generator  
    int num;            // random number to be generated
    int cnt = 1;        // count of random numbers

    cout << "To generates a file of " << SIZE << " random numbers\n";
    cout << " enter your output file name: ";   // "nums.txt"
    cin >> filNam;
    outFil.open(filNam.c_str());
    if (outFil) {
        for (int k = 0; k < SIZE; ++k) {    // generate and write numbers 
            num = MIN + rand() % MAX;       // generate random number
            cout << cnt << ". " << num << endl;
            outFil << num << endl;
            nums[cnt] = num;
            ++cnt;                          // increment count of numbers
        } // endfor
    }
    else {
        cout << "Open error on file " << filNam << endl;
        exit(1);
    } // endif
    outFil.close();        // close the file
    cout << "\n -- Done - file closed! --\n\n";
}
void getNums(int nums[], int SIZE)
{
    cout << "Your " << SIZE << " number(s) are listed: \n";
    for (int cnt = 1; cnt <= SIZE; cnt++){
        cout << nums[cnt] << endl;
    }
}
int getLowest(int nums[], int size, int & smallest)
{
    smallest = 0;
    int lowest = nums[0];
    for (int cnt = 0; cnt <= SIZE; ++cnt){
        if (nums[cnt] < lowest){
            lowest = nums[cnt];
            smallest = cnt;
        }
    }
    return smallest;
}
int getHighest(int nums[], int SIZE, int & highest)
{
    highest = 0;
    int largest = nums[0];
    for (int cnt = 0; cnt < SIZE; ++cnt){
        if (nums[cnt] > largest){
            largest = nums[cnt];
            highest = cnt;
        }
    }
    return highest;
}
int getSum(int nums[], int size, int & sum)
{
    sum = 0;
    for (int cnt = 0; cnt < SIZE; ++cnt){
        sum += (nums[cnt]);
    }
    return sum;
}
double getAvg(int nums[], int size, int sum, double & avg)
{
    avg = 0;
    avg = static_cast<double>(sum) / SIZE;
    return avg;
}

4 个答案:

答案 0 :(得分:1)

由于lowest代表索引而smallest代表该值,因此您应该返回smallest变量而不是lowest

应该是:

return lowest;

答案 1 :(得分:1)

源代码通常很好但有概念和技术错误,在这里我给你发送一个运行

的解决方案
#include <iostream>
#include <fstream>

using namespace std;

#define SIZE 500
#define MIN 1
#define MAX 10000


void putNums(int *nums)
{
    ofstream outFil;    // output file object
    string filNam;      // output file name
    srand(static_cast<unsigned int>(time(0))); // seed random number generator
    int num;            // random number to be generated

    cout << "To generates a file of " << SIZE << " random numbers\n";
    cout << " enter your output file name: ";   // "nums.txt"
    cin >> filNam;
    outFil.open(filNam.c_str());
    if (outFil) {
        for (int k = 0; k < SIZE; k++) {    // generate and write numbers
            num = MIN + rand() % MAX;       // generate random number
            cout << (k + 1) << ". " << num << endl;
            outFil << num << endl;
            nums[k] = num;
        } // endfor
    }
    else {
        cout << "Open error on file " << filNam << endl;
        exit(1);
    } // endif
    outFil.close();        // close the file
    cout << "\n -- Done - file closed! --\n\n";
}
void getNums(int *nums, int size)
{
    cout << "Your " << size << " number(s) are listed: \n";
    for (int cnt = 0; cnt < size; cnt++){
        cout << nums[cnt] << endl;
    }
}
int getLowest(int *nums, int size)
{
    int lowest = nums[0];
    for (int cnt = 1; cnt < size; cnt++){
        if (nums[cnt] < lowest){
            lowest = nums[cnt];
        }
    }
    return lowest;
}
int getHighest(int *nums, int size)
{
    int largest = nums[0];
    for (int cnt = 1; cnt < size; cnt++){
        if (nums[cnt] > largest){
            largest = nums[cnt];
        }
    }
    return largest;
}
int getSum(int *nums, int size)
{
    int sum = 0;
    for (int cnt = 0; cnt < size; cnt++)
    {
        sum += nums[cnt];
    }
    return sum;
}
double getAvg(int nums[], int size, int sum)
{
    return static_cast<double>(sum) / size;
}

int main(int argc, const char * argv[])
{

    int *nums;
    int smallest;
    int highest;
    int sum;
    double avg;

    nums = (int*)malloc(sizeof(int) * SIZE);
    putNums(nums);
    getNums(nums, SIZE);
    smallest = getLowest(nums, SIZE);
    highest = getHighest(nums, SIZE);
    sum = getSum(nums, SIZE);
    avg = getAvg(nums, SIZE, sum);
    cout << "Smallest number of array: " << smallest << endl;
    cout << " Highest number of array: " << highest << endl;
    cout << "Sum of the array: " << sum << endl;
    cout << "Average of the array: " << fixed << showpoint << avg << endl;
    free(nums);
    return 0;
}

答案 2 :(得分:0)

您在getLowest中有内存覆盖:

 for (int cnt = 0; cnt <= SIZE; ++cnt){

在循环中,您在最后一次迭代中访问nums[SIZE],因此您可以在其边界之外访问数组。

应该是这样的:

  for (int cnt = 0; cnt < SIZE; ++cnt){

答案 3 :(得分:-1)

试试这个

int getLowest(int nums[])
{
   int lowest = nums[0];
   for(int idx = 0; idx < SIZE; idx++)
   {
      if(nums[idx] < lowest)
         lowest = nums[idx];
   }

   return lowest;
}

我假设SIZE是一个恒定的全局,你返回的是最低的,所以你不必通过引用传递它