Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/functions/draw-square by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<p>Define a function <tt>drawSquare</tt> that takes an integer and char parameter
and prints a square:
   </p>
<ul><li>the integer indicates the side length of the square to draw, and
<li>the character indicates what character to use to draw the square.
</ul>
<p>See the example in the comments below.  </p>
<p>To achieve your task, try to update the <tt>drawLine</tt> function (and can include additional arguments) to be as useful as possbile.</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/functions/draw_square (author: redekopp@usc.edu)
Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
// Draws a horizontal line of the given length using the fill character.
// Update as needed to make it the most useful to help implement drawSquare
\[
void drawLine(int length, char first_last, char fill)
{
   cout << first_last;
   for(int i=0; i < length-2; i++){
      cout << fill;
   }
   cout << first_last << endl;
}
\show:
void drawLine(int length, char fill)
{
   for(int i=0; i < length; i++){
      cout << fill;
   }
   cout << endl;
}   
]\
// should print an ASCII square  
// of side length, s, using character, c
//  e.g. calling drawSquare(4, '*') should yield
//  ****
//  *  *
//  *  *
//  ****
\[ void ]\ drawSquare (int s, char c) 
{
\[
   drawLine(s, c, c);
   for(int i=0; i < s-2; i++){
      drawLine(s, c, ' ');
   }
   drawLine(s, c, c);
\show:
   // Update this *wrong* code, as needed
   for(int i=0; i < s; i++){
      drawLine(s, c);
   }
   
]\
}
C++ test suite
See manual
 
[
   ["check-function", "drawSquare", "void", ["int", "char"]],
   ["call-function", "drawSquare", ["3", "'*'"]],
   ["call-function", "drawSquare", ["10", "'+'"]],
   ["call-function", "drawSquare", ["30", "'-'"]]
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.