In: Electrical Engineering
Edit the given
program to produce the following output in c++ mode:
- Take in the name of a superhero & tell him how many villains
he/she has to defeat today - The number of villains is randomly
generated and should be a number between 11 and 42.
- Use a seed of 7.
Hint: Compile the program first before making edits
What is your name? Hello Captain America There are 42 villains you need to defeat today Oops! one of the villains was able to multiply himself by a power of 3. There are actually 74088 villains.
the program starts her
#include
<string>
//make sure that all necessary libraries are included &
correct
using namespace std;
int main() {
string name;
int number;
cout << "What is your name?" <<
endl;
cin >> name;
cout << "Hello " << name <<
endl;
cout << "There are "<< number >> "
villains you need to defeat today"<<
endl; // edit
and include any necessary code
cout << "Oops! one of the villains was able to
multiply himself by a power of 3. There are actually villains."
<< endl;
return 0;
}
The code is well commented and given below. Also the screenshot of the output and code itself is provided for better readability. Please go through the comments and if any doubts persist feel free to leave a comment asking clarification. All the best.
#include <string.h> //make sure that all necessary libraries are included & correct
#include <iostream>
#include <conio.h>
#include<math.h>
#define min 11 // Minimum number of villains
#define max 42 // Maximum number of villains
using namespace std;
int main() {
char name[30]; // Character buffer to store the name of the hero
int number; // Variable to store number of villains
long int new_number; // Variable to store new number of villains
// The variable is of type long to avoid overflow
srand(7); // Seeding random function with 7
cout << "What is your name?" << endl; // Asking for the name of Hero
gets(name); // Store the name in name array
cout << "Hello " << name << endl;// Acknowledging the hero
number = rand()%(max-min + 1) + min; // Calculating the villains to fight
cout << "There are "<< number <<" villains you need to defeat today"<< endl; // Printing results
new_number = pow(number,3); // Raising the power of villain by 3
cout << "Oops! one of the villains was able to multiply himself by a power of 3. There are actually "<<new_number<< " villains." << endl;
// Print results
return 0;
// Return
}
Output. (Notice that as we're using ramdom function so exact output cannot be generated every time.)