-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2_hw.txt
145 lines (109 loc) · 2.4 KB
/
p2_hw.txt
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Homework 2 due April 21
1. Output produced from question 1:
endlendl
grendel
//at the end of cout, no new line was established so just adds on to current line. Once endl; was entered cursor moves to new line..type variable = assigned a string (initialized in declaration)
2. This program asks you to "Enter a number:" and assigned the number to the identifier len. the program only produced output if the number > 0. if you enter in 5, the program produces a forward slash shaped line of # signs spanning a number of lines = to len. if you enter in a double it spans a number of lines = to the interger value len.
FROM SOLUTIONS:
It prints a diagonal line of the specified length.
//original program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int len;
cout << "Enter a number: ";
cin >> len;
for (int i = 0; i < len; i++)
{
for (int j = i+1; j < len; j++)
{
cout << " ";
}
cout << "#" << endl;
}
}
3. //inner loop is while loop
#include <iostream>
#include <string>
using namespace std;
int main()
{
int len;
cout << "Enter a number: ";
cin >> len;
for (int i = 0; i < len; i++)
{
int j = 1+i;
while (j < len)
{
cout << " ";
j++;
}
cout << "#" << endl;
}
}
4.// do while loop for outer loop
#include <iostream>
#include <string>
using namespace std;
int main()
{
int len;
cout << "Enter a number: ";
cin >> len;
int i = 0;
do {
for (int j = i+1; j < len; j++)
{
cout << " ";
}
cout << "#" << endl;
i++;
} while (i<len);
return 0;
}
//correct way from SOLUTIONS
#include <iostream>
using namespace std;
int main()
{
int len;
cout << "Enter a number: ";
cin >> len;
if (len > 0) // No output whatsoever if len is nonpositive
{
int i = 0;
do
{
int j = i+1;
while (j < len)
{
cout << " ";
j++;
}
cout << "#" << endl;
i++;
} while (i < len);
}
}
5.
switch (codeSection) //controlling expression
{
case 281:
cout << "bigamy";
break;
case 321:
case 322:
cout << "selling illegal lottery tickets";
break;
case 383:
cout << "selling rancid butter";
break;
case 599:
cout << "artificially coloring a live rabbit";
break;
default:
cout << "some other crime";
}