EECS 183 Lab 4: Loops Exercises
EECS 183 Lab 4: Loops Exercises
When n is 5, the printRight function outputs a right-angled triangle made of asterisks with increasing row lengths, like this: * ** *** **** ***** . The outer loop iterates from 1 to n (in this case, 5), and each row's length corresponds to the current iteration count 'i'. The nested loop runs 'i' times for each iteration, leading to a progressive increase in star count per line.
In the printRightJustified function, the outer loop variable decreases by one each time to correctly position the stars with leading spaces, creating a right-justified right triangle. With each iteration, the outer loop starts with a larger number of spaces, progressively reducing them to shift the stars to the right until the stars reach the edge. This leftward decrement of the outer loop variable i results in the indentation of stars on the left, simulating right alignment.
The printIsosceles function ensures symmetry by decrementing the 'm' variable to control the initial number of spaces printed before the stars in each line. The decreasing value of 'm' provides left-side spacing to compensate for the increasing pattern width created by repeating '*' for 2*i+1 times. This ensures that the triangle's peak is centered, retaining its symmetrical form. Consequently, variable 'm' aligns the triangle by managing consistent spacing across lines, causing the pointed pyramid shape.
To modify the function printRectangle to print a filled rectangle with different characters, an additional parameter can be introduced to accept the character to be printed. The function can be updated as follows: void printRectangle(int rows, int cols, char fillChar) { for(int i = 1; i <= rows; i++) { for(int j = 1; j <= cols; j++) { cout << fillChar; } cout << endl; } } This modification allows the function to print with any given character, enhancing its flexibility and usability.
Potential errors in modifying printRightJustified include incorrect loop bounds or step values, leading to misalignment or incorrect triangle dimensions. For example, incorrectly adjusting space or star loop increments can skew the triangle shape. Mitigation involves clearly understanding output requirements, iteratively testing for edge cases, methodically adjusting loop variables, and maintaining accurate documentation to ensure the increment and decrement steps accurately reflect the triangle’s desired shape.
The cout statement in each function is crucial for producing output to the console, serving as the mechanism through which the stars and spaces are rendered according to loop logic. If cout behavior were altered, such as buffering output or changing print targets, it could delay display results or direct output in unintended ways. For precise control of output appearance and timing, ensuring cout works without interference is essential. Misuse or modification of cout can disrupt the intended sequential and formatted display.
To generalize printIsosceles for different characters and line spacings, additional parameters for character type and inter-line spacing can be introduced. Modify loops to print adjustable distance between printed lines such as: void printIsosceles(int n, char fillChar, int spacing) { int m = n; for(int i = 0; i < n; i++) { for(int j = m; j > 1; j--) cout << " "; for(int k = 0; k <= i * 2; k++) cout << fillChar; cout << endl; for(int s = 0; s < spacing; s++) cout << endl; m--; } } This retains the triangle’s symmetry while enabling customization of character and space.
To adapt printRectangle without using nested loops, a single loop combined with a string concatenation approach can be used. First, create a single row string with repetitively concatenating the character '*'. Multiply this by the number of rows and print each resulting 'row'. The code adaptation is: string row(cols, '*'); for(int i = 0; i < rows; i++) { cout << row << endl; } This consolidation removes the inner loop, leveraging efficiency from fixed-length string operations.
Modifying the iterative conditions in printRectangle can significantly alter its output. For instance, altering loop boundaries may lead it to print fewer or more rows/columns than intended. If conditions like 'i = 1' were mistakenly switched to 'i = 0', the loop could produce extra rows or columns due to an off-by-one error. To prevent such issues, best practices include thoroughly testing boundary conditions, clearly documenting intended loop iterations, and ensuring intent aligns with loop controls.
The computational efficiency of the nested loops in printRectangle is O(rows * cols), as each loop independently iterates a fixed number of times based on its parameters. While this is optimal for rendering fixed-size grids, enhancements are possible via reduced I/O operations, such as preloading output into a buffer and printing in fewer calls. However, given the function's constraints and purpose, the loops are theoretically efficient relative to necessary operations, with limited space for further optimization without altering fundamental logic.