Hello, I need help writing the following c++ code! Please make it legible and cl
ID: 3532393 • Letter: H
Question
Hello, I need help writing the following c++ code!
Please make it legible and clear as to which question is being answered. THANK YOU!!!
2.8 Write nested loops (for or while loops) which would output the following pattern of stars. Hint: the way to solve these problems is to number the n rows either 0 to n-1 or 1 to n and then find a relationship between the row number and the number of stars to print on each row. That relationship will tell you how many times the inner loop needs to execute.
**
****
******
********
**********
************
**************
****************
2.10 Write nested loops (for or while loops) which would output the following pattern of stars; the box character represents a space.
?????*
????**
???***
??****
?*****
******
2.11 The following program contains several mistakes (not syntax errors) that will cause bugs. Identify and explain each mistake. Assume the necessary header files are including and the using namespace std line is present.
// This program displays the average of quiz scores. You may assume that each quiz score will be entered
// as an is an integer in the range [0, 10].
int main()
{
int count = 0, score, sum;
cout << "Enter quiz score (negative value to quit)? ";
cin >> score;
while (score > 0) {
sum += score;
++count;
cout << "Enter quiz score (negative value to quit)? ";
cin >> score;
}
if (count = 0) {
cout << "No average could be calculated." << endl;
} else {
double avg = static_cast<double>(sum) / count;
cout << fixed << setprecision(1);
cout << "The average is " << avg << endl;
}
return 0;
}
Explanation / Answer
2.8
http://ideone.com/VW072l
2.10
http://ideone.com/7wANai
2.11
int main()
{
int count = 0, score, sum;
cout << "Enter quiz score (negative value to quit)? ";
cin >> score;
while (score > 0) {
sum += score; //sum was not initialized, so sum is adding to junk value
++count;
cout << "Enter quiz score (negative value to quit)? ";
cin >> score;
}
if (count = 0) { //count =0 sets count to zero, thus, this if statement would ALWAYS be true
//use count == 0 to compare count to 0
cout << "No average could be calculated." << endl;
}
else {
double avg = static_cast<double>(sum) / count;
cout << fixed << setprecision(1);//should be: cout<<setiosflags(ios::fixed)<<setprecision(1);
cout << "The average is " << avg << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.