Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C and C0, it's a bit difficult to return more than one value * from a function. For instance, say we needed to know,

In C and C0, it's a bit difficult to return more than one value
* from a function. For instance, say we needed to know, given an
* array of pixels, how many pixels had average intensity 0, how many
* pixels had average intensity 1, and so on, all the way to how many
* many pixels had average intensity 255.
*
* We could write 256 different functions (!) to return these 256
* values, but what we'd like to do instead is write one loop that
* calculates all 256 values. Then, those 256 values are returned
* to the user by modifying the contents of an array that was passed
* to the function.
*
* The function histogram is supposed to count the number of
* pixels whose average intensity is i (a number between 0 and 255)
* and store the results in results[i], where results is an array
* of size 256. In other words, the pixel array A[] is an input and
* should not change, but the integer array results[] is an output,
* and we have to change it.
*
* The histogram function has a couple of bugs and therefore fails
* the test cases below. Fix it, but do not change the interface. (You
* shouldn't need to change the test cases, either.) */
void histogram(pixel_t[] A, int length, int[] results)
//@requires \length(A) == length;
//@requires \length(results) == 256;
{
results = alloc_array(int, 256);
for (int i = 0; i < length; i++)
//@loop_invariant 0 <= i;
{
int average = (get_red(A[i]) + get_green(A[i]) + get_blue(A[i])) / 3;
results[average] = results[average] + 1;
}
}
These are the test cases.
/* This is some code for testing the histogram() function. You can
* use it to help you understand what histogram is supposed to do,
* and you can use it to test your implementation. If you want, you
* can extend it with your own tests, but we won't run this test code
* in the autograder. */
pixel_t[] copy_pixels(pixel_t[] A, int length)
//@requires length == \length(A);
//@ensures length == \length( esult);
//@ensures esult != A;
{
pixel_t[] B = alloc_array(pixel_t, length);
for (int i = 0; i < length; i++)
//@loop_invariant 0 <= i;
{
B[i] = A[i];
}
return B;
}
bool pixels_equal(pixel_t[] A, pixel_t[] B, int length)
//@requires length == \length(A);
//@requires length == \length(B);
{
for (int i = 0; i < length; i++)
//@loop_invariant 0 <= i;
{
if (get_alpha(A[i]) != get_alpha(B[i])) return false;
if (get_red(A[i]) != get_red(B[i])) return false;
if (get_green(A[i]) != get_green(B[i])) return false;
if (get_blue(A[i]) != get_blue(B[i])) return false;
}
return true;
}
void test_histogram() {
pixel_t[] A;
pixel_t[] C;
int[] results = alloc_array(int, 256);
// Initialize the array of pixels, make a copy.
A = alloc_array(pixel_t, 9);
A[0] = make_pixel(0xFF, 0x01, 0x01, 0x01);
A[1] = make_pixel(0x01, 0x00, 0xFF, 0xEE);
A[2] = make_pixel(0xFF, 0xFF, 0xFF, 0xFF);
A[3] = make_pixel(0xEE, 0xFF, 0xDD, 0x00);
A[4] = make_pixel(0xDD, 0xCC, 0x00, 0xFF);
A[5] = make_pixel(0xCC, 0x00, 0xDD, 0xEE);
A[6] = make_pixel(0x00, 0xEE, 0xFF, 0xDD);
A[7] = make_pixel(0x00, 0x00, 0x00, 0x00);
A[8] = make_pixel(0x00, 0xDD, 0xCC, 0xBB);
C = copy_pixels(A, 9);
// Compute the histogram and check the results
histogram(A, 9, results);
assert(pixels_equal(A, C, 9));
for (int i = 0; i < 255; i++) {
if (i == 0x00) assert(results[i] == 10);
else if (i == 0x01) assert(results[i] == 4);
else if (i == 0xBB) assert(results[i] == 1);
else if (i == 0xCC) assert(results[i] == 3);
else if (i == 0xDD) assert(results[i] == 5);
else if (i == 0xEE) assert(results[i] == 4);
else if (i == 0xFF) assert(results[i] == 9);
else assert(results[i] == 0);
}
// Modify both arrays of pixels
A[8] = make_pixel(0x00, 0x00, 0x00, 0xFF);
C[8] = make_pixel(0x00, 0x00, 0x00, 0xFF);
// Compute the histogram and check the results
count_zeroes(A, 9, results);
assert(pixels_equal(A, C, 9));
for (int i = 0; i < 255; i++) {
if (i == 0x00) assert(results[i] == 12);
else if (i == 0x01) assert(results[i] == 4);
else if (i == 0xCC) assert(results[i] == 2);
else if (i == 0xDD) assert(results[i] == 4);
else if (i == 0xEE) assert(results[i] == 4);
else if (i == 0xFF) assert(results[i] == 11);
else assert(results[i] == 0);
}
}

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

Step: 3

blur-text-image

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

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

More Books

Students also viewed these Databases questions

Question

8. Explain the contact hypothesis.

Answered: 1 week ago

Question

2. Define the grand narrative.

Answered: 1 week ago