Holey_yan

20150326_KUAS_VisualC++_Exam04

Mar 26th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. /***************************************/
  2. /** 20150326 Exam 04                  **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2015/03/03 pm.12:00      **/
  6. /***************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. template <typename T> T GetSquareAverage(T array[], int count){
  12.     T sum = 0;
  13.     for (int i = 0; i < count; i++){
  14.         sum += array[i] * array[i];
  15.     }
  16.     return sum / count;
  17. }
  18.  
  19. int main(){
  20.     for (int i = 0; ; i++){
  21.         int count = 0;
  22.         scanf("%d", &count);
  23.         if (count == 0){
  24.             break;
  25.         }
  26.         else{
  27.             //First time use Integer Type
  28.             if (i == 0){
  29.                 int *inArray = (int *)malloc(count * sizeof(int *));
  30.                 for (int j = 0; j < count; j++)
  31.                     scanf("%d", &inArray[j]);
  32.  
  33.                 printf("Answer: %d\n", GetSquareAverage(inArray, count));
  34.                 delete [] inArray;
  35.             }
  36.             //After first time, all is Float Type
  37.             else if(i == 1){
  38.                 float *inArray = (float *)malloc(count * sizeof(float *));
  39.                 for (int j = 0; j < count; j++)
  40.                     scanf("%f", &inArray[j]);
  41.  
  42.                 printf("Answer: %f\n", GetSquareAverage(inArray, count));
  43.                 delete [] inArray;
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment