Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Images & Arrays L et's get started with this homework assignments-three functions that process arrays. Upload the starter code to your workspace, and add your

Images & Arrays

L et's get started with this homework assignments-three functions that process arrays. Upload the starter code to your workspace, and add your name and

student ID as normal. Here are the functions:

1. alternatingSum() uses an accumulation algorithm to compute the alternating sum of all elements in an array of int. It is passed an array and its length and returns the sum.

The minMax() function uses extreme values along with pointers to return a pair of pointers to the largest and smallest element in an array.

The translate() function uses pointers and arrays, to rotate the pixels in an array in both the horizontal and vertical dimension.

1: Alternating Sum

An alternating sum is one where you add the first element, subtract the second, add the third, and so on. It is a type of cumulative algorithm, which you met in this chapter.

Here's an example:

Check your assignment using make test. If you get stuck, ask for help on Piazza.

Homework

IfalternatingSum()ispassedanarray: 1 4 9 16 9 7 4 9 11itreturns:1-4+9-16+9-7+4-9+11 (or-2)

PROBLEM 2: EXTREME VALUES PAGE 14-2

Problem 2: Extreme Values

The second function in this section is minMax(). Here is the specification:

The minMax function takes a pointer to an element in an array ofdouble and the number of subsequent elements to process. It returns aMinMax variable containing a pointer to the maximum value in the sequence and a pointer to the minimum value in the sequence. You are not to return the largest and smallest values themselves. If size is 0, the special pointer value nullptr (or 0) you be used for both min and max.

Read about the extreme values algorithm in this chapter. As mentioned, you don't want to return the largest (or smallest) value; you want to return the addresses of the largest and smallest value.

Here's a modified version of the algorithm:

Pointer ptr points to first element Structure result {min=ptr, max=ptr} Create pointers and smallest Create pointer falls past the end of the sequence While the ptr < atEnd Do

 Increment ptr If *largest < *current Then 
 Store current in largest If *smallest > *current Then 

Store current in smallest Return smallest and largest (inside structure variable)

I've used indenting to reflect the general structure of the function.

Writing the Code

I have already stubbed out the function. Below, I have notes on the code for each of those sections.

Assign ptr to result.min and result.max. Create largest and smallest pointers. Initialize with ptr. Create the atEnd pointer. Add size to ptr. Traversing the array. In a while loop, if ptr is < attend Do

largest

atEnd

HOMEWORK 14

IMAGES & ARRAYS

a.

b.

Move the pointer to the next element using the increment operator. Since we have already handled the first element, use prefix instead of postfix. (See p++ idiom in text).

Comparing the values. Inside the loop body, dereference result.minand result.max and compare those values to the value that ptr is pointing at. Update the structure members depending on the results.

I'm sure you can do the final step on your own. If you run the tests now, you'll see you have 90%. The test that fails is when size is 0. For that, you need to simply add a special case before the loop begins.

3. translate()

For our last exercise, we're going to return to images and implement a translate()function. In this function you will modify the pixels by shifting them by dx in the xdimension, and dy in the y dimension. Both dx and dy will be arguments, and either may be negative, or, larger than the width or height of the image.

Here's an example using letters in place of pixels.

Here the 6x4 image is shifted in the x dimension by 2 and the y dimension by -1. If you follow the letter a, you can see it has moved to column 2. However, since it is in row 0, when it is shifted by -1, it wraps around to the bottom row.

Here's a picture with an actual image, so you can see the effect.

3. TRANSLATE()

PAGE 14-4

Step 1: Getting Started

The first thing you want to be able to do is to address the image in rows and columns. To make sure we can do that, we'll just draw a black "cross" across the image:

Add your Pixel structure and cast the img pointer to a Pixel* pixels. Write a for loop with row for the index (0 to < width) Intheloop,theaddressofthefirstelementispixels+(row* width) Now you can use the subscript to access each pixel in the row

On the middle row, set each pixel black. Here's the code for this starter portion:

Double-click the actual picture, and you'll see this, so we know that our address calculations are correct.

Step 2: Translate Horizontal

The algorithm to do this efficiently is fairly complex for this point in the course. Instead, we'll adopt the following brute-force algorithm. Start with the code you have above, and let's shift dx elements, assuming that dx is positive and less than width. Sections 1- 3 will remain the same. Replace the painting code in the middle with this loop.

for dx times Save the last element in the row in temp for each element in the row

Shift it to the right place temp in the first position in the row

This is fairly easy. There are only a few tricky spots. Our loop has to move from right-to left to shift items to the right. If you go the other way, every pixel will be the same. If you make test you'll see that the two positive X tests pass.

HOMEWORK 14

IMAGES & ARRAYS

Step 3: Translate Vertical

This is a little more complex. The code appears after shifting left or right. Calculate the address of the last row; this won't change so do it before the

loop. The last row address is pixels + width * (height 1)After that, write a loop that repeats dy times. Inside of that loop, add another that visits every column like this:

In that inner loop, save the pixel at the current column in the last row, and, at the end of the loop, place the saved pixel in the top row.

Between those two lines, shift the pixels from row to row. Here's how. Copyinto the pixel on the current row and copy from the pixel on the previous row. The loop starts at the last row and goes toward the top of the image. Theactualaddressis(row * width) + colforthecurrentpixel.

Go ahead and make test once again; only tests with negative dx or dy fail.

3. TRANSLATE()

PAGE 14-6

Step 4: Handle Negatives

One way to do this would be to check if dx or dy is negative, and then use a different loop that copies in the opposite direction (from left to right instead of right to left, for instance). That's fine, but it's really not needed.

The key is to realize that a dx of -1 (translate left) can be thought of as the same as adxofwidth 1(translateright).Ifwesimplyaddnegativedxvaluestowidthandadd negative dy values to height, we won't need to change our algorithm at all.

Here's the code for dx (before the first, horizontal, loop):

And here's the similar code for dy, before the vertical loops:

The remainder operator is used to eliminate extra loops, when either dx or dy are larger than the size of the image.

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

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions