Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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: 255
Value at iteration # 2 is: 255
Value at iteration # 3 is: 245
Value at iteration # 4 is: 246
Value at iteration # 5 is: 245
Value at iteration # 6 is: 254
Value at iteration # 7 is: 252
Value at iteration # 8 is: 249
Value at iteration # 9 is: 243
Value at iteration # 10 is: 233
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: 0
Value at iteration # 2 is: 0
Value at iteration # 3 is: 0
Value at iteration # 4 is: 0
Value at iteration # 5 is: 0
Value at iteration # 6 is: 0
Value at iteration # 7 is: 0
Value at iteration # 8 is: 0
Value at iteration # 9 is: 2
Value at iteration # 10 is: 3
Value at iteration # 11 is: 2
Value at iteration # 12 is: 1
Value at iteration # 13 is: 3
Value at iteration # 14 is: 2
Value at iteration # 15 is: 1
Value at iteration # 16 is: 2
Value at iteration # 17 is: 0
Value at iteration # 18 is: 1
Value at iteration # 19 is: 0
Value at iteration # 20 is: 0
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 :
HW3B - I/O Formatting (2/2)
Initial Vaue: 200
No. of Iterations: 10
Update Size: 0
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: 200
Value at iteration # 2 is: 200
Value at iteration # 3 is: 200
Value at iteration # 4 is: 200
Value at iteration # 5 is: 200
Value at iteration # 6 is: 200
Value at iteration # 7 is: 200
Value at iteration # 8 is: 200
Value at iteration # 9 is: 200
Value at iteration # 10 is: 200
PASSED!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

ms excel chart

Answered: 1 week ago