在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
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短