为什么double/int, 24/5= -4.8 int/double, 24/5= 4.8结果不同
时间: 2023-12-16 08:05:34 浏览: 34
`double/int`的结果是浮点数,而`int/double`的结果也是浮点数。这是因为在浮点数与整数进行运算时,整数会被隐式转换成浮点数,然后进行浮点数除法。因此,无论是`double/int`还是`int/double`,结果都是浮点数,而不是整数。
在`double/int`中,24被除以5得到的是4.8,但由于5是整数,因此它会被隐式转换成5.0,整个表达式的结果是4.8/5.0,即0.96。由于0.96是浮点数,因此最终结果也是浮点数。如果将其取相反数,结果为-0.96。
在`int/double`中,24是整数,因此它会被隐式转换成24.0,整个表达式的结果是24.0/5,即4.8。由于4.8是浮点数,因此最终结果也是浮点数。
相关问题
解释double/int, 24/5= -4.8
`double/int`是除法运算,其中一个操作数是浮点数,另一个操作数是整数。在这种情况下,整数会被隐式转换成浮点数,然后进行浮点数除法。因此,`24/5`的结果是4.8(浮点数)。
但是,题目中的运算结果是`-4.8`,这是不正确的。如果要得到这个结果,可能是因为在计算时将24和5的位置颠倒了,即进行了`5/24`的除法,然后再将结果取相反数得到`-4.8`。但是,正确的24除以5的结果应该是4.8。
使用void bubble(int a[], int size=10); void bubble(double a[], int size=10); void bubble(char a[], int size=10);写一个冒泡排序
以下是使用函数重载实现的冒泡排序示例代码:
```c++
#include <iostream>
using namespace std;
// 冒泡排序函数重载
void bubble(int a[], int size = 10);
void bubble(double a[], int size = 10);
void bubble(char a[], int size = 10);
int main() {
int a[] = {4, 2, 8, 0, 5, 7, 1, 3, 9, 6};
double b[] = {2.5, 3.6, 1.2, 4.8, 0.5, 7.2, 6.4, 8.1, 9.0, 5.3};
char c[] = {'d', 'f', 'c', 'h', 'b', 'a', 'g', 'e', 'i', 'j'};
bubble(a);
bubble(b);
bubble(c);
return 0;
}
// 整型数组冒泡排序
void bubble(int a[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << "排序后的整型数组为:";
for (int i = 0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl;
}
// 浮点型数组冒泡排序
void bubble(double a[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (a[j] > a[j + 1]) {
double temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << "排序后的浮点型数组为:";
for (int i = 0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl;
}
// 字符型数组冒泡排序
void bubble(char a[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (a[j] > a[j + 1]) {
char temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << "排序后的字符型数组为:";
for (int i = 0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl;
}
```
该示例代码中,我们定义了三个函数重载,分别用于整型数组、浮点型数组、字符型数组的冒泡排序。在 `main` 函数中,我们分别声明了三个不同类型的数组,并调用了对应的 `bubble` 函数进行排序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)