Question: #include #include #include #include / / Include for std::numeric _ limits / / Function for the random walk int randWalk ( int oldValue, int updateSize

#include
#include
#include
#include // Include for std::numeric_limits
// Function for the random walk
int randWalk(int oldValue, int updateSize){
int randomChange =(std::rand()%(updateSize *2+1))- updateSize;
int newValue = oldValue + randomChange;
if (newValue >255){
newValue =255;
}
else if (newValue <0){
newValue =0;
}
return newValue;
}
int main(){
std::srand(std::time(nullptr)); // Seed the random number generator
int initialValue, iterations, updateSize;
// Get user input for initial value with validation
while (true){
std::cout << "Please enter an initial integer value in the range [0,255]: ";
std::cin >> initialValue;
if (std::cin.fail()|| initialValue <0|| initialValue >255){
std::cin.clear(); // Clear error flags
std::cin.ignore(std::numeric_limits::max(),'
'); // Discard bad input
std::cout << "Value must be between 0 and 255. Please enter an initial value in the range [0,255]: "<< std::endl;
}
else {
break; // Valid input received, exit loop
}
}
// Get user input for number of iterations
std::cout << "Please enter the desired number of iterations: ";
std::cin >> iterations;
// Get user input for the update size
std::cout << "Please enter the size of each possible update for each iteration: ";
std::cin >> updateSize;
// Perform the random walk with updated output formatting
for (int i =0; i < iterations; ++i){
initialValue = randWalk(initialValue, updateSize);
std::cout << "Value at iteration # "<<(i +1)<<" is: "<< initialValue << std::endl;
}
return 0;
}
HW3B - Compilation Test (0/0)
COMPILATION SUCCESSFUL!!
HW3B - Testcase -1(0/0)
Initial Vaue: 255
No. of Iterations: 10
Update Size: \pm 10
Your Output
Please enter an initial integer value in the range [0,255]: Please enter the desired number of iterations: Please enter the size of each possible update for each iteration: Value at iteration # 1 is: 248
Value at iteration # 2 is: 238
Value at iteration # 3 is: 248
Value at iteration # 4 is: 244
Value at iteration # 5 is: 250
Value at iteration # 6 is: 249
Value at iteration # 7 is: 241
Value at iteration # 8 is: 233
Value at iteration # 9 is: 239
Value at iteration # 10 is: 234
Program Exit Succesfully!!
HW3B - Testcase -2(0/0)
Initial Vaue: 0
No. of Iterations: 20
Update Size: \pm 2
Your Output
Please enter an initial integer value in the range [0,255]: Please enter the desired number of iterations: Please enter the size of each possible update for each iteration: Value at iteration # 1 is: 1
Value at iteration # 2 is: 3
Value at iteration # 3 is: 2
Value at iteration # 4 is: 1
Value at iteration # 5 is: 2
Value at iteration # 6 is: 3
Value at iteration # 7 is: 3
Value at iteration # 8 is: 1
Value at iteration # 9 is: 0
Value at iteration # 10 is: 2
Value at iteration # 11 is: 4
Value at iteration # 12 is: 4
Value at iteration # 13 is: 4
Value at iteration # 14 is: 2
Value at iteration # 15 is: 4
Value at iteration # 16 is: 6
Value at iteration # 17 is: 4
Value at iteration # 18 is: 6
Value at iteration # 19 is: 5
Value at iteration # 20 is: 7
Program Exit Succesfully!!
HW3B - Invalid Input (0/2)
Input Sequence: 260,256,-9
- Please enter an initial integer value in the range [0,255]: Value must be between 0 and 255. Please enter an initial value in the range [0,255]:
+ Please enter an initial integer value in the range [0,255]: Value must be between 0 and 255. Please enter an initial value in the range [0,255]: Value must be between 0 and 255. Please enter an initial value in the range [0,255]: Value must be between 0 and 255. Please enter an initial value in the range [0,255]: Please enter the desired number of iterations: Please enter the size of each possible update for each iteration:
FAILED!!
Test Failed: False is not true :

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!