C++ 从数组输出的数字与使用递归函数输入的数字不同? 我正在做递归数组函数,它对数字进行计数,并将它们相加在一起,将它们返回到递归函数中。该函数似乎实际工作,但我最初输入数组的数字是1,2,3,4,5,但程序告诉我这些数字是49,50,51,52,53。。。非常困惑为什么会发生这种情况,任何帮助或见解都将不胜感激。谢谢大家! #include <iostream> using namespace std; const int SIZE = 5;//size of array int sum(int [], int);//recursive function int main() { int sumArray[SIZE] = { '1', '2', '3', '4', '5'};//array with predetermined values cout << sumArray[0] << endl;//49 cout << sumArray[1] << endl;//50 cout << sumArray[2] << endl;//51 cout << sumArray[3] << endl;//52 cout << sumArray[4] << endl;//53 cout << sum(sumArray, SIZE) << endl;//displays the amount 255 (5 elements added) system("pause"); return 0; } int sum(int sumArray[], int size) { if (size == 1) return sumArray[size - 1]; else { cout << size << endl; return sumArray[size - 1] + sum(sumArray, size - 1); } } #包括 使用名称空间std; 常数int SIZE=5//数组大小 整数和(整数[],整数)//递归函数 int main() { int-sumArray[SIZE]={'1','2','3','4','5'};//具有预定值的数组 cout

C++ 从数组输出的数字与使用递归函数输入的数字不同? 我正在做递归数组函数,它对数字进行计数,并将它们相加在一起,将它们返回到递归函数中。该函数似乎实际工作,但我最初输入数组的数字是1,2,3,4,5,但程序告诉我这些数字是49,50,51,52,53。。。非常困惑为什么会发生这种情况,任何帮助或见解都将不胜感激。谢谢大家! #include <iostream> using namespace std; const int SIZE = 5;//size of array int sum(int [], int);//recursive function int main() { int sumArray[SIZE] = { '1', '2', '3', '4', '5'};//array with predetermined values cout << sumArray[0] << endl;//49 cout << sumArray[1] << endl;//50 cout << sumArray[2] << endl;//51 cout << sumArray[3] << endl;//52 cout << sumArray[4] << endl;//53 cout << sum(sumArray, SIZE) << endl;//displays the amount 255 (5 elements added) system("pause"); return 0; } int sum(int sumArray[], int size) { if (size == 1) return sumArray[size - 1]; else { cout << size << endl; return sumArray[size - 1] + sum(sumArray, size - 1); } } #包括 使用名称空间std; 常数int SIZE=5//数组大小 整数和(整数[],整数)//递归函数 int main() { int-sumArray[SIZE]={'1','2','3','4','5'};//具有预定值的数组 cout,c++,arrays,function,recursion,C++,Arrays,Function,Recursion,您实际上被放入数字的数组ASCII代码中:“1”实际上是一个带有代码49的字符,它被转换为int 49。编写如下: int sumArray[SIZE] = { 1, 2, 3, 4, 5 }; 这就是所谓的“整体促销”部分。谢谢您的帮助:)

您实际上被放入数字的数组ASCII代码中:“1”实际上是一个带有代码49的字符,它被转换为int 49。编写如下:

int sumArray[SIZE] = { 1, 2, 3, 4, 5 };

这就是所谓的“整体促销”部分。

谢谢您的帮助:)