C++ C++;函数在endl(内存变量?)之后打印不需要的数字

C++ C++;函数在endl(内存变量?)之后打印不需要的数字,c++,function,output,C++,Function,Output,除了一个小问题外,这段代码对我来说非常有效。在调用我的find_min和find_max函数后,代码还会打印出一个看似随机的数字。我认为这与通过引用传递有关,它是一个内存值或其他东西。有人能解释一下,告诉我怎么摆脱它吗?谢谢 #include <iostream> #include <fstream> #include <cmath> #include <cstdlib> using namespace std; //get_avg funct

除了一个小问题外,这段代码对我来说非常有效。在调用我的
find_min
find_max
函数后,代码还会打印出一个看似随机的数字。我认为这与通过引用传递有关,它是一个内存值或其他东西。有人能解释一下,告诉我怎么摆脱它吗?谢谢

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

//get_avg function will get the average of all temperatures in the array by using for loop to add the numbers

double get_avg(int* arr, int n) {
    double res = 0.0;
    for(int i = 0; i<n; i++){
        res += arr[i];

    }
    return (res/n); //average
}

void get_array(int* arr){
    int i;

    for(i=0; i<25; i++){
        arr[i] = (rand() % 100) + 1;
    }
}

//prints the results of get_avg in and get_array in table format, then finds difference between avg and temp.

void print_output(int* arr) {
    double avg = get_avg(arr, 24);
    cout << "The average is: " << avg << endl;
    cout << "Position\tValue\t\tDifference from average\n\n";
    char sign;
    for(int i = 0; i < 24; i++){
        sign = (avg > arr[i])?('+'):('-');
        cout << i << "\t\t" << arr[i] << "\t\t" << sign << fabs(avg-arr[i]) << endl;
    }
}

//finds the minimum function using a for loop

int find_min(int* arr, int n) {
    int low = arr[0];
    int index = 0;
    for(int i = 1; i<n; i++){
        if(arr[i] < low){
            low = arr[i];
            index = i;

        }
    }
    cout << "The position of the minimum number in the array is "<< index <<" and the value is " << low << endl;    
}

//finds the maximum function using a for loop

int find_max(int* arr, int n){
    int hi = arr[0];
    int index;
    for(int i = 1; i<n; i++) {
        if(arr[i] > hi) {
            hi = arr[i];
            index = i;
        }
    }
    cout << "The position of the maximum number in the array is "<< index <<" and the value is " << hi << endl;
}



int main(){

    int arr[24]; //declares array


    get_array(arr);
    print_output(arr);
    cout << find_min(arr, 24) << endl; 
    cout << find_max(arr, 24) << endl;

    cout << endl;


    // wrap-up

    cout << "This program is coded by Troy Wilms" << endl;  // fill in your name

    // stops the program to view the output until you type any character

    return 0;

}
#包括
#包括
#包括
#包括
使用名称空间std;
//get_avg函数将通过使用for循环来添加数字,从而获得阵列中所有温度的平均值
双平均值(整数*arr,整数n){
双精度=0.0;
对于这两行中的(int i=0;i:

cout << find_min(arr, 24) << endl; 
cout << find_max(arr, 24) << endl;

cout正如在另一个答案中已经指出的那样,问题在于不从声明为返回值的函数返回值。因此,这里的大部分解决方案是阅读和理解编译器的警告

更好的是,使用选项
-Werror
将编译器警告变成错误(例如,使用GNU g++时)

其他注释建议使用“实际”C++类型,例如“代码> STD::vector < /Cord>”作为“数组”的容器。而不是使用C风格的整数数组,在迭代集合时使用一些魔法常数。这是正确的,甚至可以进一步说明:了解C++标准库并使用它!为什么当函数已经可用时,获取集合的最小值和最大值的函数?

要避免这些陷阱并提供更像C++的版本,请参见以下示例:

#include <algorithm>
#include <ctime>
#include <ios>  // I/O manipulators
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

using Container = std::vector<int>;

// get_avg function will get the average of all temperatures in the container
double get_avg(const Container& c) {
    // force promotion from int to double by using 0.0 as initial value!
    return std::accumulate(c.begin(), c.end(), 0.0) / c.size();
}


// fill container c with 'count' random values, originally get_array()
void fill(size_t count, Container& c) {
    std::srand(std::time(0));

    size_t i = 0;
    while (i++ < count) {
        c.push_back((rand() % 100) + 1);
    }
}

//prints the results of get_avg in and get_array in table format, then finds difference between avg and temp.
void print_output(const Container& c) {
    double avg = get_avg(c);

    std::cout << "The average is: " << avg << endl;
    std::cout << "Position\tValue\t\tDifference from average\n\n";

    size_t idx = 0;
    for(auto e : c){
        cout << idx++ << "\t\t" << e << "\t\t" << std::showpos << avg - e << std::noshowpos << std::endl;
    }
}

// There is std::minmax_element that gets both minimum and maximum value at once!
void print_min_max(const Container& c) {
    auto mm = std::minmax_element(c.begin(), c.end());
    std::cout << "The position of the minimum number in the container is "<< mm.first - c.begin() <<" and the value is " << *(mm.first) << std::endl;
    std::cout << "The position of the maximum number in the container is "<< mm.second - c.begin() <<" and the value is " << *(mm.second) << std::endl;
}   

int main() {

    Container c;
    const size_t count = 24;

    fill(count, c);
    print_output(c);
    print_min_max(c);

    std::cout << endl;

    // wrap-up

    std::cout << "This program is coded by Troy Wilms" << endl;  // fill in your name

    // stops the program to view the output until you type any character

    return 0;
}
#包括
#包括
#包括//I/O操纵器
#包括
#包括
#包括
使用名称空间std;
使用Container=std::vector;
//get_avg函数将获得容器中所有温度的平均值
平均双倍进货(集装箱和集装箱){
//使用0.0作为初始值,强制从int升级到double!
返回std::累计(c.begin(),c.end(),0.0)/c.size();
}
//用“count”随机值填充容器c,最初获取_数组()
空隙填充(尺寸、数量、容器和c){
std::srand(std::time(0));
尺寸i=0;
while(i++STD::您的代码< FordMIN < /COD>和<代码> FINDMAX 被声明为返回<代码> int <代码>,但不返回任何内容。还考虑使用<代码> STD::数组< /COD>(或<代码> STD::向量< /代码>)及其<代码> >代码>代替魔术数字。(i=0;i使用
endl
有时会产生问题。在任何情况下,使用
\n
都是更好的方法。@Aliseperri Amin,在任何情况下,它绝对不是更好。
endl
刷新缓冲区,有时您需要刷新缓冲区。因此它们不同,一个不能替代另一个。好奇的是:为什么标准会这样做是否不将其限定为错误?我的意思是,声明函数返回某些内容,而在函数体中不返回任何内容。隐式的
return somegarbage
print\u输出中似乎从来都不是有用的行为
24
显然是打印错误。)@叶甫根尼:你说得对,谢谢你的提示。我现在甚至消除了这个神奇的数字。