MHB PrintShampooInstructions(): Function for Looping Cycles

  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function Loop
AI Thread Summary
The discussion revolves around creating a function called PrintShampooInstructions() that takes an integer parameter numCycles and has a void return type. The function should handle three scenarios based on the value of numCycles: if it is less than 1, it should print "Too few."; if it exceeds 4, it should print "Too many."; and for values between 1 and 4, it should print "N: Lather and rinse." for each cycle, followed by "Done." after the cycles are completed. A code snippet is provided, but there is an issue with the output order when numCycles is greater than 1. The current implementation prints the cycle number in reverse order. The solution involves using a separate counter variable, i, to correctly print the cycle number starting from 1 up to numCycles. The corrected output should reflect the proper sequence of cycle instructions.
Teh
Messages
47
Reaction score
0
Write a function PrintShampooInstructions(), with int parameter numCycles, and void return type. If numCycles is less than 1, print "Too few.". If more than 4, print "Too many.". Else, print "N: Lather and rinse." numCycles times, where N is the cycle number, followed by "Done.". End with a newline. Example output for numCycles = 2:

1: Lather and rinse.
2: Lather and rinse.
Done.



HTML:
#include <iostream>
using namespace std;

/* Your solution goes here  */
void PrintShampooInstructions(int numCyles) {
   int i = 1;
   if (numCyles < 1) {
      cout << "Too few." << endl;
   }
   else if (numCyles > 4) {
      cout << "Too many." << endl;
   }
   
   else {
          
     while (numCyles > 0) {
        cout << numCyles << ": Lather and rinse."    << endl;  
        i++;
        numCyles--;

   }
   cout << "Done." << endl;
   }
   return;
}int main() {
   PrintShampooInstructions(2);

   return 0;
}

Testing with 0Your output:
Too few.


Testing with 2



Expected output:
1: Lather and rinse.
2: Lather and rinse.
Done.



Your output:
2: Lather and rinse.
1: Lather and rinse.
Done.



Tests aborted.


Cant figure out how to change this:

2: Lather and rinse.
1: Lather and rinse.
Done.


Into:

1: Lather and rinse.
2: Lather and rinse.
Done.
 
Technology news on Phys.org
Print [m]i[/m] instead of [m]numCyles[/m].
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top