Iterative Loops And Small Problems #1-3

Pages: 123
#1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

int main() {
int numberboxes {};
std::cout << "How many boxes: ";
std::cin >> numberboxes;
std::cin.ignore();
std::cout << "Enter Mailing Address: ";
std::vector<std::string> 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::string>(std::cout, "\n"));

return 0;
}


#2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cmath>
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
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: "<<endl;
cin>>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<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
return(0);
}
#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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <iostream>
#include <cmath>
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:

1
2
if (grade == 'a' || grade == 'b' || grade == 'c' || grade == 'd')
    ++passing;


where || means logical or.

or

1
2
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.
Registered users can post here. Sign in or register to post.
Pages: 123