formatting help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 40
Reputation: coolmel55 is an unknown quantity at this point 
Solved Threads: 1
coolmel55 coolmel55 is offline Offline
Light Poster

formatting help

 
0
  #1
Nov 3rd, 2004
Need a bit of help formatting my output. I'm not even sure that this can be done. The output from the following code looks like this:

Here is your output!
********************
rval = 0.954316
tval = 9.03351
p = 0.999997
Two tailed value = 5.716140e-006

Ok is there anyway I can make the Two tailed value output look like this:
Two tailed value = 1.80 * 10^-5 (you know ^ -5 is the exponential)

  1.  
  2. // Simpson.h: interface for the CSimpson class.
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5.  
  6.  
  7. #ifndef CSIMPSON_H
  8. #define CSIMPSON_H
  9.  
  10. #include "Node.h"
  11.  
  12.  
  13. class CSimpson
  14. {
  15. public:
  16. CSimpson();
  17. ~CSimpson();
  18. double calcSimpson(double low, double high, double N, double W, double dof);
  19. double calcRval(CNode *sNode, double &dof);
  20. double norm(double val);
  21. double tDist(double val, double dof);
  22. double gamma(double val);
  23.  
  24. private:
  25. double myVar;
  26. double PI;
  27. };
  28.  
  29. #endif
  30.  
  31. // Node.h: interface for the CNode class.
  32. //
  33. //////////////////////////////////////////////////////////////////////
  34.  
  35. #ifndef CNODE_H
  36. #define CNODE_H
  37.  
  38. class CNode
  39. {
  40. public:
  41. //Functions of the class CNode
  42. CNode();
  43. ~CNode();
  44. void newNode(CNode **beginNode, double newInt1, double newInt2);
  45. void displayList (CNode *beginNode);
  46. int isEmpty (CNode *beginNode);
  47. double calculateSD(CNode *bNode, double &B0, double &B1, double guess, double &dof);
  48.  
  49. //Varialbles of the class
  50. double int1;
  51. double int2;
  52. CNode *next;
  53.  
  54. };
  55.  
  56. #endif
  57.  
  58. // Simpson.cpp: implementation of the CSimpson class.
  59. //
  60. //////////////////////////////////////////////////////////////////////
  61.  
  62. #include "Simpson.h"
  63. #include <iostream>
  64. #include <math.h>
  65.  
  66. //////////////////////////////////////////////////////////////////////
  67. // Construction/Destruction
  68. //////////////////////////////////////////////////////////////////////
  69.  
  70. CSimpson::CSimpson()
  71. {
  72. myVar = 0.39894;
  73. PI = 3.1415927;
  74. }
  75.  
  76. CSimpson::~CSimpson()
  77. {
  78.  
  79. }
  80.  
  81. double CSimpson::calcSimpson(double low, double high, double N, double W, double dof)
  82. {
  83.  
  84. double sum = 0;
  85. double dist = 0;
  86. double originalLow;
  87. int multiply = 2;
  88.  
  89. originalLow = low;
  90.  
  91. if (low < 0)
  92. {
  93. low = (low * (-1));
  94. }
  95.  
  96. sum = norm(low) + norm(high);
  97.  
  98. for (int i = 1; i < (N); i++)
  99. {
  100. if((i % 2) == 0)
  101. {
  102. multiply = 2;
  103. }
  104. else
  105. {
  106. multiply = 4;
  107. }
  108. dist = (low + ( i * W));
  109. sum = sum + (multiply * (norm(dist)));
  110. }
  111.  
  112. if (originalLow < 0)
  113. {
  114. sum = 0.5 - (sum * (W/3));
  115. }
  116. else
  117. {
  118. sum = 0.5 + (sum * (W/3));
  119. }
  120. return sum;
  121. }
  122.  
  123. double CSimpson::norm(double val)
  124. {
  125. double temp;
  126.  
  127.  
  128. temp = exp(-(pow(val, 2) / 2));
  129. temp = myVar * temp;
  130.  
  131. return temp;
  132. }
  133.  
  134. double CSimpson::tDist(double val, double dof)
  135. {
  136. double temp = 0;
  137.  
  138. temp = pow(1 + (pow(val,2)/dof),(((dof +1)/2) * (-1)));
  139. return temp;
  140.  
  141. }
  142.  
  143. double CSimpson::gamma(double val)
  144. {
  145. double temper = val;
  146. double summer = 1;
  147.  
  148. while ((temper - 1) > 0){
  149. if((temper - 1) == 0.5){
  150. summer = 1.7724539 * 0.5 * summer;
  151. }
  152. else {
  153. summer = (summer * (temper - 1));
  154. }
  155. temper = temper - 1;
  156. }
  157.  
  158. return summer;
  159. }
  160.  
  161. double CSimpson::calcRval(CNode *sNode, double &dof)
  162. {
  163. CNode *thisPtr;
  164. thisPtr = sNode;
  165.  
  166. int n = 0;
  167. double xSum = 0;
  168. double ySum = 0;
  169. double xy = 0;
  170. double xSquare = 0;
  171. double ySquare = 0;
  172. double xAve = 0;
  173. double yAve = 0;
  174. double rVal = 0;
  175. double var = 0;
  176. double std = 0;
  177. double bot = 0;
  178.  
  179. while(thisPtr != NULL)
  180. {
  181. n++;
  182. xy = xy + ((thisPtr->int1) * (thisPtr->int2));
  183. xSum = xSum + thisPtr ->int1;
  184. ySum = ySum + thisPtr ->int2;
  185. xSquare = xSquare + pow(thisPtr->int1, 2);
  186. ySquare = ySquare + pow(thisPtr->int2, 2);
  187. thisPtr = thisPtr -> next;
  188. }
  189.  
  190. xAve = xSum / n;
  191. yAve = ySum / n;
  192.  
  193. dof = n;
  194. rVal = ((n * xy) - (xSum * ySum)) / (sqrt(((n * xSquare) - pow(xSum, 2)) * ((n * ySquare) - pow(ySum, 2))));
  195.  
  196. return rVal;
  197. }
  198.  
  199. // Node.cpp: implementation of the CNode class.
  200. //
  201. //////////////////////////////////////////////////////////////////////
  202.  
  203. #include "math.h"
  204. #include "string.h"
  205. #include "Node.h"
  206. #include <iostream>
  207. using namespace std;
  208.  
  209. CNode::CNode()
  210. {
  211. }
  212. CNode::~CNode()
  213. {
  214. }
  215. void CNode::newNode (CNode **bNode, double newInt1, double newInt2)
  216. {
  217. CNode *newPtr;
  218. CNode *endPtr;
  219. CNode *thisPtr;
  220.  
  221. newPtr = new CNode;
  222.  
  223. if(newPtr)
  224. {
  225. newPtr->int1 = newInt1;
  226. newPtr->int2 = newInt2;
  227. newPtr->next = NULL;
  228. endPtr = NULL;
  229. thisPtr = *bNode;
  230.  
  231. while ((thisPtr != NULL))
  232. {
  233. endPtr = thisPtr;
  234. thisPtr = thisPtr -> next;
  235. }
  236.  
  237. if (endPtr == NULL)
  238. {
  239. newPtr -> next = *bNode;
  240. *bNode = newPtr;
  241. }
  242. else
  243. {
  244. endPtr -> next = newPtr;
  245. newPtr -> next = thisPtr;
  246. }
  247. }
  248. else
  249. {
  250. cout << newInt1 << " not inserted, there was no available memory.\n";
  251. }
  252. }
  253.  
  254. int CNode::isEmpty (CNode *bNode)
  255. {
  256. if(bNode == NULL)
  257. {
  258. return 1;
  259. }
  260. else
  261. {
  262. return 0;
  263. }
  264. }
  265.  
  266.  
  267.  
  268. double CNode::calculateSD(CNode *bNode, double &B0, double &B1, double guess, double &dof)
  269. {
  270. CNode *thisPtr;
  271. thisPtr = bNode;
  272.  
  273. int n = 0;
  274. double x = 0;
  275. double y = 0;
  276. double xy = 0;
  277. double xSquare = 0;
  278. double xAve = 0;
  279. double yAve = 0;
  280. double range = 0;
  281. long double var = 0;
  282. long double std = 0;
  283. long double bot = 0;
  284.  
  285. while (thisPtr != NULL)
  286. {
  287. n++;
  288. xy = xy + ((thisPtr -> int1) * (thisPtr -> int2));
  289. x = x + thisPtr -> int1;
  290. y = y + thisPtr -> int2;
  291. xSquare = xSquare + pow(thisPtr -> int1,2);
  292. thisPtr = thisPtr -> next;
  293. }
  294.  
  295. xAve = x/n;
  296. yAve = y/n;
  297.  
  298. B1 = (xy - (n * xAve * yAve)) / (xSquare - (n * (pow(xAve, 2))));
  299. B0 = yAve - (B1 * xAve);
  300.  
  301. thisPtr = bNode;
  302.  
  303. while(thisPtr != NULL)
  304. {
  305. var = var + pow((thisPtr->int2 - B0 - (B1 * thisPtr->int1)),2);
  306. bot = bot + pow((thisPtr->int1 - xAve),2);
  307. thisPtr= thisPtr->next;
  308. }
  309.  
  310. var = var / (n-2);
  311. std = sqrt(var);
  312.  
  313. cout << "\nStandard Deviation = " << std;
  314.  
  315. dof = n;
  316. range = std * sqrt(1 + (1/n) + ((pow((guess - xAve),2)) / bot));
  317.  
  318. return range;
  319. }
  320.  
  321. void CNode::displayList(CNode *bNode)
  322. {
  323. if (bNode == NULL)
  324. {
  325. cout << "\nList starts at address "
  326. << bNode << endl;
  327. cout << "(List is empty)\n";
  328. cout << "List end at address "
  329. << bNode << endl;
  330. }
  331. else
  332. {
  333. cout << "\nList starts at address "
  334. << bNode << endl;
  335.  
  336. while (bNode != NULL)
  337. {
  338. cout << "Data " << (double) bNode->int1 << ", "
  339. << (double) bNode->int2 << " -> ";
  340. cout << bNode->next << endl;
  341. bNode = bNode->next;
  342. }
  343.  
  344. cout << "List end at address "
  345. << bNode << endl;
  346. }
  347.  
  348. }
  349.  
  350. #include "math.h"
  351. #include "string.h"
  352. #include <iostream>
  353. #include "Simpson.h"
  354. #include "Node.h"
  355. using namespace std;
  356.  
  357. int main()
  358. {
  359. CNode N1;
  360. CSimpson S1;
  361. CNode *startPtr = NULL; //Node Pointer to start of List
  362. double temp = 1; //Temporary storage for linked list data
  363. double temp2 = 1;
  364. double xLow = 0;
  365. double W;
  366. double p;
  367. double twoTail;
  368. double dof;
  369. double rVal;
  370. double tVal;
  371. int N = 20;
  372.  
  373.  
  374. cout << "Welcome to the correlation machine!!" << endl;
  375. cout << "************************************";
  376. cout << "\nHere is how it works please enter the Actual New and Changed LOC which";
  377. cout << "\nwill serve as your x value, followed by the Development Hours which";
  378. cout << "\nwill serve as your y value, when you would like conclude the list enter";
  379. cout << "\nzero for both values!" << endl;
  380. cin >> temp >> temp2;
  381.  
  382. while (temp != 0)
  383. {
  384. N1.newNode(&startPtr, temp, temp2);
  385. cin >> temp >> temp2;
  386. }
  387.  
  388. //Calculations
  389. rVal = S1.calcRval(startPtr, dof);
  390. dof = dof - 2;
  391. tVal = (rVal * sqrt(dof)) / (sqrt(1 - pow(rVal, 2)));
  392. W = (tVal - xLow) / N;
  393. p = S1.calcSimpson(xLow, tVal, N, W, dof);
  394.  
  395. //Output
  396. cout << "\nHere is your output!" << endl;
  397. cout << "********************";
  398. cout << "\nrval = " << rVal;
  399. cout << "\ntval = " << tVal;
  400. cout << "\np = " << p;
  401. twoTail = 2 * (1 - p);
  402. cout << "\nTwo tailed value = " << scientific << twoTail << endl;
  403.  
  404. return 0;
  405. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC