查找数组中的元素总和

时间:2022-08-09 21:23:24

I have this program which worked find in my college lab, but when I run it at my home it gives different result

我有这个程序可以在我的大学实验室中找到,但是当我在家里运行它时会得到不同的结果

#include <stdio.h>
int main(int argc, char* argv[]) {
        const int size=100;
        int n, sum=0;
        int* A = (int*)malloc( sizeof(int)*size );
        for (n=size-1; n>0; n--)
                A[n] = n;

        for (n=0;n<size; n++)
                sum += A[n];

        printf ("sum=%d\n", sum);
        return 0;

}

I'm expecting 4950 as result but I keep getting different result like 112133223. Why is this happening ?

我期待4950结果,但我一直得到不同的结果,如112133223.为什么会发生这种情况?

5 个解决方案

#1


7  

You are not assigning value to A[0]. As a result of this A[0] will have garbage value which is getting added to sum.

您没有为A [0]赋值。因此,A [0]将具有垃圾值,并将其添加到总和中。

Change

更改

for (n=size-1; n>0; n--)
               ^^^

to

for (n=size-1; n>=0; n--)
               ^^^^

Also you need to free the dynamically allocated memory by doing:

您还需要通过以下操作释放动态分配的内存:

free(A);

before you return.

在你回来之前。

#2


2  

You never assign a value to A[0] - the first loop ends to quickly. However you do include this unassigned value in the summation.

您永远不会为A [0]赋值 - 第一个循环快速结束。但是,您在总和中包含此未分配的值。

#3


1  

A[0] is undefined:

A [0]未定义:

for (n=size-1; n>0; n--)

for(n = size-1; n> 0; n--)

should be

应该

for (n=size-1; n>=0; n--)

for(n = size-1; n> = 0; n--)

#4


1  

Just because you didn't put any value in first element, you had garbage there. thus, some random number in sum.

只是因为你没有在第一个元素中放置任何值,你就有了垃圾。因此,一些随机数总和。

#include <stdio.h>
#include<stdlib.h> //for malloc
int main(int argc, char* argv[]) {
        const int size=100;
        int n, sum=0;
        int* A = (int*)malloc( sizeof(int)*size );
        for (n=size-1; n>=0; n--) //you forgot '='
                A[n] = n;

        for (n=0;n<size; n++)
                sum += A[n];

        printf ("sum=%d\n", sum);
        return 0;

}

#5


0  

it worked fine for me. I executed it using GCC. this is the program i ran:

它对我来说很好。我用GCC执行了它。这是我跑的程序:

#include <stdio.h>
#include <malloc.h>
int main(int argc, char* argv[]) {
    const int size=100;
    int n, sum=0;
    int* A = (int*)malloc( sizeof(int)*size );
    for (n=size-1; n>0; n--)
            A[n] = n;

    for (n=0;n<size; n++)
            sum += A[n];

    printf ("sum=%d\n", sum);
    return 0;
}

#1


7  

You are not assigning value to A[0]. As a result of this A[0] will have garbage value which is getting added to sum.

您没有为A [0]赋值。因此,A [0]将具有垃圾值,并将其添加到总和中。

Change

更改

for (n=size-1; n>0; n--)
               ^^^

to

for (n=size-1; n>=0; n--)
               ^^^^

Also you need to free the dynamically allocated memory by doing:

您还需要通过以下操作释放动态分配的内存:

free(A);

before you return.

在你回来之前。

#2


2  

You never assign a value to A[0] - the first loop ends to quickly. However you do include this unassigned value in the summation.

您永远不会为A [0]赋值 - 第一个循环快速结束。但是,您在总和中包含此未分配的值。

#3


1  

A[0] is undefined:

A [0]未定义:

for (n=size-1; n>0; n--)

for(n = size-1; n> 0; n--)

should be

应该

for (n=size-1; n>=0; n--)

for(n = size-1; n> = 0; n--)

#4


1  

Just because you didn't put any value in first element, you had garbage there. thus, some random number in sum.

只是因为你没有在第一个元素中放置任何值,你就有了垃圾。因此,一些随机数总和。

#include <stdio.h>
#include<stdlib.h> //for malloc
int main(int argc, char* argv[]) {
        const int size=100;
        int n, sum=0;
        int* A = (int*)malloc( sizeof(int)*size );
        for (n=size-1; n>=0; n--) //you forgot '='
                A[n] = n;

        for (n=0;n<size; n++)
                sum += A[n];

        printf ("sum=%d\n", sum);
        return 0;

}

#5


0  

it worked fine for me. I executed it using GCC. this is the program i ran:

它对我来说很好。我用GCC执行了它。这是我跑的程序:

#include <stdio.h>
#include <malloc.h>
int main(int argc, char* argv[]) {
    const int size=100;
    int n, sum=0;
    int* A = (int*)malloc( sizeof(int)*size );
    for (n=size-1; n>0; n--)
            A[n] = n;

    for (n=0;n<size; n++)
            sum += A[n];

    printf ("sum=%d\n", sum);
    return 0;
}