[英]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.