This is the second assignment for my CS2 course:
Write a recursive C++ function
writeLine()
that writes a character repeatedly to form a line of n characters. For example:writeLine('*', 5)
produces the output:
*****
Write a recursive [C++] function
writeBlock()
that uses writeLine to writem
lines ofn
characters each. For example,writeBlock('*', 5, 3)
produces the output:
***** ***** *****
Implement and test a recursive C++ function
writePyramid()
. The function should take in a character to build a pyramid with and the
height of the pyramid (the number of lines in it). Hint: Each line in the pyramid should be generated by two calls to writeLine: the first
call generates the spaces and a second call to generate the line of
characters. The␣
in the output below represents a space character. This output is generated when the function is asked to generate a pyramid of height 3 with the*
character.␣␣* ␣*** *****
Please feel free to rip the code apart! In my opinion, I think the switch
in main()
could be DRYed up a bit, but I wasn't sure how.
lab2.cpp
:
#include <iostream>
#include <cstdio>
#include "lab2functions.h"
char getSanitizedChar()
{
// absorb newline character (if existant) from previous input
if('\n' == std::cin.peek()) std::cin.ignore();
return std::tolower(std::cin.get());
}
int main()
{
char type = '*';
char shape = 'l';
int rows = 0;
int cols = 0;
do
{
std::cout << "Enter shape you would like {(L)ine, (b)lock, (t)riangle}: ";
std::cin >> shape;
switch(shape)
{
case 'l':
std::cout << "Enter number of columns: ";
std::cin >> cols;
std::cout << "Enter character to build with (*): ";
type = getSanitizedChar();
writeLine(('\n' == type ? '*' : type), cols);
std::endl(std::cout);
break;
case 'b':
std::cout << "Enter number of rows: ";
std::cin >> rows;
std::cout << "Enter number of columns: ";
std::cin >> cols;
std::cout << "Enter character to build with (*): ";
type = getSanitizedChar();
writeBlock(('\n' == type ? '*' : type), rows, cols);
break;
case 't':
std::cout << "Enter number of rows: ";
std::cin >> rows;
std::cout << "Enter character to build with (*): ";
type = getSanitizedChar();
writePyramid(('\n' == type ? '*' : type), rows);
break;
default:
std::cout << "Invalid option";
break;
}
std::cout << "\nRun a calculation again (y/N): "; // signify n as default with capital letter
} while ('y' == getSanitizedChar());
}
lab2functions.h
:
#ifndef LAB2FUNCTIONS_H
#define LAB2FUNCTIONS_H
#include <cstdint>
void writeLine(const char, const int8_t);
void writeBlock(const char, const int8_t, const int8_t);
void writePyramid(const char, const int8_t);
#endif
lab2functions.cpp
:
#include <iostream>
#include "lab2functions.h"
void writeLine(const char c, const int8_t num)
{
if (num > 0)
{
std::cout << c;
writeLine(c, num - 1);
}
}
void writeBlock(const char c, const int8_t m, const int8_t n)
{
if (m > 0)
{
writeLine(c, n);
std::endl(std::cout);
writeBlock(c, m - 1, n);
}
}
void writePyramid(const char c, const int8_t h)
{
static int count;
count++;
if (h > 0)
{
writeLine(' ', (2 * h - 1) / 2);
writeLine(c, (2 * count - 1));
std::endl(std::cout);
writePyramid(c, h - 1);
}
count = 0;
}