minerva
feat: completed assignment 5
SignedGitea couldn't verify the signature.
Showing 6 changed files with 1241 additions and 0 deletions.
README.mdadded
+3 −0
@@ -0,0 +1,3 @@1# minerva23classes, homework, notebooks, small tests and projects
cis_27/assignment_5/assignment5added
+0 −0
Binary file, not shown.
cis_27/assignment_5/common_alg.cppadded
+184 −0
@@ -0,0 +1,184 @@1#include "common_alg.h"23#include <fstream>4#include <iostream>56// constructor: fill array from numbers.txt7Common_Alg::Common_Alg() {8 size = 1000;9 numbers = new int[size]();1011 int i = 0;12 std::ifstream file("numbers.txt");1314 if (file.is_open()) {15 int num;1617 while (i < size && file >> num) {18 numbers[i] = num;19 i++;20 }21 file.close();22 } else {23 std::cout << "could not open numbers.txt\n";24 }25}2627// destructor: free the memory made with new28Common_Alg::~Common_Alg() {29 delete[] numbers;30 numbers = nullptr;31 std::cout << "destructor: dynamic array freed\n";32}3334// method 1: output all values35void Common_Alg::output_all_values() {3637 for (int i = 0; i < size; i++) {38 std::cout << numbers[i] << "\n";39 }40}4142// method 2: output sum of all values43void Common_Alg::output_sum_all_values() {44 int sum = 0;4546 for (int i = 0; i < size; i++) {47 sum = sum + numbers[i];48 }49 std::cout << sum << "\n";50}5152// method 3: output all odd numbers53void Common_Alg::output_all_odd() {5455 for (int i = 0; i < size; i++) {56 if (numbers[i] % 2 != 0) {57 std::cout << numbers[i] << "\n";58 }59 }60}6162// method 4: output all even numbers63void Common_Alg::output_all_even() {64 for (int i = 0; i < size; i++) {65 if (numbers[i] % 2 == 0) {66 std::cout << numbers[i] << "\n";67 }68 }69}7071// method 5: binary search for a value72void Common_Alg::binary_search(int num) {73 74 // data must be in sorted first75 bubble_sort();7677 int low = 0;78 int high = size - 1;79 int position = -1;8081 while (low <= high) {82 int mid = (low + high) / 2;8384 if (numbers[mid] == num) {85 position = mid;86 break;87 } else if (numbers[mid] > num) {88 low = mid + 1;89 } else {90 high = mid - 1;91 }92 }9394 if (position != -1) {95 std::cout << "Number found in array at position " << position << "\n";96 } else {97 std::cout << "Number not found in array.\n";98 }99}100101// method 6: output middle value102void Common_Alg::output_middle_value() {103 std::cout << numbers[size / 2] << "\n";104}105106// method 7: output first value in the array107void Common_Alg::output_first_value() {108 std::cout << "first value in array: " << numbers[0] << "\n";109}110111// method 8: output last value in the array112void Common_Alg::output_last_value() {113 std::cout << "last value in array: " << numbers[size - 1] << "\n";114}115116// method 9: output highest value, and its location in the array117void Common_Alg::output_highest_value_loc() {118 int highest_val = 0;119 int location = 0;120121 for (int i = 0; i < size; i++) {122 if (numbers[i] > highest_val) {123 highest_val = numbers[i];124 location = i;125 }126 }127 std::cout << "highest value: " << highest_val << " , location: " << location128 << "\n";129}130131// method 10: output lowest value and its location in the array132void Common_Alg::output_lowest_value_loc() {133 int lowest_val = 1000;134 int location = 0;135136 for (int i = 0; i < size; i++) {137 if (numbers[i] < lowest_val) {138 lowest_val = numbers[i];139 location = i;140 }141 }142 std::cout << "lowest value: " << lowest_val << " , location: " << location143 << "\n";144}145146// bubble sort, largest to smallest (used by method 5 and method 11)147void Common_Alg::bubble_sort() {148149 for (int i = 0; i < size - 1; i++) {150151 for (int j = 0; j < size - 1 - i; j++) {152 int hold = 0;153154 if (numbers[j] < numbers[j + 1]) {155 hold = numbers[j];156 numbers[j] = numbers[j + 1];157 numbers[j + 1] = hold;158 }159 }160 }161}162163// method 11: bubble sort, then output the sorted values164void Common_Alg::output_bubble_sort() {165 bubble_sort();166167 for (int i = 0; i < size; i++) {168 std::cout << numbers[i] << "\n";169 }170}171172// method 12: output mean average173void Common_Alg::mean_average() {174 int sum = 0;175 int average = 0;176177 for (int i = 0; i < size; i++) {178 sum = sum + numbers[i];179 }180181 average = sum / size;182183 std::cout << "mean_average: " << average << "\n";184}
cis_27/assignment_5/common_alg.hadded
+30 −0
@@ -0,0 +1,30 @@123class Common_Alg {4private:5 int *numbers;6 int size;78public:9 // constructor: fill array from numbers.txt10 Common_Alg();1112 // destructor: free dynamic array13 ~Common_Alg();1415 void output_all_values();16 void output_sum_all_values();17 void output_all_odd();18 void output_all_even();19 void binary_search(int num);20 void output_middle_value();21 void output_first_value();22 void output_last_value();23 void output_highest_value_loc();24 void output_lowest_value_loc();25 void bubble_sort();26 void output_bubble_sort();27 void mean_average();28};2930
cis_27/assignment_5/main.cppadded
+24 −0
@@ -0,0 +1,24 @@12#include "common_alg.h"34int main() {56 Common_Alg assignment5;78 // assignment5.output_all_values();9 // assignment5.output_sum_all_values();10 // assignment5.output_all_odd();11 // assignment5.output_all_even();12 assignment5.binary_search(379);13 // assignment5.output_middle_value();14 // assignment5.output_first_value();15 // assignment5.output_last_value();16 // assignment5.output_highest_value_loc();17 // assignment5.output_lowest_value_loc();18 // assignment5.output_bubble_sort();19 // assignment5.mean_average();2021 return 0;22}2324// g++ -std=c++17 -o assignment5 main.cpp common_alg.cpp
cis_27/assignment_5/numbers.txtadded
+1000 −0
@@ -0,0 +1,1000 @@1855263934564899513562474608898953910321141912897138351430615159164421729186301946120931216552291823406249257732645027472883298913031531942327563311734146354583649537550388523958540287411154225543925442194578446633476234856249656502645127352547538854106555655644657587588385967160986140362138638826487655766619677386868369335707471727292739347440675827625177506789237983980355819998269383210845848557586645871348834089377905999112092884937679415595785961649751298946998551002781016981029191035441048101059901061311076131085911096491108791119221126831136211146631159601168291176641189671199081203261214411222941233211243721251241269111276611288561291401302813191613249133911134491356551364871378071381161399614041014122514296614336914431514597014656314710614851914927015091915135915290615366015476215535156921157691584751593321604231612741628316370916487216547516689616795916897916990417062017167917282317313417443417528917616817734817818917980118040518181418250418375118456318553818690918786718850189495190701918791921941936711943741953019621019779619833019994620063520199820287820340820481220588920610620774120874120913921050521177621230213475214728215358216817217992218700219701220160221453222529223563224531225364226387227370228352298982308312314882327892336912349702352923637723726423821023968224041924135242585243755244421245330246240247812248465249300250452519392525712537062548352552222567502579412584702593922606062614926297826323526497265767266562674192687582694682704082714352724662732327494227583527650927712278342279817280202281967282906283678284378285336286156287748288924289590290786291534292803293418294303295773296932297527298146299681300178301743302642303409304893056193062253071843082553095493102753112023121033137813145973159573168273176903182753198293205333212993221933232493246043257163265133279313289893292703306613317913325153331043341133356133366503379923384263394563405203415923427923432553445753455733465553471903486603492343504083511293522713538743542063553313567603579135823635920136044436161736288436378236458936516336690536727636832369284370904371514372828373133748633756033767433771123782133794183808083813193823383613384910385511386806387530388210389716390431391839260239380739468839526039629639725839854739985400994013544021403837404363405652406676407358408794409202410318411192412969413596414862415249416410417910418614198234203174215614228674237424245284256344268842791342842142948843076643154843278843312043465143549043670643748443842743969244014444190244217044385444414644526144663344788244895844993645023645134245276845330345435345581545627045788445868645961946030846177046280463199464364651194663814679846830746974347086547148147261447372247486447513947647447755347872147976348090448125548218948317648472148566148635648741148880648989549069549159149244549319849477849543249626649776849861749916850077550120850265050322050482750552506836507465508688509215510595511590512748513202514684515305516906517375182425193745205735213522164523171524195252745267225273535283475294825302505316735323685332125347125352315365885376905383855399945404315411155422055439725442195451125464065475575487195494735504435512705525315531295544955561455664655792155859955934256060456112256297856346056446956542356699356765256856356989957059571994572506573657491257557257632057728257886957929358025358192358260658322258496958562586648587119588413589778590989591640592711593951594238595662596895978095986775996896006226012602903603914604686053156064646078860815360918361021861125361236961359761496261553261694061740761840561952562027621191622394623676624697625916626426277806284256296786307016311456328206338263459563591563692363767763878463942264013164119964273364390864437564583264627564782064823064970650415651189652416538276549646556756567876579016586756597626602066615776629296638006643456651866666146676426682886694186706116716846727276736516742467525867629667717567842367924268095268118968247968379868479468534168687168766068821689682690248691752692597693728694522695214696705697150698535699757004407015437028097036977044507058767065570787570852770925671089571136371287571363071432171554271641971750171827671977372063572196672238672395272473572570272631572714172878672939373019473124273283073330773433273522073612173738273879973959074085574182274292574385774416174569974649474781574842749374750337751759752858753847754863755186756275730775851475946176012676141476229176389776462276549376636876765176880576989977091377147477268277385377477977525877622777721177888077930878022978184878298478359784232785216786436787371788944789817790269791882792733793332794273795732796843797744798133799606800900801588802540803302804204805887806430807554808561809404810707811242812861813511814272815340816752817339818481953482092821900822346823997824507825227826672827131828930829640830604831549832654833296834202835859836360837879838628839464840205841758429928436168446648451508469384737384839584998385058985114885253285312985471385521785676285736685878985925486060386117286262786392786458386589986656186775286880586964587058887112787223873416874451875804876662877998782198798318804528811028822368833318842608856038866018877588886988897098907258911538928089375189453189598789676897600898845899856900789901576902299903230904499054189066519071189086279092839105019114289125309136809145999151939169039176319183939193792071992115592228492310192490192526492678592759928544929893042493110009328089332009348319356419365093785193856393943794061294113894285294383494493394548294616494754394881994951495013595185495214695332995476895547895697895769595873795975096037996146296262696383896435965601966435967104968884969685970611971483972618973297429975654976497793397846597979298054498129821089832979845129852139866589876249881349897789907489915999921279938499944609954479961539971779981809994911000379