简体   繁体   English

为什么此 C++ 代码的 output 错误? (hackerrank的问题之一)

[英]Why am I getting the wrong output for this C++ code? (one of the problem of hackerrank)

This is the program for printing out sum of array elements.这是打印数组元素总和的程序。 It is showing run time error.它显示运行时错误。 The output is coming out to be 0 instead of printing out the sum of the elements. output 出来是 0 而不是打印出元素的总和。

#include<iostream.h>
using namespace std;
void simpleArraySum()   
{
    int ar[100],n,i,sum=0;

    for(i=0;i<n;i++)
    {
        sum=sum + ar[i];
    }

    cout<<sum;
}
int main()
{
    int ar[100],n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
    }

    simpleArraySum();
    return 0;
}

On this line in your main:在你主要的这一行:

int ar[100], n;

You create an array of 100 elements.您创建一个包含 100 个元素的数组。 You later fill that array using cin您稍后使用cin填充该数组

for(int i = 0 ; i < n ; i++)
{
    cin >> ar[i];
}

Then you do nothing with that array.然后你对那个数组什么都不做。 You are not calculating any sum.你没有计算任何总和。 You let that array go, forgotten.你让那个数组 go,忘记了。

Then, you call a simpleArraySum function.然后,调用simpleArraySum function。 That function is creating an entirely new, distinct array . function 正在创建一个全新的、不同的数组

//  v-----v------There
int ar[100],n,i,sum=0;

That array has no value assigned to it.该数组没有分配任何值。 In fact, reading from it is undefined behavior.实际上,从中读取是未定义的行为。

What you want is to receive that array in the arguments of your function:您想要的是在 function 的 arguments 中接收该数组:

void simpleArraySum(int* ar, int n) {
    // ...
}

And call it like that in your main:并在你的主目录中这样称呼它:

simpleArraySum(ar, 100);

You can avoid the issues of arrays and functions by not using them:您可以通过不使用它们来避免 arrays 和函数的问题:

int main()
{
  int quantity = 0;
  std::cin >> quantity;
  int sum = 0;
  int value;
  while (std::cin >> value)
  {
     sum += value;
  }
  std::cout << sum << "\n";
  return EXIT_SUCCESS;
}

In simpleArraySum , the variable n is uninitialized.simpleArraySum中,变量n未初始化。 So this loop:所以这个循环:

for(i=0;i<n;i++)

invokes undefined behavior when reading from n .n读取时调用未定义的行为。

Also, you are summing a different array in the function, than the one you read in mian .此外,您在 function 中对一个不同的数组求和,而不是在mian中读取的数组。 It seems that you need to pass in the array from main to this function:看来您需要将数组从main传递给这个 function:

void simpleArraySum(int *ar, int n) {

and call it like this:并这样称呼它:

simpleArraySum(ar, n);

Finally, you don't even need a function for this, since there is an existing algorithm std::accumulate that you can use:最后,您甚至不需要 function ,因为您可以使用现有的算法std::accumulate

cout << std::accumulate(ar, ar + n, 0);

In the function, you're adding the elements of ar which is local to the function simpleArraySum() and is not of the array ar that is local to main() .在 function 中,您将添加ar的元素,该元素是 function simpleArraySum()的本地元素,并且不是main()本地的数组ar

So, pass the array and its length to the function and return its sum.因此,将数组及其长度传递给 function 并返回其总和。 Here is your corrected code:这是您更正的代码:

#include<iostream>
using namespace std;
void simpleArraySum(int ar[], int n)   
{
    int i, sum = 0;

    for(i=0;i<n;i++)
    {
        sum=sum + ar[i];
    }

    cout<<sum;
}
int main()
{
    int ar[100],n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
    }

    simpleArraySum(ar, n);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提交 Kattis “OddGnome”问题时出错。 我在本地得到正确的输出,我的代码有什么问题? C++ - Getting error on submission for Kattis "OddGnome" problem. I am getting the right output locally, what is wrong with my code? C++ 为什么在用 C++ 编写这个文件处理代码后得到空白输出? - Why am i getting blank output after writing this filehandling code in c++? 我没有得到想要的 output 反向数字代码 C++ - I am not getting desired output in reverse a number code in C++ 为什么我在 HackerRank 上得到的输出与我的 IDE 输出不同? - Why I am getting different output on HackerRank than the output of my IDE? 为什么我在C ++中不断得到错误的输出 - Why do I keep getting the wrong output in c++ 为什么我在C ++代码中遇到此错误 - Why I am getting this error in c++ code 我刚刚开始使用 c++,但我在hackerRank 中遇到此代码错误 - I have just started c++ but I'm getting error in hackerRank for this code c/c++hackerrank,我的代码正在运行,但我得到了 0,38/10 分(不知道为什么)代码有效 - c/c++ hackerrank, my code is working but im getting 0,38/10 points(dunno why) code works C++ *对角差*问题,我哪里出错了? - C++ *Diagonal Difference* Problem, Where Am I Going Wrong? 为什么在 C++ 中连接 output 中的变量时出现错误? - Why I am getting error while concatenating variables in output in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM