-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path335p1.cpp
108 lines (98 loc) · 2.86 KB
/
335p1.cpp
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
// 335p1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//this function will just print all the elements in the diskList
void printDiskList(vector<string> diskList)
{
for (string element : diskList)
{
cout << element << " ";
}
cout << endl;
}
//this struct is used so that the sortDisks function can return both a vector and an int, as required
//by the project description
struct diskResult
{
int m;
vector<string> diskList;
//constructor
diskResult(vector<string> _list, int _m)
{
diskList = _list;
m = _m;
}
};
diskResult sortDisks(int n, vector<string> diskList)
{
int moves = 0;
//here we will loop the swaps for (n/2)+1 times
//because this allows odd number of n to be processed correctly without
//affecting the M count.
for (int k = 0; k < ((n/2)+1); k++)
{
//loop through every element going forward -1 because we are looking "ahead"
for (int i = 0; i < ((2 * n)-1); i++)
{
//if a white is on the left, with a black on the right, swap them.
if (diskList[i] == "White" && diskList[i + 1] == "Black")
{
moves++;
diskList[i] = "Black";
diskList[i + 1] = "White";
}
}
//printDiskList(diskList);
//loop through every element going backward
for (int i = ((2*n)-1); i > 0; i--)
{
//if a white is on the left, with a black on the right, swap them.
if (diskList[i] == "Black" && diskList[i - 1] == "White")
{
moves++;
diskList[i] = "White";
diskList[i - 1] = "Black";
}
}
//printDiskList(diskList);
}
//construct our return object.
diskResult result = diskResult(diskList, moves);
return result;
}
int main()
{
int n = 5;
cout << "Project 1: CPSC 335!\n";
cout << "Javier Perez\n";
cout << "Please enter the n disks: ";
//set up our diskList
vector<string> diskList;
cin >> n;
//fill our list as described by project description
//"They alternate: light, dark, light, dark, and so on."
for (size_t i = 0; i < (2*n); i++)
{
if (i % 2 != 0)
{
diskList.push_back("Black");
}
else
{
diskList.push_back("White");
}
}
//print starting diskList
printDiskList(diskList);
cout << "Sorting..." << endl;
//call our sortDisks function.
diskResult result = sortDisks(n, diskList);
//print resulting list, sorted.
printDiskList(result.diskList);
cout << "Total Moves: " << result.m << endl << "Enter any character to close this window!\n";
cin >> n;
return 0;
}