Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED TO FIX ERRORS ONLY AND SHOW CODE WORKING MY CODE #include #include #include using namespace std; int main() { int n; while (cin >>

NEED TO FIX ERRORS ONLY AND SHOW CODE WORKING

MY CODE

#include #include #include using namespace std;

int main() { int n; while (cin >> n && n != 0) { vector coaches(n); for (int i = 0; i < n; i++) { cin >> coaches[i]; }

stack station; int nextCoach = 1; bool possible = true; for (int i = 0; i < n; i++) { while (!station.empty() && station.top() == nextCoach) { station.pop(); nextCoach++; }

if (coaches[i] == nextCoach) { nextCoach++; } else if (!station.empty() && station.top() < coaches[i]) { possible = false; break; } else { station.push(coaches[i]); } }

if (possible) { cout << "Yes "; } else { cout << "No "; }

// Print a blank line after each test case cout << endl; } return 0; }

MY CODE STACK ADT FOR REQUIREMENT

#include #include #include

using namespace std;

bool can_rearrange_coaches(int n, vector coaches) { stack stack; int i = 0;

for (int coach : coaches) { while (!stack.empty() && stack.top() == i + 1) { i += 1; stack.pop(); }

if (coach == i + 1) { i += 1; } else { stack.push(coach); } }

while (!stack.empty() && stack.top() == i + 1) { i += 1; stack.pop(); }

return i == n; }

int main() { while (true) { int n; cin >> n;

if (n == 0) { break; }

vector coaches;

while (true) { int coach; cin >> coach;

if (coach == 0) { break; }

coaches.push_back(coach); }

if (can_rearrange_coaches(n, coaches)) { cout << "Yes" << endl; } else { cout << "No" << endl; }

cout << endl; }

return 0; }

NEED TO ADD THESE TWO FILES BUT KEPT GETTING ERRORS

input.txt:

162 10 139 138 4 28 117 63 31 48 74 11 49 118 52 128 153 130 47 17 95 110 18 70 24 112 26 64 115 141 30 87 8 46 34 80 36 13 38 39 102 61 76 120 23 156 106 116 68 85 147 59 121 78 37 53 50 54 91 101 60 94 22 107 71 123 6 67 103 145 152 75 134 73 108 25 140 77 104 127 20 81 2 154 96 151 86 135 88 89 90 58 1 93 161 29 35 97 56 79 82 124 19 7 142 21 143 100 129 109 146 111 5 98 55 45 133 43 16 148 92 113 42 65 83 125 126 114 51 3 62 144 157 150 57 40 136 137 159 162 131 119 69 44 99 158 14 33 9 149 105 15 32 41 12 155 122 132 84 66 72 160 27 1 2 6 5 9 8 7 4 3 12 11 13 10 17 18 20 22 21 25 31 30 32 33 29 34 35 28 41 40 42 39 43 38 46 47 48 51 50 49 45 52 44 56 55 54 53 37 57 36 58 59 60 27 26 61 62 24 23 19 16 65 66 64 67 68 63 15 75 74 73 72 76 77 79 80 82 83 81 85 88 87 94 93 95 92 96 97 91 90 99 102 101 104 105 103 107 111 110 109 108 106 100 112 98 89 114 117 116 118 115 113 119 121 120 123 122 124 126 127 128 130 129 125 131 132 135 136 139 138 140 137 134 133 142 145 148 149 147 153 155 154 152 151 156 150 146 144 160 159 158 157 161 143 141 86 84 78 71 70 69 14 162 144 44 136 40 128 141 8 115 125 131 58 55 145 14 64 28 19 59 76 48 25 16 20 27 7 143 63 112 17 30 65 34 39 130 117 67 82 120 85 154 107 42 151 99 96 46 47 159 162 22 66 12 53 157 113 38 97 11 29 18 10 24 60 75 4 132 90 122 56 79 119 72 150 80 103 35 106 102 15 54 69 6 62 105 5 84 87 88 89 51 91 70 93 95 123 140 77 41 26 100 111 23 161 142 36 133 57 92 109 21 50 104 110 149 101 155 134 61 160 129 98 68 33 124 73 126 1 135 152 32 118 138 147 49 139 3 137 31 52 71 13 2 45 127 9 146 108 43 78 86 121 81 153 148 83 156 114 94 116 158 37 74 82 123 152 156 5 23 125 162 96 61 108 92 110 14 20 86 60 75 124 48 129 84 111 24 121 157 37 34 50 30 114 32 113 44 115 58 158 74 88 131 151 19 6 102 80 54 4 126 8 10 51 89 53 29 7 56 12 128 59 90 55 38 35 33 159 16 112 137 69 142 99 72 46 9 101 3 1 78 57 15 141 140 83 146 154 120 130 41 49 43 2 63 65 42 67 91 138 160 97 21 85 66 144 104 105 106 107 109 70 87 95 133 45 81 11 116 149 118 119 100 28 136 93 13 26 52 127 145 153 77 73 36 27 31 135 122 134 94 64 17 161 132 143 103 139 18 147 79 98 150 47 76 71 22 39 68 148 117 62 25 155 40 1 39 89 4 97 21 49 8 12 154 29 27 121 14 79 91 94 158 19 109 132 124 23 84 16 26 157 73 81 28 5 17 47 86 35 80 146 126 71 40 133 90 61 99 111 46 134 48 95 33 51 20 112 156 45 102 57 41 147 60 106 100 161 64 65 66 9 11 74 110 36 15 115 69 63 152 3 118 2 145 30 82 155 58 122 135 120 88 31 98 34 44 150 142 148 96 72 125 92 54 101 128 59 119 105 139 25 78 24 70 153 53 162 62 160 104 68 56 123 18 103 114 87 6 52 136 127 141 32 38 131 117 10 138 140 159 137 13 37 108 116 67 143 130 107 76 113 75 149 85 43 50 22 93 83 55 129 144 7 77 42 151 1 2 4 5 3 8 7 6 9 10 11 13 12 16 15 17 21 22 20 19 18 14 23 24 25 26 28 27 29 36 37 35 38 43 42 41 40 39 34 33 48 49 50 51 52 53 47 46 55 54 45 56 44 58 57 32 31 60 61 62 59 64 65 69 68 72 71 74 73 70 67 75 66 63 30 77 78 79 76 82 81 84 83 85 80 90 91 89 92 94 93 88 95 87 86 96 97 101 102 100 103 105 104 99 98 109 112 111 113 114 110 120 119 125 124 123 126 122 121 127 118 129 131 130 136 135 137 139 138 140 134 133 142 147 146 148 152 153 151 156 157 158 160 159 161 155 154 150 149 145 144 143 141 132 128 117 116 115 108 107 106 162 2 3 1 6 5 7 8 9 14 17 16 19 18 26 25 24 27 29 28 23 30 31 22 34 37 38 39 40 36 42 41 43 44 45 35 46 33 32 47 21 20 15 13 12 48 11 50 52 53 51 49 10 4 55 56 54 58 60 61 59 62 57 64 63 65 66 69 70 71 68 67 76 77 75 79 82 81 80 78 83 74 84 73 85 87 86 88 72 89 90 91 93 92 98 97 101 102 100 99 96 103 104 111 112 113 110 109 108 114 117 122 125 124 127 131 132 130 137

136 138 140 143 145 152 151 150 149 148 153 147 154 146 155 144 142 157 156 141 139 135 159 158 161 160 134 133 129 128 126 123 121 120 119 118 116 115 107 106 105 95 94 162 0 0

sample output.txt

No Yes No No No Yes Yes

Question for Reference:

Railroad cars numbered 1,2, ..., n on the right track are to be permuted and moved along on the left track. A car may be moved directly onto the left track, or it may be shunted onto the siding to be removed at a later time and placed on the left track. The siding thus operates as a stack, a push operation moving a car from the right track onto the siding and a pop operation moving the "top" car from the siding onto the left track. For example, when n = 3, push 1, push 2, move 3, pop 2, pop 1 arranges them in the order 3, 2,1.

Are any permutations not possible? Yes, for example, 312 is an impossible permutation.

Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, 3,....N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the same direction so that their order will be. Your job is to help him to write a program that decides whether it is possible to get the required order of coaches.

Apply stack ADT in the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

13. You always should try to make a good first impression.

Answered: 1 week ago

Question

2. Show the trainees how to do it without saying anything.

Answered: 1 week ago