This following program is supposed print 80 40 5, but it does
not. Find all the bugs and rewrite the entire corrected
version of the program. Note that cout<<”80 40 5”; is not a
correct fix, you may not use a for-loop or the variable
arr. Make as small changes to the original code as possible, some
reordering may be necessary, but replacing entire
lines of code is not acceptable.
int main(){
intarr[3] = { 5, 10, 15 };
int* ptr = arr;
ptr = 5; // set arr[0] to 5
*ptr + 1 = 40; // set arr[1] to 40
ptr += 2;
ptr[0] = 80; // set arr[2] to 80
while (ptr >= arr) {
ptr--;
cout << ' ' << ptr; // print values
}
cout << endl;
}
Solution:
//code after some corrections
#include <iostream>
using namespace std;
int main(){
int arr[3] = { 5, 10, 15 };//array initialization
int* ptr = arr;
*ptr += 75; // add arr[0] to 75
*(ptr+1) += 30;//add arr[1] to 30
arr[2] -= 10;//substract arr[2] to 10
while (ptr < arr+3) {//while loop
cout<<*(ptr)<<" ";
ptr++;
}
cout << endl;
return 0;
}
Thank you, Have a great day:-)
Get Answers For Free
Most questions answered within 1 hours.