Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;数组返回不正确的数字_C++_Arrays_Output - Fatal编程技术网

C++ C++;数组返回不正确的数字

C++ C++;数组返回不正确的数字,c++,arrays,output,C++,Arrays,Output,我的代码是一个程序,允许用户在数组中输入十个数字,然后程序显示输入的最大和最小数字。我的代码对于最小的数字非常有效,但是每次运行它时,对于最大的数字显示“4254729” 我从昨天开始就在做这件事,转换东西,尝试不同的输入,但什么都没用 #include <iostream> using namespace std; int main() { //defining my variables int numbers[10]; int smallest = 0

我的代码是一个程序,允许用户在数组中输入十个数字,然后程序显示输入的最大和最小数字。我的代码对于最小的数字非常有效,但是每次运行它时,对于最大的数字显示“4254729”

我从昨天开始就在做这件事,转换东西,尝试不同的输入,但什么都没用

#include <iostream>

using namespace std;

int main()
{
    //defining my variables
    int numbers[10];
    int smallest = 0;
    int largest = 0;
    int temporary = 0;

    //beginning of my for loop to let the user enter numbers into the array
    for (int i = 0; i < 10; i++)
    {
        cout << "Please enter number " << i+1 << " : "<< endl << endl;
        cin >> numbers[i];
        cout << endl;
    }

    //assining the smallest and largest numbers to the first numbers in the array
    smallest = numbers[0];
    largest = numbers[0];

    //beginning of for loop to compare the numbers entered
    for (int i = 1; i <= 10; i++)
    {
        temporary = numbers[i];
        if (temporary > largest)
            largest = temporary;

        if(temporary < smallest)
            smallest = temporary;
    }

    //cout statements to display the largest and smallest numbers entered
    cout << "The largest number entered is: " << largest << endl << endl;
    cout << "The smallest number entered is: " << smallest << endl << endl;

    return 0;
}
#包括
使用名称空间std;
int main()
{
//定义我的变量
整数[10];
int=0;
int最大=0;
int临时=0;
//开始我的for循环,让用户在数组中输入数字
对于(int i=0;i<10;i++)
{

cout如果您在问题中添加您的输入,Hi会更好,但是我修复了一些东西,可能会起作用

//beginning of for loop to compare the numbers entered
for (int i = 1; i < 10; i++) //I change i<=10 to <
{
    temporary = numbers[i];
    if (temporary > largest)
        largest = temporary;

    else if (temporary < smallest) //Here I put else
        smallest = temporary;
}
//用于比较输入数字的for循环的开头
对于(int i=1;i<10;i++)//i更改i最大值)
最大=临时;
else if(临时的<最小的)//我把else放在这里
最小=临时;
}

更改
for(int i=1;i红旗报警:
for(int i=1;i数组索引从
0
开始,而不是
1
@dededeecos,它解决了我的问题!非常感谢!它有效吗?如果是,请接受。我相信
for
应该是:
for(int i=0;i<10;i++)
,因为数组索引从0开始。语句:
for(int i=1;i<10;i++)
实际上将迭代9次,而不是10次。没有必要从i=0开始,bc在for循环之前已经这样做了。错误是您