What is the following C++ code; int myfunction( int arr[], int a, int b){ int t;
ID: 3819920 • Letter: W
Question
What is the following C++ code;
int myfunction( int arr[], int a, int b){
int t;
if ((a>b)|| (b<0)||(a<0)) {return -1;}
while (a
t=arr[a]
arr[a] = arr[b];
arr[b] = t;
a++;b--; }
return 0; }
int main(){
int x[10]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
myfunction(x, 5, 7);
for (int i=0; i<0; i++){
cout << x[i] << “ ”;
}
return 0;
}
a. 1 2 3 4 5 6 7 8 9 10
b. 10 9 8 7 6 5 4 3 2 1
c. 1 2 3 4 5 8 7 6 9 10
d. 1 3 5 7 9 2 4 6 8 10
Note : Please provide a solution
Explanation / Answer
i have rectified the mistakes of your code as shown below:
#include <iostream>
using namespace std;
int myfunction( int arr[], int a, int b){
int t;
if ((a>b)|| (b<0)||(a<0))
return -1;
while (a){
t=arr[a];
arr[a] = arr[b];
arr[b] = t;
a++;b--;
}
return 0;
}
int main() {
int x[10]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
myfunction(x, 5, 7);
for (int i=0; i<0; i++){
cout << x[i] <<" ";
}
return 0;
}
op: 1 2 3 4 56 7 8 9 10
starting from the main function, an array x of 10 values are considered. then myfunction is called which passes value of arr= x, a= 5, b= 7. in myfunction it checks the if condition. since (5>7)|| (7<0)||(5<0) is false it goes back to the main function and enters for loop and hence prints the input values.
similarly for other input values,displays the input values as an output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.