Value of each element is accumulated in a total, which is displayed after all array elements have been displayed. - C++ Data Type

C++ examples for Data Type:Array

Description

Value of each element is accumulated in a total, which is displayed after all array elements have been displayed.

Demo Code

#include <iostream>
using namespace std;
int main()/*  w w w.  jav a  2  s .  c om*/
{
   const int MAXTEMPS = 5;
   int i, temp[MAXTEMPS], total = 0;
   for (i = 0; i < MAXTEMPS; i++)    // enter the temperatures
   {
      cout << "Enter a temperature: ";
      cin  >> temp[i];
   }
   cout << "\nThe total of the temperatures";
   for (i = 0; i < MAXTEMPS; i++)    // display and total the temperatures
   {
      cout << "  " << temp[i];
      total = total + temp[i];
   }
   cout << " is " << total << endl;
   return 0;
}

Result


Related Tutorials