C++ Please !! 1. Predict the output of this code: #include <iostream> #include <
ID: 3738135 • Letter: C
Question
C++ Please !!
1. Predict the output of this code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string mascot = "Go Cougars";
mascot.replace(4, 7, "alBaptist");
mascot.append("!");
mascot.insert(13, " Lancers");
cout << mascot << endl;
return 0;
}
2. Debug this code segment. There is only one error. Output should have two different randoms, the first is not seeded with time, the other is.
#include <iostream>
#include <cstdlib> // Enables use of rand()
#include <ctime> // Enables use of time()
using namespace std;
int main()
{
//int randomNum = rand();
cout << "Look at this semi-random number: ";
cout << rand();
cout << "." << endl;
srand(time(0));
cout << "Look at this actual random number: ";
cout << rand
cout << "."<< endl;
return 0;
}
3. Debug this code segment. There is only one error. (Hint: this is a different type of error). Objective of this program is to get a sentence input from the user and display the sentence (including spaces).
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string output;
cout << "Please input your sentence." << endl;
cin >> output;
cout << "Here is your sentence " << output << endl;
return 0;
}
4. Predict the output of this program:
#include "stdafx.h"
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
float annualIncome = 20000.00, taxTotal;
if (annualIncome < 20000) {
taxTotal = annualIncome* 0.05;
taxTotal = taxTotal * 100;
taxTotal = floor(taxTotal) / 100;
cout << "Your tax rate is 5% and your tax amount is " << taxTotal << endl;
}
else if (annualIncome < 100000.00) {
taxTotal = annualIncome * 0.1;
taxTotal = taxTotal * 100;
taxTotal = floor(taxTotal) / 100;
cout << "Your tax rate is 10% and your tax amount is " << taxTotal << endl;
}
else {
taxTotal = annualIncome * 0.15;
taxTotal = taxTotal * 100;
taxTotal = floor(taxTotal) / 100;
cout << "Your tax rate is 15% and your tax amount is " << taxTotal << endl;
}
return 0;
}
Explanation / Answer
Ans1. Output: Go CalBaptist Lancers!
Ans2. The correct program is
#include <iostream>
#include <cstdlib> // Enables use of rand()
#include <ctime> // Enables use of time()
using namespace std;
int main()
{
//int randomNum = rand();
cout << "Look at this semi-random number: ";
cout << rand();
cout << "." << endl;
srand(time(0));
cout << "Look at this actual random number: ";
cout << rand(); //Correction at this line. it should be a rand() function.
cout << "."<< endl;
return 0;
}
Ans3. Here is the correct code
#include <iostream>
#include <string>
using namespace std;
int main()
{
string output;
cout << "Please input your sentence." << endl;
getline(cin,output); //getline should be used for entering a sentence as white space will no more be assumed as line break
cout << "Here is your sentence " << output << endl;
return 0;
}
Ans4. Output: Your tax rate is 10% and your tax amount is 2000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.