minerva
feat: add assignment 3 and 6
SignedGitea couldn't verify the signature.
Showing 6 changed files with 673 additions and 0 deletions.
cis_27/assignment_3/task_1/task_1.cppadded
+276 −0
@@ -0,0 +1,276 @@1#include <cstdlib>2#include <ctime>3#include <iostream>45const int SIZE = 1000;67class Common_Alg {8private:9 int numbers[SIZE];1011public:12 // constructor: seed rand, fill array with 1000 random ints from 1 to 100013 Common_Alg() {14 srand(time(0));1516 for (int i = 0; i < SIZE; i++) {17 numbers[i] = rand() % 1000 + 1;18 }19 }2021 // method 1: output all values22 void output_all_values() {2324 for (int i = 0; i < SIZE; i++) {25 std::cout << numbers[i] << "\n";26 }27 }2829 // method 2: output sum of all values30 void output_sum_all_values() {31 int sum = 0;3233 for (int i = 0; i < SIZE; i++) {34 sum = sum + numbers[i];35 }36 std::cout << sum << "\n";37 }3839 // method 3: output all odd numbers40 void output_all_odd() {4142 for (int i = 0; i < SIZE; i++) {43 if (numbers[i] % 2 != 0) {44 std::cout << numbers[i] << "\n";45 }46 }47 }4849 // method 4: output all even numbers50 void output_all_even() {51 for (int i = 0; i < SIZE; i++) {52 if (numbers[i] % 2 == 0) {53 std::cout << numbers[i] << "\n";54 }55 }56 }5758 // method 5: linear search, output every position the value is found at59 void linear_search(int num) {60 bool found = false;6162 for (int i = 0; i < SIZE; i++) {63 if (num == numbers[i]) {64 std::cout << "Number found in array at position " << i << "\n";65 found = true;66 }67 }6869 if (!found) {70 std::cout << "Number not found in array.\n";71 }72 }7374 // method 6: output the two middle values (location 499 and 500)75 void output_middle_values() {76 std::cout << "middle value at location " << SIZE / 2 - 1 << ": "77 << numbers[SIZE / 2 - 1] << "\n";78 std::cout << "middle value at location " << SIZE / 2 << ": "79 << numbers[SIZE / 2] << "\n";80 }8182 // method 7: output first value in the array83 void output_first_value() {84 std::cout << "first value in array: " << numbers[0] << "\n";85 }8687 // method 8: output last value in the array88 void output_last_value() {89 std::cout << "last value in array: " << numbers[SIZE - 1] << "\n";90 }9192 // method 9: output highest value, and its location in the array93 void output_highest_value_loc() {94 int highest_val = 0;95 int location = 0;9697 for (int i = 0; i < SIZE; i++) {98 if (numbers[i] > highest_val) {99 highest_val = numbers[i];100 location = i;101 }102 }103 std::cout << "highest value: " << highest_val << " , location: " << location104 << "\n";105 }106107 // method 10: output lowest value and its location in the array108 void output_lowest_value_loc() {109 int lowest_val = 1000;110 int location = 0;111112 for (int i = 0; i < SIZE; i++) {113 if (numbers[i] < lowest_val) {114 lowest_val = numbers[i];115 location = i;116 }117 }118 std::cout << "lowest value: " << lowest_val << " , location: " << location119 << "\n";120 }121122 // bubble sort, largest to smallest (used by method 11 and method 13)123 void bubble_sort(int arr[]) {124125 for (int i = 0; i < SIZE - 1; i++) {126127 for (int j = 0; j < SIZE - 1 - i; j++) {128 int hold = 0;129130 if (arr[j] < arr[j + 1]) {131 hold = arr[j];132 arr[j] = arr[j + 1];133 arr[j + 1] = hold;134 }135 }136 }137 }138139 // method 11: bubble sort the array, then output the sorted values140 void output_bubble_sort() {141 bubble_sort(numbers);142143 for (int i = 0; i < SIZE; i++) {144 std::cout << numbers[i] << "\n";145 }146 }147148 // method 12: output mean average as a decimal149 void mean_average() {150 int sum = 0;151 double average = 0;152153 for (int i = 0; i < SIZE; i++) {154 sum = sum + numbers[i];155 }156157 average = (double)sum / SIZE;158159 std::cout << "mean_average: " << average << "\n";160 }161162 // method 13: median, sort a copy and average the two middle values163 void median() {164 int copy[SIZE];165166 for (int i = 0; i < SIZE; i++) {167 copy[i] = numbers[i];168 }169 bubble_sort(copy);170171 double median_val = (copy[SIZE / 2 - 1] + copy[SIZE / 2]) / 2.0;172173 std::cout << "median: " << median_val << "\n";174 }175};176177int main() {178179 Common_Alg task1;180 int choice = -1;181182 while (choice != 0) {183 std::cout << "\n----- menu -----\n";184 std::cout << "1. output all values\n";185 std::cout << "2. output sum of all values\n";186 std::cout << "3. output all odd numbers\n";187 std::cout << "4. output all even numbers\n";188 std::cout << "5. linear search\n";189 std::cout << "6. output middle values\n";190 std::cout << "7. output first value\n";191 std::cout << "8. output last value\n";192 std::cout << "9. output highest value and location\n";193 std::cout << "10. output lowest value and location\n";194 std::cout << "11. bubble sort\n";195 std::cout << "12. output mean average\n";196 std::cout << "13. output median\n";197 std::cout << "0. exit\n";198 std::cout << "choice: ";199200 // not a number: clear the error and throw away the bad input201 if (!(std::cin >> choice)) {202 if (std::cin.eof()) {203 break;204 }205 std::cin.clear();206 std::cin.ignore(10000, '\n');207 std::cout << "invalid choice, enter a number\n";208 choice = -1;209 continue;210 }211212 // remove the leftover newline213 std::cin.ignore(10000, '\n');214215 switch (choice) {216 case 1:217 task1.output_all_values();218 break;219 case 2:220 task1.output_sum_all_values();221 break;222 case 3:223 task1.output_all_odd();224 break;225 case 4:226 task1.output_all_even();227 break;228 case 5: {229 int num;230 std::cout << "enter a search value: ";231232 if (!(std::cin >> num)) {233 std::cin.clear();234 std::cin.ignore(10000, '\n');235 std::cout << "invalid value, enter a number\n";236 break;237 }238 std::cin.ignore(10000, '\n');239 task1.linear_search(num);240 break;241 }242 case 6:243 task1.output_middle_values();244 break;245 case 7:246 task1.output_first_value();247 break;248 case 8:249 task1.output_last_value();250 break;251 case 9:252 task1.output_highest_value_loc();253 break;254 case 10:255 task1.output_lowest_value_loc();256 break;257 case 11:258 task1.output_bubble_sort();259 break;260 case 12:261 task1.mean_average();262 break;263 case 13:264 task1.median();265 break;266 case 0:267 break;268 default:269 std::cout << "invalid choice\n";270 }271 }272273 return 0;274}275276// g++ -std=c++17 -Wall -o task1 task_1.cpp
cis_27/assignment_3/task_2/task_2.pyadded
+202 −0
@@ -0,0 +1,202 @@1import random234class Common_Alg:5 # constructor: fill the list with 1000 random ints from 1 to 10006 def __init__(self):7 self.numbers = []89 for i in range(1000):10 self.numbers.append(random.randint(1, 1000))1112 # method 1: output all values13 def output_all_values(self):1415 for i in range(len(self.numbers)):16 print(self.numbers[i])1718 # method 2: output sum of all values19 def output_sum_all_values(self):20 sum = 02122 for i in range(len(self.numbers)):23 sum = sum + self.numbers[i]2425 print(sum)2627 # method 3: output all odd numbers28 def output_all_odd(self):2930 for i in range(len(self.numbers)):31 if self.numbers[i] % 2 != 0:32 print(self.numbers[i])3334 # method 4: output all even numbers35 def output_all_even(self):36 for i in range(len(self.numbers)):37 if self.numbers[i] % 2 == 0:38 print(self.numbers[i])3940 # method 5: linear search, output every position the value is found at41 def linear_search(self, num):42 found = False4344 for i in range(len(self.numbers)):45 if num == self.numbers[i]:46 print("Number found in array at position " + str(i))47 found = True4849 if not found:50 print("Number not found in array.")5152 # method 6: output the two middle values (location 499 and 500)53 def output_middle_values(self):54 middle = len(self.numbers) // 25556 print("middle value at location " + str(middle - 1) + ": " + str(self.numbers[middle - 1]))57 print("middle value at location " + str(middle) + ": " + str(self.numbers[middle]))5859 # method 7: output first value in the array60 def output_first_value(self):61 print("first value in array: " + str(self.numbers[0]))6263 # method 8: output last value in the array64 def output_last_value(self):65 print("last value in array: " + str(self.numbers[len(self.numbers) - 1]))6667 # method 9: output highest value, and its location in the array68 def output_highest_value_loc(self):69 highest_val = 070 location = 07172 for i in range(len(self.numbers)):73 if self.numbers[i] > highest_val:74 highest_val = self.numbers[i]75 location = i7677 print("highest value: " + str(highest_val) + " , location: " + str(location))7879 # method 10: output lowest value and its location in the array80 def output_lowest_value_loc(self):81 lowest_val = 100082 location = 08384 for i in range(len(self.numbers)):85 if self.numbers[i] < lowest_val:86 lowest_val = self.numbers[i]87 location = i8889 print("lowest value: " + str(lowest_val) + " , location: " + str(location))9091 # bubble sort, largest to smallest (used by method 11 and method 13)92 def bubble_sort(self, arr):9394 for i in range(len(arr) - 1):95 for j in range(len(arr) - 1 - i):96 hold = 09798 if arr[j] < arr[j + 1]:99 hold = arr[j]100 arr[j] = arr[j + 1]101 arr[j + 1] = hold102103 # method 11: bubble sort the list, then output the sorted values104 def output_bubble_sort(self):105 self.bubble_sort(self.numbers)106107 for i in range(len(self.numbers)):108 print(self.numbers[i])109110 # method 12: output mean average as a decimal111 def mean_average(self):112 sum = 0113 average = 0114115 for i in range(len(self.numbers)):116 sum = sum + self.numbers[i]117118 average = sum / len(self.numbers)119120 print("mean_average: " + str(average))121122 # method 13: median, sort a copy and average the two middle values123 def median(self):124 copy = []125126 for i in range(len(self.numbers)):127 copy.append(self.numbers[i])128129 self.bubble_sort(copy)130131 middle = len(copy) // 2132 median_val = (copy[middle - 1] + copy[middle]) / 2133134 print("median: " + str(median_val))135136137def main():138139 task2 = Common_Alg()140 choice = -1141142 while choice != 0:143 print("\n----- menu -----")144 print("1. output all values")145 print("2. output sum of all values")146 print("3. output all odd numbers")147 print("4. output all even numbers")148 print("5. linear search")149 print("6. output middle values")150 print("7. output first value")151 print("8. output last value")152 print("9. output highest value and location")153 print("10. output lowest value and location")154 print("11. bubble sort")155 print("12. output mean average")156 print("13. output median")157 print("0. exit")158159 # not a number: ask again160 try:161 choice = int(input("choice: "))162 except ValueError:163 print("invalid choice, enter a number")164 continue165166 if choice == 1:167 task2.output_all_values()168 elif choice == 2:169 task2.output_sum_all_values()170 elif choice == 3:171 task2.output_all_odd()172 elif choice == 4:173 task2.output_all_even()174 elif choice == 5:175 try:176 num = int(input("enter a search value: "))177 except ValueError:178 print("invalid value, enter a number")179 continue180 task2.linear_search(num)181 elif choice == 6:182 task2.output_middle_values()183 elif choice == 7:184 task2.output_first_value()185 elif choice == 8:186 task2.output_last_value()187 elif choice == 9:188 task2.output_highest_value_loc()189 elif choice == 10:190 task2.output_lowest_value_loc()191 elif choice == 11:192 task2.output_bubble_sort()193 elif choice == 12:194 task2.mean_average()195 elif choice == 13:196 task2.median()197 elif choice != 0:198 print("invalid choice")199200201if __name__ == "__main__":202 main()
cis_27/assignment_6/char_increment.cppadded
+15 −0
@@ -0,0 +1,15 @@1#include <iostream>23int main() {4 char char_ex = 'A';56 // increment 30 times, print the char and its ascii value each time7 for (int i = 0; i < 30; i++) {8 char_ex++;9 std::cout << "char: " << char_ex << " , ascii: " << (int)char_ex << "\n";10 }1112 return 0;13}1415// g++ -std=c++17 -Wall -o char_increment char_increment.cpp
cis_27/assignment_6/main.cppadded
+0 −0
No content changes.
cis_27/assignment_6/my_stringadded
+0 −0
Binary file, not shown.
cis_27/assignment_6/my_string.cppadded
+180 −0
@@ -0,0 +1,180 @@1#include <iostream>23const int MAX_SIZE = 256;45class My_String {6private:7 char input_buffer[MAX_SIZE];8 char original[MAX_SIZE];910public:11 // constructor: copy the input char by char into input_buffer 12 My_String(const char input[]) {13 int i = 0;1415 while (i < MAX_SIZE - 1 && input[i] != '\0') {16 input_buffer[i] = input[i];17 original[i] = input[i];18 i++;19 }20 input_buffer[i] = '\0';21 original[i] = '\0';22 }2324 // method 1: count characters until '\0'25 int length() {26 int count = 0;2728 for (;;) {29 if (input_buffer[count] == '\0') {30 break;31 }32 count++;33 }34 return count;35 }3637 // method 2: check if the string is all letters38 bool is_alpha() {39 bool all_letters = true;4041 for (int i = 0; input_buffer[i] != '\0'; i++) {42 char c = input_buffer[i];4344 // A-Z is 65-90, 45 // a-z is 97-12246 if (!((c >= 65 && c <= 90) || (c >= 97 && c <= 122))) {47 std::cout << "not a letter at index " << i << ": '" << c << "'\n";48 all_letters = false;49 }50 }5152 if (all_letters) {53 std::cout << "string is all letters\n";54 } else {55 std::cout << "string is not all letters\n";56 }57 return all_letters;58 }5960 // method 3: convert a-z to A-Z by subtracting 3261 void to_upper() {62 for (int i = 0; input_buffer[i] != '\0'; i++) {63 if (input_buffer[i] >= 97 && input_buffer[i] <= 122) {64 input_buffer[i] = input_buffer[i] - 32;65 }66 }67 std::cout << input_buffer << "\n";68 }6970 // method 4: convert A-Z to a-z by adding 3271 void to_lower() {72 for (int i = 0; input_buffer[i] != '\0'; i++) {73 if (input_buffer[i] >= 65 && input_buffer[i] <= 90) {74 input_buffer[i] = input_buffer[i] + 32;75 }76 }77 std::cout << input_buffer << "\n";78 }7980 // method 5: put c where '\0' was, then add a new '\0' after it81 void push_back(char c) {82 int end = length();8384 // last slot is saved for '\0'85 if (end >= MAX_SIZE - 1) {86 std::cout << "buffer is full, can't add " << c << "\n";87 return;88 }8990 input_buffer[end] = c;91 input_buffer[end + 1] = '\0';92 std::cout << input_buffer << "\n";93 }9495 // method 6: output original string96 void output_original() { std::cout << "original: " << original << "\n"; }9798 // method 7: output current string99 void output_current() { std::cout << "current: " << input_buffer << "\n"; }100};101102int main() {103 char input[MAX_SIZE];104105 std::cout << "enter a string: ";106 std::cin.getline(input, MAX_SIZE);107108 // input was longer than the buffer: keep what fit, drop the rest109 if (std::cin.fail()) {110 std::cin.clear();111 std::cin.ignore(10000, '\n');112 }113114 My_String my_string(input);115 my_string.output_original();116117 int choice = -1;118119 while (choice != 0) {120 std::cout << "\n----- menu -----\n";121 std::cout << "1. length\n";122 std::cout << "2. is alpha\n";123 std::cout << "3. to upper\n";124 std::cout << "4. to lower\n";125 std::cout << "5. push back\n";126 std::cout << "6. show original and current\n";127 std::cout << "0. exit\n";128 std::cout << "choice: ";129130 // not a number: clear the error and throw away the bad input131 if (!(std::cin >> choice)) {132 if (std::cin.eof()) {133 break;134 }135 std::cin.clear();136 std::cin.ignore(10000, '\n');137 std::cout << "invalid choice, enter a number\n";138 choice = -1;139 continue;140 }141142 // remove the leftover newline143 std::cin.ignore(10000, '\n');144145 switch (choice) {146 case 1:147 std::cout << "length: " << my_string.length() << "\n";148 break;149 case 2:150 my_string.is_alpha();151 break;152 case 3:153 my_string.to_upper();154 break;155 case 4:156 my_string.to_lower();157 break;158 case 5: {159 char c;160 std::cout << "enter one character: ";161 std::cin >> c;162 std::cin.ignore(10000, '\n');163 my_string.push_back(c);164 break;165 }166 case 6:167 my_string.output_original();168 my_string.output_current();169 break;170 case 0:171 break;172 default:173 std::cout << "invalid choice\n";174 }175 }176177 return 0;178}179180// g++ -std=c++17 -Wall -o my_string my_string.cpp