Question #306401


  1. Your task is to define the sendApology() function with the following details:
  2. Return type - void
  3. Function name - sendApology
  4. Parameters - a character and an integer.
  5. The sendApology() function will first check if the character passed is either 's', 'o', 'r', or 'y'. If it is, print it based on the passed integer number of times. For example, if the passed integer is 5, then you need to print the character 5 times. Otherwise, just ignore it (i.e. don't do anything).

OUTPUT

sooorrrrrrrrryyyyy

Given Code Editor:

#include <iostream>

using namespace std;

void sendApology(char, int);

int main(void) {

sendApology('s', 2);

sendApology('f', 10);

sendApology('b', 3);

sendApology('o', 5);

sendApology('r', 12);

sendApology('m', 5);

sendApology('v', 2);

sendApology('y', 7);

 return 0;

}

void sendApology(char letter, int n) {

  // TODO: Implement the function definition  

}


Expert's answer

#include <iostream>

using namespace std;
void sendApology(char, int);

int main(void)
{
  sendApology('s', 2);
  sendApology('f', 10);
  sendApology('b', 3);
  sendApology('o', 5);
  sendApology('r', 12);
  sendApology('m', 5);
  sendApology('v', 2);
  sendApology('y', 7);
  return 0;
}

void sendApology(char letter, int n) {
  if (letter == 's' || letter == 'o' 
    || letter == 'r' || letter == 'y') {
    for (int i = 0; i < n; i++) {
      cout << letter;
    }
  }
}
LATEST TUTORIALS
APPROVED BY CLIENTS