aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk5/lab4
diff options
context:
space:
mode:
Diffstat (limited to 'ee1301/wk5/lab4')
-rw-r--r--ee1301/wk5/lab4/arrayCat.cpp46
-rw-r--r--ee1301/wk5/lab4/mysteryBox.cpp36
-rw-r--r--ee1301/wk5/lab4/pOc.cpp24
-rw-r--r--ee1301/wk5/lab4/partner.cpp104
-rw-r--r--ee1301/wk5/lab4/partner2.cpp104
-rw-r--r--ee1301/wk5/lab4/partner3.cpp126
-rw-r--r--ee1301/wk5/lab4/strap012_lab4_w_2.cpp108
-rw-r--r--ee1301/wk5/lab4/swappy.cpp48
-rw-r--r--ee1301/wk5/lab4/time1.cpp48
-rw-r--r--ee1301/wk5/lab4/time2.cpp72
10 files changed, 358 insertions, 358 deletions
diff --git a/ee1301/wk5/lab4/arrayCat.cpp b/ee1301/wk5/lab4/arrayCat.cpp
index 1dbcb01..d711ba6 100644
--- a/ee1301/wk5/lab4/arrayCat.cpp
+++ b/ee1301/wk5/lab4/arrayCat.cpp
@@ -1,23 +1,23 @@
-#include <iostream>
-using namespace std;
-void append (char first[], int l1, char second[], int l2, char result[200]);
-
-int main () {
- char first[] = {'I', ' ', 'a', 'm', ' '};
- char second[] = {'i', 'r', 'o', 'n', 'm', 'a', 'n', '\0'};
- char result[200];
- append(first, 5, second, 8, result);
- cout << result << endl;
-}
-
-void append (char first[],int l1, char second[],int l2, char result[200]) {
- int length=0;
- for (int i=0; i<l1; i++) {
- result[length]=first[i];
- length++;
- }
- for (int i=0; i<l2; i++) {
- result[length]=second[i];
- length++;
- }
-}
+#include <iostream>
+using namespace std;
+void append (char first[], int l1, char second[], int l2, char result[200]);
+
+int main () {
+ char first[] = {'I', ' ', 'a', 'm', ' '};
+ char second[] = {'i', 'r', 'o', 'n', 'm', 'a', 'n', '\0'};
+ char result[200];
+ append(first, 5, second, 8, result);
+ cout << result << endl;
+}
+
+void append (char first[],int l1, char second[],int l2, char result[200]) {
+ int length=0;
+ for (int i=0; i<l1; i++) {
+ result[length]=first[i];
+ length++;
+ }
+ for (int i=0; i<l2; i++) {
+ result[length]=second[i];
+ length++;
+ }
+}
diff --git a/ee1301/wk5/lab4/mysteryBox.cpp b/ee1301/wk5/lab4/mysteryBox.cpp
index 4737fdd..c0c58ab 100644
--- a/ee1301/wk5/lab4/mysteryBox.cpp
+++ b/ee1301/wk5/lab4/mysteryBox.cpp
@@ -1,18 +1,18 @@
-#include <iostream>
-#include <string>
-using namespace std;
-
-bool mystery(string fstr);
-int main () {
- string e = "Sargent Pepper's Lonely Hearts Club Band";
- cout << mystery(e) << endl;
-}
-
-bool mystery(string fstr) {
- string rstr; // a string is like an array of chars, e.g., char[]
- for(int i=fstr.length()-1; i>=0 ;i--){
- rstr += fstr[i]; // fstr[i] gets the char at index i
- }
- cout << fstr << endl << rstr << endl;
- return rstr == fstr;
-}
+#include <iostream>
+#include <string>
+using namespace std;
+
+bool mystery(string fstr);
+int main () {
+ string e = "Sargent Pepper's Lonely Hearts Club Band";
+ cout << mystery(e) << endl;
+}
+
+bool mystery(string fstr) {
+ string rstr; // a string is like an array of chars, e.g., char[]
+ for(int i=fstr.length()-1; i>=0 ;i--){
+ rstr += fstr[i]; // fstr[i] gets the char at index i
+ }
+ cout << fstr << endl << rstr << endl;
+ return rstr == fstr;
+}
diff --git a/ee1301/wk5/lab4/pOc.cpp b/ee1301/wk5/lab4/pOc.cpp
index 93dbb06..919806f 100644
--- a/ee1301/wk5/lab4/pOc.cpp
+++ b/ee1301/wk5/lab4/pOc.cpp
@@ -1,12 +1,12 @@
-#include <iostream>
-using namespace std;
-
-int main(int argc, char* argv[]) {
-
- int i;
- cout << "You typed the following on the command line:" << endl;
- for (i = 0; i < argc ; i++) {
- cout << argv[i] << ", ";
- }
- cout << endl;
-}
+#include <iostream>
+using namespace std;
+
+int main(int argc, char* argv[]) {
+
+ int i;
+ cout << "You typed the following on the command line:" << endl;
+ for (i = 0; i < argc ; i++) {
+ cout << argv[i] << ", ";
+ }
+ cout << endl;
+}
diff --git a/ee1301/wk5/lab4/partner.cpp b/ee1301/wk5/lab4/partner.cpp
index 3313e8c..538163d 100644
--- a/ee1301/wk5/lab4/partner.cpp
+++ b/ee1301/wk5/lab4/partner.cpp
@@ -1,52 +1,52 @@
-#include <iostream>
-
-using namespace std;
-
-string requestName();
-double requestHeight(string fullName);
-int requestNumberOfPartners();
-
-
-int main()
-{
- string fullName[2]; //fullName1, fullName2;
- double height[2]; //height1, height2;
-
- fullName[0] = requestName();
- height[0] = requestHeight(fullName[0]);
- fullName[1] = requestName();
- height[1] = requestHeight(fullName[1]);
-
- cout << "If " << fullName[0] << " and " << fullName[1]
- << " form a human tower, their combined height will be "
- << (height[0] + height[1]) << endl;
-
-}
-
-string requestName()
-{
- string name;
- cout << "Please enter full name: ";
- getline(cin, name);
- return name;
-}
-
-double requestHeight(string fullName)
-{
- double height;
- cout << "Please enter " << fullName << "'s height: ";
- cin >> height;
- cin.ignore(2, '\n'); // gets rid of \n in the buffer
-
- return height;
-}
-
-int requestNumberOfPartners()
-{
- int numberOfPartners;
- cout << "How many partners are there?";
- cin >> numberOfPartners;
-
- return numberOfPartners;
-}
-
+#include <iostream>
+
+using namespace std;
+
+string requestName();
+double requestHeight(string fullName);
+int requestNumberOfPartners();
+
+
+int main()
+{
+ string fullName[2]; //fullName1, fullName2;
+ double height[2]; //height1, height2;
+
+ fullName[0] = requestName();
+ height[0] = requestHeight(fullName[0]);
+ fullName[1] = requestName();
+ height[1] = requestHeight(fullName[1]);
+
+ cout << "If " << fullName[0] << " and " << fullName[1]
+ << " form a human tower, their combined height will be "
+ << (height[0] + height[1]) << endl;
+
+}
+
+string requestName()
+{
+ string name;
+ cout << "Please enter full name: ";
+ getline(cin, name);
+ return name;
+}
+
+double requestHeight(string fullName)
+{
+ double height;
+ cout << "Please enter " << fullName << "'s height: ";
+ cin >> height;
+ cin.ignore(2, '\n'); // gets rid of \n in the buffer
+
+ return height;
+}
+
+int requestNumberOfPartners()
+{
+ int numberOfPartners;
+ cout << "How many partners are there?";
+ cin >> numberOfPartners;
+
+ return numberOfPartners;
+}
+
diff --git a/ee1301/wk5/lab4/partner2.cpp b/ee1301/wk5/lab4/partner2.cpp
index 973a44b..2b6bd36 100644
--- a/ee1301/wk5/lab4/partner2.cpp
+++ b/ee1301/wk5/lab4/partner2.cpp
@@ -1,53 +1,53 @@
-#include <iostream>
-
-using namespace std;
-
-string requestName();
-double requestHeight(string fullName);
-int requestNumberOfPartners();
-void NotinMain(string fullName[], const int length1, double height[], const int length2);
-
-int main()
-{
- string fullName[2]; //fullName1, fullName2;
- double height[2]; //height1, height2;
-
- fullName[0] = requestName();
- height[0] = requestHeight(fullName[0]);
- fullName[1] = requestName();
- height[1] = requestHeight(fullName[1]);
- NotinMain(fullName, 2, height, 2);
-}
-
-string requestName()
-{
- string name;
- cout << "Please enter full name: ";
- getline(cin, name);
- return name;
-}
-
-double requestHeight(string fullName)
-{
- double height;
- cout << "Please enter " << fullName << "'s height: ";
- cin >> height;
- cin.ignore(2, '\n'); // gets rid of \n in the buffer
-
- return height;
-}
-
-int requestNumberOfPartners()
-{
- int numberOfPartners;
- cout << "How many partners are there?";
- cin >> numberOfPartners;
-
- return numberOfPartners;
-}
-
-void NotinMain(string fullName[], const int length1, double height[], const int length2) {
- cout << "If " << fullName[0] << " and " << fullName[1]
- << " form a human tower, their combined height will be "
- << (height[0] + height[1]) << endl;
+#include <iostream>
+
+using namespace std;
+
+string requestName();
+double requestHeight(string fullName);
+int requestNumberOfPartners();
+void NotinMain(string fullName[], const int length1, double height[], const int length2);
+
+int main()
+{
+ string fullName[2]; //fullName1, fullName2;
+ double height[2]; //height1, height2;
+
+ fullName[0] = requestName();
+ height[0] = requestHeight(fullName[0]);
+ fullName[1] = requestName();
+ height[1] = requestHeight(fullName[1]);
+ NotinMain(fullName, 2, height, 2);
+}
+
+string requestName()
+{
+ string name;
+ cout << "Please enter full name: ";
+ getline(cin, name);
+ return name;
+}
+
+double requestHeight(string fullName)
+{
+ double height;
+ cout << "Please enter " << fullName << "'s height: ";
+ cin >> height;
+ cin.ignore(2, '\n'); // gets rid of \n in the buffer
+
+ return height;
+}
+
+int requestNumberOfPartners()
+{
+ int numberOfPartners;
+ cout << "How many partners are there?";
+ cin >> numberOfPartners;
+
+ return numberOfPartners;
+}
+
+void NotinMain(string fullName[], const int length1, double height[], const int length2) {
+ cout << "If " << fullName[0] << " and " << fullName[1]
+ << " form a human tower, their combined height will be "
+ << (height[0] + height[1]) << endl;
} \ No newline at end of file
diff --git a/ee1301/wk5/lab4/partner3.cpp b/ee1301/wk5/lab4/partner3.cpp
index fdddd69..75d6d7b 100644
--- a/ee1301/wk5/lab4/partner3.cpp
+++ b/ee1301/wk5/lab4/partner3.cpp
@@ -1,63 +1,63 @@
-#include <iostream>
-
-using namespace std;
-
-string requestName();
-double requestHeight(string fullName);
-int requestNumberOfPartners();
-void NotinMain(string array1[], double array2[], int numberOfPartners);
-
-int main()
-{
- int numberOfPartners = requestNumberOfPartners();
- string fullName[100]; //fullName1, fullName2;
- double height[100]; //height1, height2;
-
- // fullName[0] = requestName();
- // height[0] = requestHeight(fullName[0]);
- for (int i=0; i<numberOfPartners; i++) {
- fullName[i] = requestName();
- height[i] = requestHeight(fullName[i]);
- }
- NotinMain(fullName, height, numberOfPartners);
-}
-
-string requestName()
-{
- string name;
- cout << "Please enter full name: ";
- getline(cin, name);
- return name;
-}
-
-double requestHeight(string fullName)
-{
- double height;
- cout << "Please enter " << fullName << "'s height: ";
- cin >> height;
- cin.ignore(2, '\n'); // gets rid of \n in the buffer
-
- return height;
-}
-
-int requestNumberOfPartners()
-{
- int numberOfPartners;
- cout << "How many partners are there? ";
- cin >> numberOfPartners;
- cin.ignore(2, '\n');
-
- return numberOfPartners;
-}
-
-void NotinMain(string array1[100], double array2[100], int numberOfPartners) {
- double heightTotal=0;
- cout << "If " << array1[0];
- for (int i=1; i<numberOfPartners-1; i++) {
- cout << ", " << array1[i];
- heightTotal += array2[i];
- }
- cout << " and " << array1[numberOfPartners-1]
- << " form a human tower, their combined height will be "
- << (array2[0] + heightTotal + array2[numberOfPartners-1]) << endl;
- }
+#include <iostream>
+
+using namespace std;
+
+string requestName();
+double requestHeight(string fullName);
+int requestNumberOfPartners();
+void NotinMain(string array1[], double array2[], int numberOfPartners);
+
+int main()
+{
+ int numberOfPartners = requestNumberOfPartners();
+ string fullName[100]; //fullName1, fullName2;
+ double height[100]; //height1, height2;
+
+ // fullName[0] = requestName();
+ // height[0] = requestHeight(fullName[0]);
+ for (int i=0; i<numberOfPartners; i++) {
+ fullName[i] = requestName();
+ height[i] = requestHeight(fullName[i]);
+ }
+ NotinMain(fullName, height, numberOfPartners);
+}
+
+string requestName()
+{
+ string name;
+ cout << "Please enter full name: ";
+ getline(cin, name);
+ return name;
+}
+
+double requestHeight(string fullName)
+{
+ double height;
+ cout << "Please enter " << fullName << "'s height: ";
+ cin >> height;
+ cin.ignore(2, '\n'); // gets rid of \n in the buffer
+
+ return height;
+}
+
+int requestNumberOfPartners()
+{
+ int numberOfPartners;
+ cout << "How many partners are there? ";
+ cin >> numberOfPartners;
+ cin.ignore(2, '\n');
+
+ return numberOfPartners;
+}
+
+void NotinMain(string array1[100], double array2[100], int numberOfPartners) {
+ double heightTotal=0;
+ cout << "If " << array1[0];
+ for (int i=1; i<numberOfPartners-1; i++) {
+ cout << ", " << array1[i];
+ heightTotal += array2[i];
+ }
+ cout << " and " << array1[numberOfPartners-1]
+ << " form a human tower, their combined height will be "
+ << (array2[0] + heightTotal + array2[numberOfPartners-1]) << endl;
+ }
diff --git a/ee1301/wk5/lab4/strap012_lab4_w_2.cpp b/ee1301/wk5/lab4/strap012_lab4_w_2.cpp
index 1574abc..5b82cd4 100644
--- a/ee1301/wk5/lab4/strap012_lab4_w_2.cpp
+++ b/ee1301/wk5/lab4/strap012_lab4_w_2.cpp
@@ -1,54 +1,54 @@
-#include <iostream>
-using namespace std;
-
-void bsort(int list[], int length);
-void swap (int &a, int &b);
-void printArray (int list[], int length);
-void makeArray (int list[], int length);
-const int n=50;
-
-int main () {
-int list[n];
-makeArray(list,n);
-bsort(list, n);
-printArray(list, n);
-}
-
-//This function makes the array used for the rest of the program.
-void makeArray(int list[], int length) {
- int temp=100;
- for (int i=0; i<n; i++) {
- list[i]=temp;
- temp--;
- }
-}
-
-//This function is the algorithm.
-void bsort(int list[], int length) {
- for (int i=0; i<n; i++) {
- for (int j=0; j<n-1; j++) {
- if (list[j]>list[j+1]) {
- //This is so the bubble can happen
- swap(list[j], list[j+1]);
- }
- }
- }
-}
-
-//This function simply swaps two values so the bubbling can commence
-void swap (int &a, int &b) {
- int temp;
- temp=a; a=b; b=temp;
-}
-
-//This function prints the array at 5 per line.
-void printArray (int list[], int length) {
- int k=0;
- for (int j=0; j<(n/10)*2; j++) {
- for (int i=0; i<5; i++) {
- cout << list[k] << " ";
- k++;
- }
- cout << endl;
- }
-}
+#include <iostream>
+using namespace std;
+
+void bsort(int list[], int length);
+void swap (int &a, int &b);
+void printArray (int list[], int length);
+void makeArray (int list[], int length);
+const int n=50;
+
+int main () {
+int list[n];
+makeArray(list,n);
+bsort(list, n);
+printArray(list, n);
+}
+
+//This function makes the array used for the rest of the program.
+void makeArray(int list[], int length) {
+ int temp=100;
+ for (int i=0; i<n; i++) {
+ list[i]=temp;
+ temp--;
+ }
+}
+
+//This function is the algorithm.
+void bsort(int list[], int length) {
+ for (int i=0; i<n; i++) {
+ for (int j=0; j<n-1; j++) {
+ if (list[j]>list[j+1]) {
+ //This is so the bubble can happen
+ swap(list[j], list[j+1]);
+ }
+ }
+ }
+}
+
+//This function simply swaps two values so the bubbling can commence
+void swap (int &a, int &b) {
+ int temp;
+ temp=a; a=b; b=temp;
+}
+
+//This function prints the array at 5 per line.
+void printArray (int list[], int length) {
+ int k=0;
+ for (int j=0; j<(n/10)*2; j++) {
+ for (int i=0; i<5; i++) {
+ cout << list[k] << " ";
+ k++;
+ }
+ cout << endl;
+ }
+}
diff --git a/ee1301/wk5/lab4/swappy.cpp b/ee1301/wk5/lab4/swappy.cpp
index b697ff5..a2810a6 100644
--- a/ee1301/wk5/lab4/swappy.cpp
+++ b/ee1301/wk5/lab4/swappy.cpp
@@ -1,24 +1,24 @@
-#include <iostream>
-using namespace std;
-
-void swap (int &a, int &b);
-int main(int argc, char* argv[]) {
- if (argc !=2) {
- cout << "Incorrect number of arguments! USAGE:" << endl
- << " swappy <string>" << endl;;
- return 3;
- }
- int i;
- for (i=0; i<10000; i++) {
- if (argv[1][i]=='\0') {
- break;
- }
- }
- swap(argv[1][0],argv[1][(i-1)]);
- cout << argv[1] << endl;
-
-}
-void swap (char &a, char &b) {
- char temp;
- temp=a; a=b; b=temp;
-}
+#include <iostream>
+using namespace std;
+
+void swap (int &a, int &b);
+int main(int argc, char* argv[]) {
+ if (argc !=2) {
+ cout << "Incorrect number of arguments! USAGE:" << endl
+ << " swappy <string>" << endl;;
+ return 3;
+ }
+ int i;
+ for (i=0; i<10000; i++) {
+ if (argv[1][i]=='\0') {
+ break;
+ }
+ }
+ swap(argv[1][0],argv[1][(i-1)]);
+ cout << argv[1] << endl;
+
+}
+void swap (char &a, char &b) {
+ char temp;
+ temp=a; a=b; b=temp;
+}
diff --git a/ee1301/wk5/lab4/time1.cpp b/ee1301/wk5/lab4/time1.cpp
index 75c0aab..0bb9eaa 100644
--- a/ee1301/wk5/lab4/time1.cpp
+++ b/ee1301/wk5/lab4/time1.cpp
@@ -1,24 +1,24 @@
-#include <iostream>
-#include <string>
-
-using namespace std;
-
-int timeToMinutes(int hours, int mins);
-int main() {
- int hours, minutes;
- char colon, cont = 'y';
- do {
- cout << "Enter a time duration (hh:mm) ";
- cin >> hours >> colon >> minutes;
- cout << "Total minutes: " << timeToMinutes(hours, minutes) << endl;
- do {
- cout << "Continue? (y/n):";
- cin >> cont;
- } while ( !( !(cont!='n') != !(cont!='y') ) );
- //The last logic is a XNOR gate of magic, please do not touch
- } while ((cont=='y'));
-}
-
-int timeToMinutes(int hours, int mins) {
- return mins + hours * 60;
-}
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+int timeToMinutes(int hours, int mins);
+int main() {
+ int hours, minutes;
+ char colon, cont = 'y';
+ do {
+ cout << "Enter a time duration (hh:mm) ";
+ cin >> hours >> colon >> minutes;
+ cout << "Total minutes: " << timeToMinutes(hours, minutes) << endl;
+ do {
+ cout << "Continue? (y/n):";
+ cin >> cont;
+ } while ( !( !(cont!='n') != !(cont!='y') ) );
+ //The last logic is a XNOR gate of magic, please do not touch
+ } while ((cont=='y'));
+}
+
+int timeToMinutes(int hours, int mins) {
+ return mins + hours * 60;
+}
diff --git a/ee1301/wk5/lab4/time2.cpp b/ee1301/wk5/lab4/time2.cpp
index e79cd82..0126e23 100644
--- a/ee1301/wk5/lab4/time2.cpp
+++ b/ee1301/wk5/lab4/time2.cpp
@@ -1,36 +1,36 @@
-#include <iostream>
-#include <string>
-
-using namespace std;
-
-void minutesToTime(int minute_value, int& hours, int& mins);
-int main() {
- int hours, mins;
- char cont = 'y';
- do {
- cout << "Enter a number of minutes: ";
- cin >> mins;
- minutesToTime(mins, hours, mins);
- cout << hours << ":";
- if (mins<10) {
- cout << '0' << mins << endl;
- } else {
- cout << mins << endl;
- }
- do {
- cout << "Continue? (y/n):";
- cin >> cont;
- } while ( !( !(cont!='n') != !(cont!='y') ) );
- //The last logic is a XNOR gate of magic, touching will disturb the magic
- } while ((cont=='y'));
-}
-
-void minutesToTime(int minute_value, int& hours, int& mins) {
- mins=minute_value%60;
- hours=0;
- for (int i=minute_value/60; i>0; i--) {
- hours++;
- if (hours>12)
- hours=1;
- }
-}
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+void minutesToTime(int minute_value, int& hours, int& mins);
+int main() {
+ int hours, mins;
+ char cont = 'y';
+ do {
+ cout << "Enter a number of minutes: ";
+ cin >> mins;
+ minutesToTime(mins, hours, mins);
+ cout << hours << ":";
+ if (mins<10) {
+ cout << '0' << mins << endl;
+ } else {
+ cout << mins << endl;
+ }
+ do {
+ cout << "Continue? (y/n):";
+ cin >> cont;
+ } while ( !( !(cont!='n') != !(cont!='y') ) );
+ //The last logic is a XNOR gate of magic, touching will disturb the magic
+ } while ((cont=='y'));
+}
+
+void minutesToTime(int minute_value, int& hours, int& mins) {
+ mins=minute_value%60;
+ hours=0;
+ for (int i=minute_value/60; i>0; i--) {
+ hours++;
+ if (hours>12)
+ hours=1;
+ }
+}