Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that outputs all 99 stanzas of the \"Ninety Nine Bottles of beer

ID: 3641810 • Letter: W

Question

Write a program that outputs all 99 stanzas of the "Ninety Nine Bottles of beer on the Wall" song. Your program should print the number of bottles in English, not as a number. For example:
Ninety nine bottles of beer on the wall, Ninety nine bottles of beer,
Take one down, pass it around,
Ninety eight bottles of beer on the wall.
...
One bottle of beer on the wall, One bottle of beer,
Take one down, pass it around, Zero bottles of beer on the wall.
Your program should not use ninety nine different output statements!
CodeMate Hint: Write a function that takes as input an integer between 0 and 99 and outputs that value in English. Use / and % to extract the tens and the ones digit so you know what number to output in English.
CodeMate Hint: You may need to test specifically for numbers such as 0, 10-19. CodeMate Hint: You may use switch statements as necessary
//
//
//
//
//
//
//
//
// ======================================
//This program outputs the 99 bottles of beer on the wall song.
// A simple loop calls a function that outputs each stanza
// for a particular number of bottles of beer. //
// We use another function that takes a integer from 0-99 and
// outputs that integer as a word. It uses / and % to extract the
// tens digit and the ones digit so we don't need 100 different
// if-then-else statements.
#include <iostream>
#include <cstdlib>
using namespace std;
// Function prototypes
void print_stanza(int numstanzas); void print_num_in_english(int num);
// ======================
// print_num_in_english
// Outputs n in English.
// N must be between 0-99.
// ======================
void print_num_in_english(int num) {
// -------------------------------- // ----- ENTER YOUR CODE HERE ----- // --------------------------------
return; }
// ======================
// print_stanza
// Outputs an entire stanza for n bottles. // ======================
void print_stanza(int n)
{
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// -------------------------------- return;
}
======================================= Ninety-Nine Bottles
=======================================
HOMEWORK-5
Section: PUT here YOUR SECTION NUMBER
//
//
//
int main() {
====================== main function
======================
// -------------------------------- // ----- ENTER YOUR CODE HERE ----- // --------------------------------
//
// Loop from 99 down to 0
// Call print_stanza from inside the loop
}

Explanation / Answer

#include
#include
using namespace std;

string number(int n);

int main(){
for(int bottles = 99; bottles > 0; bottles--){
cout << number(bottles) << (bottles == 1 ? " bottle" : " bottles") << " of beer on the wall ";
cout << number(bottles) << (bottles == 1 ? " bottle" : " bottles") << " of beer ";
cout << "Take one down, pass it around, ";
cout << number(bottles-1) << (bottles == 2 ? " bottle" : " bottles") << " of beer on the wall ";
cin.get();
}
}

string num1[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string num2[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string num3[] = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

string number(int n){
string result;

if(n < 10) result = num1[n];
else if(n < 20) result = num2[n-10];
else{
int dig1 = n/10, dig2 = n%10;
result = num3[dig1-2];
if(dig2 != 0) result += "-" + num1[dig2];
}

result[0] = toupper(result[0]);
return result;
}

Try using arrays to shorten this puppy up!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote