To find the total of an array element C++ code sample
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: To find the sum of an array element in C++ programming
To show: How to sum up the array element in C++ programming
// a program to find the total of all the elements in array y
#include <iostream>
using namespace std;
// replace every n occurrences with 7
#define n 7
int main(void)
{
int i, total = 0, y[n] = {6,9,2,4,5,23,12};
for (i=0; i<n; i++)
{
// display the array contents
cout<<y[i]<<" ";
// do the summing up
total = total + y[i];
}
// display the result...
cout<<"\nSum of 7 numbers in an array is = "<<total<<endl;
return 0;
}
Output example:
6 9 2 4 5 23 12
Sum of 7 numbers in an array is = 61
Press any key to continue . . .