C++ Word Alignment Adjustment Assignment Help!

If anyone here can understand this assignment, please answer it in simple way as possible and in steps.I have already asked my Professor, friends and TA but they couldn't explain it to me and I am really confused. Before I start coding, I must understand what problem I am trying to solve. He gave some hints and tips.

**I just want the description of the assignment in other words. I am not asking anyone to do this assignment for me. **


Here is the question:




The mission of this assignment is to realize a simple paragraph justifier. As shown in my coding demonstration, your program will keep reading lines of text image using getline(). After reading a paragraph, it adjusts all the lines into an instructed width, so that it can show the words evenly spread out and fit in a straight edge at both margins (similar to "align full" option in a Microsoft word document).

This can be relatively easily done as we are dealing with a mono-space font set (not a proportional font set) shown on a command-line-type console. As an example, consider a line containing 5 words and 30 characters altogether. If this line needs to be justified into a 40-character width, the remaining10 spaces needs to be spread out between the words. Assume that the first word of the next line has more than 9 characters, that is, the word in the next line cannot be placed in the tail of this line so as to consume the extra spaces.

In this case, 2 spaces are placed between the first 4 words, followed by 4 spaces prior to the last word (tail adjustment), or 3 spaces are placed between the first 4 words, followed by a space prior to the last word (even adjustment).

The requirements of this assignment are:

>program keeps reading lines of text until reading an empty line
>program then reads a width for the read paragraph
>program then justifies the paragraph based on tail adjustment
>program then shows the result in a bounding box
>program allows to adjust the paragraph by going back to 2
>program ends when it reads 0 as a new width
Extra points (2 points) will be considered for even adjustment implementation.

Here is a hint to realize this mission - lines of words, say vector<string> words, is a straightforward conversion as implemented in the previous assignment and posted lecture example. Now, user types some width for justification. Let this width be W. The mission is to fill i-words in one line of this W. Note i > 0, i.e., every line must have at least one word. The logic will be:

>let { w1, w2, ..., wi } be a collection of words. Then, w1.length() + w2.length() + ... + wi.length() is the total length of this collection
>since we need at least one space between these i words, we need i - 1 spaces at least, and therefore, we need Wmin = w1.length() + w2.length() + ... + wi.length() + (i - 1) characters, which must be less than or equal to W
>your first loop must identify this i and Wmin by going through vector<string> words you created
>in the second loop, you simply create a line by adding w1 through wi-1 by placing a space between the two consecutive words
>before placing the last word wi, you need to place W - Wmin spaces because this number is the excess spaces to fill in for justification (tail adjustment)!
Last edited on
Hi,

Could you please edit your post so it uses quote tags, not code tags. Select the text, and press the "" button At the moment it is 5 screens wide on my laptop.

http://www.cplusplus.com/articles/z13hAqkS/

Which part of the problem don't you understand? It seems like a fairly explicit explanation.

Try writing pseudo code, you have an outline, write those as comments in a file. Then put more steps in to break down each of the given steps into smaller tasks. Keep progressively refining it until you are happy to write code.

Good Luck !!

@TheIdeasMan Sorry about the format. I am new here. So, I understand that first you check your line and see if it goes beyond the width that user inputs. Then, we do the spacing accordingly.
For example: " I am going to the cinema to watch Superman Vs. Batman" and I want to this in width of "10" The total character including the space is 53.
This is where i do not understand the math. I don't know how much spacing you have to do and the part
Wmin
. Thank You for taking your time to take a look at it.
Last edited on
sadij97 wrote:
This is where i do not understand the math. I don't know how much spacing you have to do and the part


What about this part:

assignment wrote:
>let { w1, w2, ..., wi } be a collection of words. Then, w1.length() + w2.length() + ... + wi.length() is the total length of this collection
>since we need at least one space between these i words, we need i - 1 spaces at least, and therefore, we need Wmin = w1.length() + w2.length() + ... + wi.length() + (i - 1) characters, which must be less than or equal to W
>your first loop must identify this i and Wmin by going through vector<string> words you created
>in the second loop, you simply create a line by adding w1 through wi-1 by placing a space between the two consecutive words
>before placing the last word wi, you need to place W - Wmin spaces because this number is the excess spaces to fill in for justification (tail adjustment)!


It explains well I think. Apparently your previous assignment detailed how to make a vector of words.

What are your thoughts on this idea?

TheIdeasMan wrote:
Try writing pseudo code, you have an outline, write those as comments in a file. Then put more steps in to break down each of the given steps into smaller tasks. Keep progressively refining it until you are happy to write code.

@TheIdeasMan I really appreciate your answer and Thank You again! I will comment the outline and write a psuedo code.
Last edited on
Ah good ! Thanks for the appreciation :+) More importantly though:

Often a bit of planning / design is worthwhile as opposed to just tearing in and writing code.

Also I have mentioned pseudo code to a number of people, most find too boring. I think it is a really good tool: it helps to divide and conquer; identify loops and functions, data types etc.

So good on you for giving it a go !! :+)
Last edited on
Topic archived. No new replies allowed.