Something went wrong. Try again.
Personal learning sandbox
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277#include <cstdlib>#include <ctime>#include <iostream>
const int SIZE = 1000;
class Common_Alg {private: int numbers[SIZE];
public: // constructor: seed rand, fill array with 1000 random ints from 1 to 1000 Common_Alg() { srand(time(0));
for (int i = 0; i < SIZE; i++) { numbers[i] = rand() % 1000 + 1; } }
// method 1: output all values void output_all_values() {
for (int i = 0; i < SIZE; i++) { std::cout << numbers[i] << "\n"; } }
// method 2: output sum of all values void output_sum_all_values() { int sum = 0;
for (int i = 0; i < SIZE; i++) { sum = sum + numbers[i]; } std::cout << sum << "\n"; }
// method 3: output all odd numbers void output_all_odd() {
for (int i = 0; i < SIZE; i++) { if (numbers[i] % 2 != 0) { std::cout << numbers[i] << "\n"; } } }
// method 4: output all even numbers void output_all_even() { for (int i = 0; i < SIZE; i++) { if (numbers[i] % 2 == 0) { std::cout << numbers[i] << "\n"; } } }
// method 5: linear search, output every position the value is found at void linear_search(int num) { bool found = false;
for (int i = 0; i < SIZE; i++) { if (num == numbers[i]) { std::cout << "Number found in array at position " << i << "\n"; found = true; } }
if (!found) { std::cout << "Number not found in array.\n"; } }
// method 6: output the two middle values (location 499 and 500) void output_middle_values() { std::cout << "middle value at location " << SIZE / 2 - 1 << ": " << numbers[SIZE / 2 - 1] << "\n"; std::cout << "middle value at location " << SIZE / 2 << ": " << numbers[SIZE / 2] << "\n"; }
// method 7: output first value in the array void output_first_value() { std::cout << "first value in array: " << numbers[0] << "\n"; }
// method 8: output last value in the array void output_last_value() { std::cout << "last value in array: " << numbers[SIZE - 1] << "\n"; }
// method 9: output highest value, and its location in the array void output_highest_value_loc() { int highest_val = 0; int location = 0;
for (int i = 0; i < SIZE; i++) { if (numbers[i] > highest_val) { highest_val = numbers[i]; location = i; } } std::cout << "highest value: " << highest_val << " , location: " << location << "\n"; }
// method 10: output lowest value and its location in the array void output_lowest_value_loc() { int lowest_val = 1000; int location = 0;
for (int i = 0; i < SIZE; i++) { if (numbers[i] < lowest_val) { lowest_val = numbers[i]; location = i; } } std::cout << "lowest value: " << lowest_val << " , location: " << location << "\n"; }
// bubble sort, largest to smallest (used by method 11 and method 13) void bubble_sort(int arr[]) {
for (int i = 0; i < SIZE - 1; i++) {
for (int j = 0; j < SIZE - 1 - i; j++) { int hold = 0;
if (arr[j] < arr[j + 1]) { hold = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = hold; } } } }
// method 11: bubble sort the array, then output the sorted values void output_bubble_sort() { bubble_sort(numbers);
for (int i = 0; i < SIZE; i++) { std::cout << numbers[i] << "\n"; } }
// method 12: output mean average as a decimal void mean_average() { int sum = 0; double average = 0;
for (int i = 0; i < SIZE; i++) { sum = sum + numbers[i]; }
average = (double)sum / SIZE;
std::cout << "mean_average: " << average << "\n"; }
// method 13: median, sort a copy and average the two middle values void median() { int copy[SIZE];
for (int i = 0; i < SIZE; i++) { copy[i] = numbers[i]; } bubble_sort(copy);
double median_val = (copy[SIZE / 2 - 1] + copy[SIZE / 2]) / 2.0;
std::cout << "median: " << median_val << "\n"; }};
int main() {
Common_Alg task1; int choice = -1;
while (choice != 0) { std::cout << "\n----- menu -----\n"; std::cout << "1. output all values\n"; std::cout << "2. output sum of all values\n"; std::cout << "3. output all odd numbers\n"; std::cout << "4. output all even numbers\n"; std::cout << "5. linear search\n"; std::cout << "6. output middle values\n"; std::cout << "7. output first value\n"; std::cout << "8. output last value\n"; std::cout << "9. output highest value and location\n"; std::cout << "10. output lowest value and location\n"; std::cout << "11. bubble sort\n"; std::cout << "12. output mean average\n"; std::cout << "13. output median\n"; std::cout << "0. exit\n"; std::cout << "choice: ";
// not a number: clear the error and throw away the bad input if (!(std::cin >> choice)) { if (std::cin.eof()) { break; } std::cin.clear(); std::cin.ignore(10000, '\n'); std::cout << "invalid choice, enter a number\n"; choice = -1; continue; }
// remove the leftover newline std::cin.ignore(10000, '\n');
switch (choice) { case 1: task1.output_all_values(); break; case 2: task1.output_sum_all_values(); break; case 3: task1.output_all_odd(); break; case 4: task1.output_all_even(); break; case 5: { int num; std::cout << "enter a search value: ";
if (!(std::cin >> num)) { std::cin.clear(); std::cin.ignore(10000, '\n'); std::cout << "invalid value, enter a number\n"; break; } std::cin.ignore(10000, '\n'); task1.linear_search(num); break; } case 6: task1.output_middle_values(); break; case 7: task1.output_first_value(); break; case 8: task1.output_last_value(); break; case 9: task1.output_highest_value_loc(); break; case 10: task1.output_lowest_value_loc(); break; case 11: task1.output_bubble_sort(); break; case 12: task1.mean_average(); break; case 13: task1.median(); break; case 0: break; default: std::cout << "invalid choice\n"; } }
return 0;}
// g++ -std=c++17 -Wall -o task1 task_1.cpp