带指针的数组求和我的求和有问题

Posted

技术标签:

【中文标题】带指针的数组求和我的求和有问题【英文标题】:Array Sum with Pointers i have a problem in the sum 【发布时间】:2021-05-15 18:04:16 【问题描述】:

我有这个代码。我有一个问题。当我为这些数字"1,1,1,1,1" 运行它时,它会正确回答我,但是当我使用这些数字"2,1,3,2,2" 或任何其他数字时,它会错误地回答我。有什么问题?

#include <iostream>
using namespace std;

int main() 
    int size = 5;
    int array1[size];
    int i, j, *p;

    int sum = 0;
    p = &array1[j];

    for (int i = 0; i < size; i++) 
        cout << "give next number";
        cin >> array1[i];
        cout << "\n";
    

    cout << "the array is:"
         << "\n";
    for (int j = 0; j < size; j++) 
        cout << array1[j] << "\n";
        sum = sum + *p;
    
    cout << "the sum of array elements is: " << sum;

    return 0;

【问题讨论】:

p=&amp;array1[j];j 没有确定值时是无稽之谈。当您稍后更改j 的值时,它也不会神奇地修复(您甚至没有这样做,因为for(int j... 循环声明了它自己的本地j)。 那我该如何解决呢?正确的代码如何......我是新手,所以我不太了解 size 应该是 const 以防止可变长度数组。并完全放下指针。只需使用普通的数组索引。 你帮了我很多。谢谢你! 我会的!我将研究代码,这次我将尝试制作一个与众不同的新代码。我感谢我得到的所有答案,我尊重你们所有人。我是来自互联网的自学者,我之所以学习这个,是因为我想对这种编程有一个小小的体验。所以我不会复制你的答案。非常感谢你!如果我遇到错误,我会再次发布。 【参考方案1】:

所以你有一个问题

p = &array1[j];

您正在做的是获取数组的jth 元素的地址。在您的情况下,j 未初始化,这会导致 UB,因为 j 可能包含任何变量。

要解决此问题,您可以将 j 初始化为 0 (j = 0)。或者只是获取数组中第一个元素的地址,您可以执行以下操作:

p = array;

然后是你的循环,它是arr[j] 地址的顶点值,正如我上面所说的 UB。

cout << "the array is:" << "\n";
for (j = 0; j < size; j++) 
    cout << array1[j] << "\n";
    sum = sum + *(p + j);

你的问题是你一直在添加array1[0]。 (也就是说,如果您将 j 初始化为 0)。

其他需要注意的是,您要重新声明 ij

int i, j, *p;
...
for (int i = 0; ...)
...
for (int j = 0; ...)

你可以这样做

for (i = 0; ...)
...
for (j = 0; ...)

将已声明的变量设置为0

这是整个程序:

#include <iostream>

int main() 
    int size = 5;
    int array1[size];
    int i, j, *p; 

    int sum = 0;
    // p = &array1[j]; // UB j not initialized but used

    /* solution 1
    j = 0;
    p = &array1[j]
    */
                                                                                                                                                                                                                                                                                                                              
    // solution 2 which is same as solution 1
    p = array1; // gets address of array[0]
    for (i = 0; i < size; i++)     // no need for `int` in front of i
                                    // i is already declared above
                                    // my preference is to declare i here
                                    // and remove declaration above
        std::cout << "give next number";
        std::cin >> array1[i];
        std::cout << "\n";
       

    std::cout << "the array is:"
        << "\n";
    for (j = 0; j < size; j++)  // same as above
        std::cout << array1[j] << "\n";
        sum = sum + *(p + j); 
       
    std::cout << "the sum of array elements is: " << sum;

    return 0;

输入:

give next number5

give next number4

give next number3

give next number2

give next number1

输出

the array is:
5
4
3
2
1
the sum of array elements is: 15

【讨论】:

首先感谢您的回答,但我是 C++ 的初学者,所以如果您能在照片上显示我或复制粘贴我的程序并进行更改以便我可以运行,我将不胜感激它并更好地理解它?因为我在改变 j=0 后失去了你 你想念我。我没有欺骗任何人我的问题中的代码是我制作的代码,我没有学校作业或我只是从互联网上为自己学习以获得更多经验的东西,我是一个女孩。 我会研究代码,这次我会尝试制作一个不同的新代码。我感谢我得到的所有答案,我尊重你们所有人。所以请不要用我的问题来评判我。非常感谢!

以上是关于带指针的数组求和我的求和有问题的主要内容,如果未能解决你的问题,请参考以下文章

EXCEL 里带字母的数字求和如何解?

C++ 数组元素中 相邻的两个元素求和 a[0]+a[1] a[2]+a[3] 依此类推

用指针实现二维数组行列求和

对数组中的 2 个连续元素求和

带连接的求和函数是用 mysql 将 laravel ORM 中条目的总和乘以 mysql

fortran关于数组内部函数的问题