使用 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP