返回带有 void 函数的排序数组以查找中位数

Return sorted array with void function to find median

本文关键字:数组 查找 中位数 排序 void 函数 返回      更新时间:2023-10-16

所以我正在编写这个程序,该程序将用户输入读取到数组中,然后找到平均值和中位数。我有三个不同的文件,我将在 unix 中一起编译:
1) stats.h,其中包含我为平均值、中位数和排序函数
提供的原型2)统计.cpp,我应该在哪里实现这三个功能,以及
3)主.cpp,与我的主要功能。

到目前为止,我的 main() 运行正常。我有两个主要问题:

1)我想确保我将array[]及其大小正确传递给三个函数中的每一个(我对传递数组有点生疏)。
2)我得到了三个函数(包括void Sort函数)的原型,并被告知在Median 函数中调用Sort函数。

如果我无法在排序函数中返回排序后的数组(如果有意义),我真的不明白如何更新 Median 函数中的旧数组。

所以这是我的三个文件:1) 统计

//not sure if I need to include anything at top
float Mean (const int* array, size_t size);
float Median (int* array, size_t size);
void Sort (int* array, size_t size);

2)统计数据.cpp

#include <stats.h>  //only include header file with prototypes?
//function to find the mean that should be called by
main()                                         
float Mean (int NewArray[], size_t size)
{                                                                    
   float sum, mean = 0.0;          
   for (int i = 0; i < size; i++)
   {                           
      sum += NewArray[i];                                
   }                                         
   mean = ((float)sum)/size;
   return mean;                                   
}
//function to find median that should be called by main()
float Median (int NewArray[], size_t size);
{//not sure how to call sort (void) function and return new sorted array to median function
    if(size % 2 == 0)                                            
    {                      
      median = (NewArray[size/2] + NewArray[size/2-1])/2.0f;
    }                                               
    else            
    {                                                  
      median = NewArray[size/2];                        
    }                                                                      
 return median;  //return median to main()                 
} 
// void function to sort array in ascending order and called by median()
Sort(int NewArray[], size_t size)                    
{                                               
   for (i = 0; i < size; i++)
   {                                                    
      for(int j = 0; j < size-1; j++)                          
      {                           
         if(NewArray[j] > NewArray[j+1])             
         {
             int temp = NewArray[j+1];                  
             NewArray[j+1] = NewArray[j];            
             NewArray[j] = temp;               
          }
       }
    }
}

3) 和主()

#include <iostream>                                
#include <stats.h>                           
#include <stats.cpp>                  
int main()                                      
{                                              
    int input, sum, NumberOfEntries = 0;     
    const int SIZE = 100                      
    int array[SIZE];                               
    std::cout << "Please enter integers ('x' to quit): ";
    for (int i = 0; i < SIZE; i++)    
    {                            
       if (std::cin >> array[i])               
       {                                             
       }
       else                                        
       {                                      
          NumberOfEntries = i;                  
          break;                                    
       }                                                    
    }                                             
    std::cout << "Data as entered: ";                
    for (int i = 0; i < NumberOfEntries; i++)     
       std::cout << array[i] << " ";    
    std::cout << "Mean: " << float Mean (array[SIZE], NumberOfEntries);
    std::cout << "Median: " << float Median (array[SIZE], NumberOfEntries);
    std::cout << "Data after sort: "; //would print out sorted array 
    return 0;                                    
}

你应该为数组创建一个同义词(这使得传递更容易):

typedef int DataArray[/* Some size constant*/];

(请参阅有关数组类型定义的 StackOverflow 问题)

您可以将函数声明为:

double Calculate_Mean(const Data_Array array, unsigned int size);
double Calculate_Median(const Data_Array array, unsigned int size);

顺便说一句,除非你需要节省空间,否则使用 double ,而不是浮动。 C++中大多数使用浮点的库都使用 double 类型。 它更准确。

此外,使用unsigned int作为数量。 我还没有看到长度为 -5 的数组。 unsigned 关键字将帮助编译器检查分配给索引和数量的负值(嗯,你能买 -5 个苹果吗?