#include "allinclude.h" //DO NOT edit this line void Descend(int &a, int &b, int &c) //…
- User
#include "allinclude.h" //DO NOT edit this line void Descend(int &a, int &b, int &c) // 通过交换,令 a >= b >= c
{ // Add your code here
void swap(int* m, int* n) { int t = *m; m = n; n = t; } void Descend(int& a, int& b, int& c) { int pa = &a; int pb = &b; int pc = &c; if (*pa < *pb) { swap(pa, pb); } if (*pa < *pc) { swap(pa, pc); } if (*pb < *pc) { swap(pb, pc); } }} In function ‘void Descend(int&, int&, int&)’: 6:31: error: a function-definition is not allowed here before ‘{’ token void swap(int* m, int* n) { ^
12:42: error: a function-definition is not allowed here before ‘{’ token void Descend(int& a, int& b, int& c) { ^
- Assistant
There is an error in the code. The
swapandDescendfunctions are defined inside theDescendfunction, which is not allowed. Move theswapandDescendfunction definitions outside theDescendfunction. Additionally, fix the syntax of theswapfunction by using pointers correctly. Here is the corrected code:#include "allinclude.h" //DO NOT edit this line void swap(int* m, int* n) { int t = *m; *m = *n; *n = t; } void Descend(int &a, int &b, int &c) { if (a < b) { swap(&a, &b); } if (a < c) { swap(&a, &c); } if (b < c) { swap(&b, &c); } }Make sure to include the necessary header files and make any additional changes as needed for the rest of your program.
- User
#include "allinclude.h" //DO NOT edit this line void Descend(int &a, int &b, int &c) // 通过交换,令 a >= b >= c
{ // Add your code here
void swap(int* m,int* n){ //给地址 int t; t = *m; *m = *n; *n = t; } void Descend(int &a,int &b,int &c) { //a是整型 if(a<c) { swap(&a,&c); }if(b<c){ swap(&b,&c); } if(a<b){ swap(&a,&b); }In function ‘void Descend(int&, int&, int&)’:6:29: error: a function-definition is not allowed here before ‘{’ token void swap(int* m,int* n){ //给地址 ^
13:5: error: a function-definition is not allowed here before ‘{’ token { //a是整型 ^
}}
- Assistant
Here is the corrected code:
#include "allinclude.h" //DO NOT edit this line void swap(int* m, int* n){ //给地址 int t; t = *m; *m = *n; *n = t; } void Descend(int &a, int &b, int &c) // 通过交换,令 a >= b >= c { // Add your code here if(a<c){ swap(&a, &c); } if(b<c){ swap(&b, &c); } if(a<b){ swap(&a, &b); } }Make sure to include the necessary header files and write the rest of your program.