A collection of grayscale images is stored in memory as one continuous one-dimensional array, where each pixel occupies one array element. Each grayscale image has a resolution of 32 × 24 pixels (width × height), and the images are organized into batches of 8 images. Pixels within an image are stored from left to right and top to bottom (row-major order). Images within a batch are stored one after another, and batches are also stored one after another. Write a C++ program that reads a memory position (array index) and determines the corresponding:
batch number
image number within the batch
row
column
All numbers start from 0. Each image contains 32 x 24 = 768 pixels, and each batch therefore contains 8 x 768 = 6144 pixels. For example:
Enter the memory position: 7000
The position corresponds to
Batch: 1
Image: 1
Row: 2
Column: 24
Explanation
The calculation is as follows: Using integer division 7000 / 6144 = 1, so the pixel is located in batch 1. Its position within that batch is 7000 mod 6144 = 856. The image number within the batch is 856 / 768 = 1, and the position within that image is 856 mod 768 = 88. Finally, 88 / 32 = 2 and 88 mod 32 = 24, so the result is the same as shown in the example above.
The input is guaranteed to be a valid integer in the range 0 to 99,999, so you do not need to perform any input validation.
The outputs of your program should be exactly the same as those in the input-output samples.
Every character in the output (spaces, newlines, punctuation, and letters) should be identical to the provided samples.
Before submitting assignments, ensure that your program successfully handles the input-output examples provided above.
Output formatting:
Check the code template first, so you’ll know the expected input and output before you start coding.
It is recommended to modify the code template to complete the assignment.
Many students made mistakes because they started coding after looking only at the example, without carefully checking the required output format.
The following line is from the code template.
cout << "Batch: " << variable1 << endl;
The following is INCORRECT, because it removes a space from the string enclosed by double quotes.
cout << "Batch:" << variable1 << endl;
The following is INCORRECT, because it outputs extra line breaks caused by system("pause").
cout << "Batch: " << variable1 << endl; system("pause");
The following is INCORRECT, because it outputs extra endl.
cout << "Batch: " << variable1 << endl; cout << endl;
Suggestions:
Rename variable0 through variable4 with meaningful names.
Remove comments.
Typically, if your variable names are descriptive, there's no need to include comments.
Ensure that you use the integer type (not floating-point) throughout your code for this assignment.