Guest User

Untitled

a guest
Oct 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. // Test program for the CSE 6329 Spring 2017 Correlate() function.
  2. // This test program performs the following:
  3. // 1. Prompts the user to enter the size (number of elements) of the data array/s to be processed.
  4. // 2. Prompts the user to enter the data items for the first array.
  5. // 3. Populates the "size" and "ArrayOne[]" parameters with the user-entered data.
  6. // 4. Prompts the user to enter the data items for the second array.
  7. // 3. Populates the "size" and "ArrayTwo[]" parameters with the user-entered data.
  8. // 4. Displays the user-entered inputs: "size" and "ArrayOne []", "ArrayTwo []".
  9. // 5. Displays Correlate() outputs: Spearman Coefficient, Correlation Flag inputValidityFlag (Stats() return value).
  10.  
  11. #include<stdio.h>
  12. #include<math.h>
  13.  
  14. // Declare Correlate () function
  15. _Bool Correlate (int size, float ArrayOne[], float ArrayTwo[], float *spearmanCoefficient, float *correlationFlag);
  16.  
  17. int main( )
  18. {
  19. // Initialize outputs
  20. float spearmanCoefficient = 0.0;
  21. float correlationFlag = 0.0;
  22.  
  23. _Bool inputValidityFlag = 0;
  24. float ArrayOne[100], ArrayTwo[100];
  25.  
  26. int i; /* Array index */
  27.  
  28. int size = 0;
  29.  
  30.  
  31. //Get the size of data set
  32.  
  33. printf("Enter size of data set\n");
  34. scanf ("%d", &size);
  35. printf("\n# elements = %d", size);
  36.  
  37. //Get input Array One
  38. printf("\nInput for Array One");
  39. for(i=0; i <= size-1; i++)
  40. {
  41. printf("\nEnter data item: \n");
  42. scanf("%f", &ArrayOne[i]);
  43. }
  44.  
  45. //Get input Array Two
  46. printf("Input for Array Two");
  47. for(i=0; i <= size-1; i++)
  48. {
  49. printf("\nEnter data item: \n");
  50. scanf("%f", &ArrayTwo[i]);
  51. }
  52.  
  53. inputValidityFlag = Correlate (size, ArrayOne, ArrayTwo, &spearmanCoefficient, &correlationFlag);
  54. printf("\nValidityFlag = %d\n", inputValidityFlag);
  55.  
  56. if (inputValidityFlag)
  57. {
  58. for (i = 0; i <= size-1; i++)
  59. {
  60. printf("\nArrayOne[%d] = %10.3f ArrayTwo[%d] = %10.3f", i, ArrayOne[i], i, ArrayTwo[i]);
  61. }
  62. printf("\n Spearman Coefficient = %10.2f", spearmanCoefficient);
  63. printf("\n Correlation Flag = %10.2f", correlationFlag);
  64.  
  65. }
  66. else
  67. {
  68. printf("\nInput is invalid; outputs are not displayed\n");
  69. }
  70.  
  71. return 1;
  72. }
  73.  
  74.  
  75. _Bool Correlate (int size, float arrayOne[], float arrayTwo[], float * spearmanCoefficient, float * correlationFlag)
  76. {
  77. float meanone=0.0,meantwo=0.0,xxsqr=0.0,yysqr=0.0,xxyy=0.0;
  78. float xx[100], yy[100];
  79. int i;
  80. for(i=0;i<size;i++)
  81. {
  82. meanone += arrayOne[i];
  83. }
  84. meanone = meanone/size;
  85.  
  86. for(i=0;i<size;i++)
  87. {
  88. meantwo += arrayTwo[i];
  89. }
  90. meantwo = meantwo/size;
  91.  
  92. for(i=0;i<size;i++)
  93. {
  94. xx[i] = arrayOne[i]-meanone;
  95. yy[i] = arrayTwo[i]-meantwo;
  96. xxsqr += (xx[i] * xx[i]);
  97. yysqr += (yy[i] * yy[i]);
  98. xxyy += (xx[i]*yy[i]);
  99.  
  100. }
  101. *spearmanCoefficient = xxyy / sqrt(xxsqr*yysqr);
  102.  
  103. if(*spearmanCoefficient>=0.9 || *spearmanCoefficient<=1.0 )
  104. *correlationFlag = 1.0;
  105. else if (*spearmanCoefficient>=-0.9 || *spearmanCoefficient<=-1.0 )
  106. *correlationFlag = -1.0;
  107. else
  108. *correlationFlag = 0;
  109. for (i = 0; i <= size-1; i++)
  110. {
  111. printf("\nxx[%d] = %10.3f yy[%d] = %10.3f", i, xx[i], i, yy[i]);
  112. }
  113.  
  114. return size>0? 1: 0;
  115. }
Add Comment
Please, Sign In to add comment