Running Basic Statistics in C++

This example demonstrates launching an instance of Statistica, gathering system memory information, customizing a spreadsheet, and running basic statistics.

/*The following example will retrieve the total and available amounts of memory available (categorized by physical, page file, and virtual memory). Next, descriptive statistics are run on the two variables (total memory and available memory). Finally, the variables are totaled and the sum is appended to the spreadsheet.*/

//BEGIN Windows Specific inclusions #include <windows.h> #include <comdef.h> //needed for COM /*The "#import" directive imports the class library information of the specified file.  In this case, the file "statist.exe" will give us access to the Statistica interface and its documents' library.  You will either need to include this file in your project directory, or hard code the full path to the file in the #import directive. Lastly, note that "#import" is a Visual C++ compiler specific directive; if you are using a different compiler, then please consult your compiler's documentation on how to import a COM interface.*/ #import "statist.exe" #import "sta_bas.dll" //Exclude rarely-used stuff from Windows headers #define WIN32_LEAN_AND_MEAN //END Windows Specific inclusions

#define MEGABYTE 1048576 //2^20 #include <cstdio>

using namespace Statistica; using namespace STABasicStatistics;

int main()

{

//retrieve information about the current memory status MEMORYSTATUS Memory; ::GlobalMemoryStatus (&Memory);

//Physical memory long TotalPhyMem = static_cast<long>(Memory.dwTotalPhys/MEGABYTE); long FreePhyMem = static_cast<long>(Memory.dwAvailPhys/MEGABYTE);

//Page file long TotalPageFile = static_cast<long>(Memory.dwTotalPageFile/MEGABYTE); long FreePageFile = static_cast<long>(Memory.dwAvailPageFile/MEGABYTE);

//Virtual memory long TotalVirtMem = static_cast<long>(Memory.dwTotalVirtual/MEGABYTE); long FreeVirtMem = static_cast<long>(Memory.dwAvailVirtual/MEGABYTE);

long TotalAvailMem = TotalPhyMem + TotalPageFile + TotalVirtMem; long TotalFreeMem = FreePhyMem + FreePageFile + FreeVirtMem;

//Launch Statistica //Initialize COM ::CoInitialize(NULL); Statistica::_ApplicationPtr pApp;

//initialize a new Statistica object through COM HRESULT hr = ::CoCreateInstance(__uuidof(Statistica::Application),NULL,CLSCTX_SERVER, __uuidof(Statistica::_Application),reinterpret_cast<void**>(&pApp));

//verify that COM was able to create an instance of Statistica if (FAILED(hr))

{ ::MessageBox(NULL,"Unable to intialize Statistica", "Initialization Failure",MB_ICONHAND); return EXIT_FAILURE; }

try

{ //create a new spreadsheet named "Current Memory" _SpreadsheetPtr pSpreadSheet = pApp->Spreadsheets->New("Current Memory"); pSpreadSheet->Header->PutValue("Total and Available Computer Memory");

//increase the width of the cases pSpreadSheet->PutCaseNameWidth(1.5);

//rename the first two variables pSpreadSheet->PutVariableName(1,"Total"); pSpreadSheet->PutVariableName(2,"Available");

//rename the first three cases pSpreadSheet->PutCaseName(1,"Physical Memory"); pSpreadSheet->PutCaseName(2,"Page File Memory"); pSpreadSheet->PutCaseName(3,"Virtual Memory");

//fill the spreadsheet with our calculated data pSpreadSheet->PutValue(1,1,TotalPhyMem); pSpreadSheet->PutValue(1,2,FreePhyMem); pSpreadSheet->PutValue(2,1,TotalPageFile); pSpreadSheet->PutValue(2,2,FreePageFile); pSpreadSheet->PutValue(3,1,TotalVirtMem); pSpreadSheet->PutValue(3,2,FreeVirtMem);

//remove the extra cases and variables pSpreadSheet->DeleteVariables(3,10); pSpreadSheet->DeleteCases(4,10); pSpreadSheet->Visible = true;

//run descriptive statistics on the dataset _AnalysisPtr pBasDiag = pApp->Analysis(scBasicStatistics); pBasDiag->Run(); BasDescriptiveStatisticsPtr pBasDesc = pBasDiag->Dialog; //analyze both variables pBasDesc->PutVariables("1 2"); //all of the defaults except std. dev. will be used pBasDesc->StandardDeviation = false; //also include sum, range, and median pBasDesc->Sum = true; pBasDesc->Range = true; pBasDesc->Median = true;

//process the analysis output pBasDiag->GetRouteOutput(pBasDesc->Summary.GetInterfacePtr())->PutVisible(true); pBasDiag->Close();

//append our own custom statistics (sums) to the dataset pSpreadSheet->AddCases(3,1); pSpreadSheet->PutCaseName(4,"Totals:"); pSpreadSheet->PutValue(4,1,TotalAvailMem); pSpreadSheet->PutValue(4,2,TotalFreeMem); }

catch(_com_error Err)

{ ::MessageBox(NULL,Err.Description(),pApp->Name,MB_ICONHAND); return EXIT_FAILURE; }

//clean up after the COM initialization pApp.Release(); ::CoUninitialize(); return EXIT_SUCCESS; }