Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c++中通过函数求中值_C++_Arrays - Fatal编程技术网

在c++中通过函数求中值

在c++中通过函数求中值,c++,arrays,C++,Arrays,嗨,我做了这个函数来找到中间值,但是我得到的不是3.5,而是3。我不明白为什么?谁能给我指路吗。谢谢。:) int中值(int数组[],int n) { 浮点数med=0; int a=0; int b=0; float-newMed=0; 如果(n%2==0) a=n/2; b=(n/2)-1; med=(数组[a]+数组[b]); newMed=(med/2); //否则 //med=(n+1)/2; 返回newMed; } int main() { int数组[6]={1,2,3,4,6,

嗨,我做了这个函数来找到中间值,但是我得到的不是3.5,而是3。我不明白为什么?谁能给我指路吗。谢谢。:)

int中值(int数组[],int n)
{
浮点数med=0;
int a=0;
int b=0;
float-newMed=0;
如果(n%2==0)
a=n/2;
b=(n/2)-1;
med=(数组[a]+数组[b]);
newMed=(med/2);
//否则
//med=(n+1)/2;
返回newMed;
}
int main()
{
int数组[6]={1,2,3,4,6,7};
cout将
int中值(int数组[],int n)
(作为返回
int
的函数不能返回
3.5
)更改为

当您的返回类型为
int
时,计算为
3.5
的返回值
newMed
被简单地转换为
int
,即
3
。此代码大致相当于:

cout<<"the median is: "<< (int)median(array,6)<<"\n";

cout你可以用这样的东西

#include <iostream>
#include <algorithm>
using namespace std;
#define  SIZE 7
float getMedian( int *arry,int size) {
    sort(arry, arry + size);
    if(size%2){
      return arry[(size/2)];
    }
    else {
        return ((arry[(size/2) -1]+arry[size/2])/2);
    }
}
int main()
{
    int intArray[7] = {1,2,3,4,5,6,7};

    //Now we call the sort function

   std::cout << getMedian (intArray,SIZE) << std::endl;

return 0;
}
#包括
#包括
使用名称空间std;
#定义尺寸7
float getMedian(int*arry,int size){
排序(arry,arry+大小);
如果(大小%2){
返回arry[(大小/2)];
}
否则{
返回((arry[(size/2)-1]+arry[size/2])/2);
}
}
int main()
{
int intArray[7]={1,2,3,4,5,6,7};
//现在我们调用sort函数

std::有没有办法在不改变函数类型int的情况下得到3.5?我的意思是通过在函数内部做一些事情来得到浮点数?返回和并在调用者中除以2。或者传递一个浮点指针/引用来收集结果。
int
不能返回
3.5
而不是对整个数组进行排序这是可能的在这种情况下,对于7个元素,它不会有任何区别,但是对于较大的数组,它会有区别,因为我们在查找中间元素时所做的工作较少。
cout<<"the median is: "<< (int)median(array,6)<<"\n";
#include <iostream>
#include <algorithm>
using namespace std;
#define  SIZE 7
float getMedian( int *arry,int size) {
    sort(arry, arry + size);
    if(size%2){
      return arry[(size/2)];
    }
    else {
        return ((arry[(size/2) -1]+arry[size/2])/2);
    }
}
int main()
{
    int intArray[7] = {1,2,3,4,5,6,7};

    //Now we call the sort function

   std::cout << getMedian (intArray,SIZE) << std::endl;

return 0;
}