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

List of all available Websheets


Viewing cpp/functions/draw-square-2func 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 use the <tt>drawLine</tt> function that is provided.</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/functions/draw-square (author: redekopp@usc.edu)
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, with the
// given character as the first and last character on the line
// and all other characters are set to fill
void drawLine(int length, char first_last, char fill);
// 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);
]\
}
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;
}
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'.