### Iterative Loops And Small Problems #1-3

Pages: 123
#1:

 ``12345678910111213141516171819`` ``````#include #include #include #include #include int main() { int numberboxes {}; std::cout << "How many boxes: "; std::cin >> numberboxes; std::cin.ignore(); std::cout << "Enter Mailing Address: "; std::vector addr; for (std::string s; std::getline(std::cin, s) && !s.empty(); addr.emplace_back(s)); for (int n = 0; n < numberboxes; std::cout << "BOX NUMBER " << ++n << " OF " << numberboxes << "\n\n") std::copy(addr.begin(), addr.end(), std::ostream_iterator(std::cout, "\n")); return 0; }``````

#2:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include using namespace std; //printTabs function to calculate and output table void printTabs(int startValO, int rowO, int incrementO) { int answer; for(int i = 1; i <= rowO ; i++) //start loop to control rows { for(int j = 1; j <= 3; j++) //loop to control columns { answer = pow(startValO, j); //expression to calculate square and cube of starting value/incremented value with column j cout << answer << '\t'; //out put of integer answer }//End of row loop startValO += incrementO; // to increment the initial value cout << endl; }//End of row loop }//End of printTabs //selTabs to void selTabs(int startValP, int rowP, int incrementP) { //if statement to set increment to 1 if a value of 0 or less us input if(incrementP < 1) incrementP = 1; printTabs(startValP, rowP, incrementP); } //main function to call table from function printTabs int main() { int startVal, rows, increment; //user instructions and input cout << "Enter starting value: "; cin >> startVal; cout << "Enter the number of values to be displayed: "; cin >> rows; cout << "Enter the increment between the values: "; cin >> increment; //display headings cout << "NUMBER" << '\t' << "SQUARE" << '\t' << "CUBE" << endl; //call and produce table from void function printTabs selTabs(startVal, rows, increment); return 0; }``````

#3:

 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace std; int main() { char grade; int passing=0; int failing=0; int student=10; for (int student = 10; student > 0; student--) { cout<<"Enter a grade: "<>grade; if (grade== 'a') passing++; else if (grade== 'b') passing++; else if (grade== 'c') passing++; else if (grade== 'd') passing++; else if (grade== 'e') failing++; } cout<
#2. As I said previously, you don't need seltabs() function. Also printtabs() can be simplified and the NUMBER column number taken out of the inner loop. Also, you don't need i.

 ``123456789101112131415161718192021222324252627282930313233343536`` `````` #include #include using namespace std; void printTabs(int startValO, int rowO, int incrementO) { if (incrementO < 1) incrementO = 1; for (; startValO <= rowO * incrementO; startValO += incrementO) { cout << startValO << '\t'; for (int j = 2; j <= 3; ++j) cout << pow(startValO, j) << '\t'; cout << endl; } } int main() { int startVal {}, rows {}, increment {}; cout << "Enter starting value: "; cin >> startVal; cout << "Enter the number of values to be displayed: "; cin >> rows; cout << "Enter the increment between the values: "; cin >> increment; cout << "\nNUMBER" << '\t' << "SQUARE" << '\t' << "CUBE" << endl; printTabs(startVal, rows, increment); }``````

#3. You can do:

 ``12`` ``````if (grade == 'a' || grade == 'b' || grade == 'c' || grade == 'd') ++passing;``````

where || means logical or.

or

 ``12`` ``````if (grade >= 'a' && grade <= 'd') ++passing;``````

where && is logical and.
Last edited on
doesn't matter I got a 6/6 anyways...
It does for your knowledge development of C++.
If you look back at the original assignment in the first post, there was no prompt for the mailing address. (The user does not enter it.)

You can define a constant that contains the mailing address and skip the lines (lines 12 - 14) where you ask for the address and have the user enter it.