# (Homework Solution): Can someone help me with this assignment? EXERCISE Submit answers for the following question. (4 pts) Assume the following declarations

Can someone help me with this assignment?

EXERCISE

Submit answers for the following question.

(4 pts) Assume the following declarations:

char name[21];

char yourName[21];

char studentName[31];

Mark each of the following statement as valid or invalid. If a statement is valid, explain what it does. For an invalid statement, explain what’s wrong and correct it.

a. yourName[0] = ‘’;

b. yourName = studentName;

c. if (yourName == name) // if contain same words

std::cout << “Same word in ” << yourName << ” and ” << name;

d. for (int j=0; j < name.length; j++)

std::cout << name[j];

PROJECT: Image Processing

Write a program to simulate image processing (blur an image, see Unit 3_3_ch8_2dArray slides). An image file is simulated by a 2D array of ints. Shell code is provided in HW5_image_shell.cpp, which includes the following functions:

Process an image file (blur the image).

Print out blurred image on screen.

The screenshot below shows a sample image:

Remember, image blurring is done by calculating the weighted average of each pixel (each element of the 2D array). The weights are specified by a predefined 3 by 3 weight mask:

1 2 1

2 2 2

1 2 1

The center weight 2 (highlighted) is applied to a pixel itself and the other weights are applied to its 8 neighbors.

New value of a pixel = [ sum (weight for itself/neighbor × value of itself/neighbor) ] / sum of weights

We will use approach 1 for pixels with less than 8 neightbors:

Approach 1: pixels with fewer than 8 neighbors are not processed

Blurred result:

For example, new value for pixel[1][1] (in yellow rectangle) would be calculated based on itself and 8 neighbors.

new value for pixel[1][1]

= (1 * 10 + 2 * 100 + 1 * 10 +

2 * 10 + 2 * 300 + 2 * 10 +

1 * 100 + 2 * 10 + 1 * 100) / 14     // 14 is the sum of the weights: 1+2+1 + 2+2+2 + 1+2+1

= 1080 / 14

= 77.14… à 77 is kept as the result

ADD #1: declaration of function blur. Be sure to add IN/OUT/INOUT comment to each parameter.

ADD #2: implementation of function blur

You’re not supposed to modify the given code otherwise (at least 10% penalty).

Submission: 2 files

In one word/pdf document:

Project: screenshot of program execution

One cpp file: HW5_image_YourLastName.cpp

Use the rubric below to check the completeness of your work before turning it in.

Rubric: CS225, HW5

 Item Max (Pts) Received (Pts) Exercise (4 pts) E1. C-string: for each one, ½ pts for answer, ½ pts for explanation 4 Project (14 pts) function declaration for function blur with param type comments 1 implementation of function blur Old value of each pixel is used when calculating new values all pixels with 8 neighbors are processed correctly approach 1: all pixels with fewer than 8 neighbors use old value 3 6 3 Your project compiles and runs. 1 Programming style (2 pts) Consistent indentation: preferred 2 spaces for each level Meaningful names for constants and variables; Proper comments in the program 0.5 1.5 Penalty Modified given .cpp file in unspecified ways At least (-2) Missing required screenshot(s) (-2) Total: 20

// HW5_image_shell.cpp (shell code. ADD CODE #1 and #2)
// by Bin “Crystal” Peng, CS225
// image processing
// blur an image. print it on screen

#include <iostream>
#include <iomanip> // std::setw()

//———————————————-
// global declarations
//———————————————-

// max dimension of 2D array
const int MAX_ROW = 100;
const int MAX_COL = 100;

//———————————————-
// function declarations
//———————————————-

// blur an image
// Pre: pic filled with height x width numbers
// Post: pic is blurred using a 3 x 3 predefined weight mask

// ADD CODE #1: function declaration

// print an image on screen
void printImage(const int pic[][MAX_COL]/*IN*/, int height/*IN*/, int width/*IN*/);
// Pre: pic filled with height x width numbers
// Post: image printed to screen. The height and width are printed first and then the image file data is printed

//———————————————-

int main()
{
// one image
int image[MAX_ROW][MAX_COL] = {
{ 10, 100, 10, 100, 10, 100 },
{ 10, 300, 10, 300, 10, 300 },
{ 100, 10, 100, 10, 100, 10 },
{ 300, 10, 300, 10, 300, 10 } };

int imgHeight = 4; // height of image
int imgWidth = 6; // width of image

// process the image
blur(image, imgHeight, imgWidth);
printImage(image, imgHeight, imgWidth);

return 0;
} // end main

//———————————————-
// Function Implementation
//———————————————-

// blur an image
// Pre: pic filled with height x width numbers
// Post: pic is blurred using a 3 x 3 predefined weight mask

// ADD CODE #2: implementation of function blur

// print an image to output stream obj out
void printImage(const int pic[][MAX_COL]/*IN*/, int height/*IN*/, int width/*IN*/)
{
std::cout << height << ‘ ‘ << width << ‘n’;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
std::cout << std::setw(4) << pic[row][col];
}
std::cout << ‘n’;
}

} // end printImage

a. yourName[0] = ‘’;

It is a valid statement. Assigns null character to the first(zeroeth) location of the array yourName.

b. yourName = studentName;

Invalid statement. Assignment to expression with array type will return an error as it’s not allowed.

You can use

`strncpy(yourName, "studentName", 24); to assign values. It copies upto 24 characters from the string pointed to, by studentName to yourName.`

Or you can use,

char yourName[21]=”studentName”; during the declaration itself.

c. if (yourName == name) // if contain same words

std::cout << “Same word in ” << yourName << ” and ” << name;

Invalid statement as two arrays cant be matched in this way. You can use strcmp() function to check whether both arrays hold the same value.

if (strcmp(yourName, name)==0)
std::cout<<“Same word in “<<yourName<<” and “<<name;
else
std::cout<<“no”;

strcmp() returns 0 if both are identical. The standard library string.h should be imported to use this string fuction strcmp().

d. for (int j=0; j < name.length; j++)

std::cout << name[j];

It would return an error as length is of non-class type ‘char[21]’. So if you want to print the elements of the array name you can use the function strlen() instead. The code goes like this,

for (int j=0; j < strlen(name); j++)

std::cout << name[j];

Hope it was helpful enough to answer your questions. Have a thumps up if it did. Thank You.

Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

## Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.