Home

Statistics Functions:
The Sum of Integers of a Series

 
int __fastcall SumInt(const int * Data, const int Data_Size);

The SumInt() function is used to calculate the total of a group of integral numbers. This function takes two arguments. The first, Data, is the name of the array that holds the numbers. The numbers must be integers. If you want to calculate a total of floating-point numbers, use the Sum() function. The second argument, Data_Size is the size of the array minus one.

Here is an example that simulates a company inventory to count business assets:

//---------------------------------------------------------------------------
#include <iostream.h>
#include <math.hpp>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused

int main(int argc, char* argv[])
{
	int Tables, Chairs, BookShelves, TrashCans,
	Desktops, Laptops, Printers, FaxMachines,
	Books, Pens, Pencils, Others;

	cout << "Company Inventory\nType the number of items of each category\n";
	cout << "Desktops: "; cin >> Desktops;
	cout << "Laptops: "; cin >> Laptops;
	cout << "Printers: "; cin >> Printers;
	cout << "Fax Machines: "; cin >> FaxMachines;
	cout << "Chairs: "; cin >> Chairs;
	cout << "Tables: "; cin >> Tables;
	cout << "Book Shelves: "; cin >> BookShelves;
	cout << "Books: "; cin >> Books;
	cout << "Trash Cans: "; cin >> TrashCans;
	cout << "Pens: "; cin >> Pens;
	cout << "Pencils: "; cin >> Pencils;
	cout << "Others: "; cin >> Others;

	int Assets[] = { Tables, Chairs, BookShelves, TrashCans,
			 Desktops, Laptops, Printers, FaxMachines,
			 Books, Pens, Pencils, Others };
	int Items = (sizeof(Assets)/sizeof(int)) - 1;
	int AllAssets = SumInt(Assets, Items);

	cout << "\nTotal Number of Items: " << AllAssets;

	cout << "\n\nPress Enter to send the inventory...";
	getchar();
	return 0;
}
//---------------------------------------------------------------------------
Company Inventory
Type the number of items of each category
Desktops: 12
Laptops: 2
Printers: 8
Fax Machines: 2
Chairs: 18
Tables: 14
Book Shelves: 10
Books: 20
Trash Cans: 15
Pens: 120
Pencils: 144
Others: 212

Total Number of Items: 577

Press Enter to send the inventory...
 
Home Copyright © 2004-2016, FunctionX, Inc.