在C ++中使用指针算法的数组总和
这是一个C++程序,用于使用指针找出数组元素的总和。
算法
Begin
Initialize the array elements with values from user input.
Initialize s = 0
Loop for i = 0 to
s = s + *(ptr + i)
Print the sum value in variable s.
End范例程式码
#include<iostream>
using namespace std;
int main() {
int a[7], i, s = 0;
int *ptr;
cout << "Enter the Numbers: ";
for (i = 0; i < 7; i++) {
cin >> a[i];
}
ptr = a;
for (i = 0; i < 7; i++) {
s = s + *(ptr + i);
}
cout << "\nSum of Elements of Array: " << s;
}输出结果
Enter the Numbers: 1 2 3 4 5 6 7 Sum of Elements of Array: 28
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西