Prompt input until a mimimum number of values is entered - c++

I am writing a small program in which I have to search for smallest and largest element entered.
The thing is I have made an array 10 elements wide and the user should enter at least 5 element.
If the user enter less than 5 elements I want to prompt insufficient elements.
This is the code I have written but it's not working:
#include <iostream>
using namespace std;
int main()
{
int arr[10], largest, smallest, i = 1, j = 1;
cout << "Enter the values in array at least 5 and maximum 10 " << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
while (arr[i] >= 5)
{
largest = arr[0];
smallest = arr[0];
if (largest <= arr[j])
{
largest = arr[j];
}
if (smallest > arr[j])
{
smallest = arr[j];
}
j++;
i++;
}
getch();
return 0;
}

This code demonstrates how you'd find the min and max of a std::vector. The same principles apply to arrays. This piece of code does not allow a variable number of inputs.
#include <algorithm> // std::min_element() & std::max_element
#include <iostream>
#include <random>
#include <vector>
int main() {
// Set up; Fills vector with 10 random digits in range [1, 10]
std::mt19937 prng(std::random_device{}());
std::uniform_int_distribution<int> dist(1, 10);
std::vector<int> numbers(10);
for (unsigned int i = 0; i < numbers.size(); ++i) {
numbers[i] = dist(prng);
}
// Find minimum and maximum, the easy way
auto min = std::min_element(numbers.begin(), numbers.end());
auto max = std::max_element(numbers.begin(), numbers.end());
// Find minimum and maximum, the hard way
int minIdx = 0;
int maxIdx = 0;
for (unsigned int i = 0; i < numbers.size(); ++i) {
if (numbers[i] < numbers[minIdx]) minIdx = i;
if (numbers[i] > numbers[maxIdx]) maxIdx = i;
}
// Print all numbers to verify
for (auto i : numbers) {
std::cout << i << ' ';
}
// This print goes with the std::min_element approach
std::cout << "\nMin: " << *min << "\nMax: " << *max << '\n';
// This print goes with the for loop approach
std::cout << "\nMin: " << numbers[minIdx] << "\nMax: " << numbers[maxIdx]
<< '\n';
}
Output:
1 5 2 10 2 8 6 7 6 7
Min: 1
Max: 10
Min: 1
Max: 10
In both methods, WHERE the min and max are stored are what's recorded. The first method returns an iterator, and the for-loop method records the index. It's more valuable to know where your min and max are versus just knowing the value.
The deal with a variable number of inputs and requiring a minimum is that things get complicated very quickly. The easiest, but not greatest (subjective) approach is to use a sentinel value. It's something the user must type to indicate that they are done typing. The wrinkle here that throws that out a bit is the fact that the user can enter the number 10. So I kind of pull out some bigger guns to handle this. A nice thing about the big guns is that they don't require a sentinel value. The user can just press Enter.
The code below uses a C-array like you, and it is a lot more tedious because of it. std::vectors know their size, and you can just add elements to it. With a C-array, you have to track the size separately, make sure it's always correct, etc. I lose the ability to use range-based for loops since the array may be only partially utilized.
#include <algorithm> // std::min_element() & std::max_element
#include <iostream>
#include <iterator>
#include <random>
#include <string>
int main() {
constexpr int capacity = 10;
int numbers[capacity];
int size = 0;
std::string tmp;
while (size < capacity && std::getline(std::cin, tmp)) {
if (tmp == "" && size >= 5) {
break;
}
if (tmp == "" && size < 5) {
std::cerr << "Need at least 5 elements.\n";
continue;
}
std::size_t pos;
int num;
try {
num = std::stoi(tmp, &pos);
} catch (...) {
std::cerr << "Exception thrown.\n";
continue;
}
if (pos != tmp.length()) {
std::cerr << "Bad Value.\n";
continue;
}
if (num >= 0 && num <= 10) {
numbers[size] = num;
++size;
}
}
// Find minimum and maximum, the easy way
auto min = std::min_element(std::begin(numbers), std::begin(numbers) + size);
auto max = std::max_element(std::begin(numbers), std::begin(numbers) + size);
// Find minimum and maximum, the hard way
int minIdx = 0;
int maxIdx = 0;
for (int i = 0; i < size; ++i) {
if (numbers[i] < numbers[minIdx]) minIdx = i;
if (numbers[i] > numbers[maxIdx]) maxIdx = i;
}
// Print all numbers to verify
for (int i = 0; i < size; ++i) {
std::cout << numbers[i] << ' ';
}
// This print goes with the std::min_element approach
std::cout << "\nMin: " << *min << "\nMax: " << *max << '\n';
// This print goes with the for loop approach
std::cout << "\nMin: " << numbers[minIdx] << "\nMax: " << numbers[maxIdx]
<< '\n';
}
You can tell how much extra work is required just for basic ASCII input. If you make some assumptions, a lot of the code can be cut out, but where user input is concerned, those assumptions are rarely safe, and are more of a convenience for quick testing or learning purposes.

Related

How do I calculate the sum of all the array numbers after the first negative?

Can you help me with this problem? All I could do was count all the negative numbers.
Here is my code:
using namespace std;
int main()
{
const int SIZE = 10;
int arr[SIZE]{};
int number=0;
srand(time(NULL));
cout << "Your array is: " << endl;
for (int i=0; i<SIZE; i++)
{
int newValue = rand()%20-10;
arr[i] = newValue;
cout << arr[i] << " ";
if (arr[i] < 0)
{
for (int j=-1; j<SIZE; j++)
{
number = arr[i];
sum += fabs(number);
break;
}
}
}
cout << endl;
cout << "Sum of elements after first element < 0 is: " << sum;
cout << endl;
}
One way is to have a flag that is zero to start with that is switched on after the first negative:
int flag = 0;
int sum = 0;
for (std::size_t i = 0; i < SIZE; ++i){
sum += flag * arr[i];
flag |= arr[i] < 0;
}
This approach carries the advantage that you don't need an array at all: substituting the next number from standard input for arr[i] is sufficient.
In your specific case, there are numerous simple and efficient solutions, like that offered by Bathsheba.
However, for a more general case of summing elements in an array after the first value satisfying a given condition, you can use the std::find_if and std::accumulate functions from the STL, providing appropriate lambda functions to do the test (checking for negative) and summation (the sum += fabs(number) in your code implies that you want to sum the absolute values of the remaining elements1).
Here's a possible implementation:
#include <cstdlib> // std::abs, std::rand
#include <ctime> // std::time
#include <algorithm> // std::find_if
#include <numeric> // std::accumulate
#include <iostream>
using std::cout, std::endl;
int main()
{
const int SIZE = 10;
int arr[SIZE]{};
// Generate random array...
std::srand(static_cast<unsigned int>(time(nullptr)));
cout << "Your array is: " << endl;
for (int i = 0; i < SIZE; i++) {
int newValue = std::rand() % 20 - 10;
arr[i] = newValue;
cout << arr[i] << " ";
}
// Sum all abs values after first negative ...
auto is_neg = [](int i) { return i < 0; };
auto fn = std::find_if(std::begin(arr), std::end(arr), is_neg);
auto sum_abs = [](int a, int b) { return a + std::abs(b); };
// Set the sum to ZERO if the first negative is the last element...
int sum = (fn == std::end(arr)) ? 0 : std::accumulate(++fn, std::end(arr), 0, sum_abs);
cout << endl;
cout << "Sum of elements after first element < 0 is: " << sum;
cout << endl;
return 0;
}
1 If this is not the case, and you just want the sum of the actual values, then you can omit the 4th (sum_abs) argument in the call to std::accumulate (and the definition of that lambda).

Is there a way to pass a vector from a helper function to another helper function in C++?

I'm working through one of the classic C++ exercises of writing a program to determine which numbers are primes. The version I'm working on now requires me to be able to determine which values are prime up to a value inputted by the user called max.
The algorithm I'm attempting to construct behaves in the following fashion:
1) Enter the desired max value.
2) Take this max and then put it into a function which will calculate the sqrt(max).
3) Using sqrt(max) I will construct a vector of the primes up to the value of sqrt(max)
4) using this sqrt(max) vector I will then evaluate which values are prime up to the value max by creating a specific function to determine what values in the list up to max are prime. Then I will generate a list of all these primes.
With this being the structure here is my code for the endeavor:
#include "pch.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::string;
using std::vector;
int determine_prime(int x) {
// function made to determine if a number is prime
// used the fact that to determine if number is prime only need to check if
// prime values less than sqrt(x) divide x
vector<int> vector_of_sqrt_primes = list_of_prime_sqrt();
vp_1 = x % vp_1 = x % vector_of_sqrt_primes[i];
for (int i = 0; i < vector_of_sqrt_primes.size(); i = i + 1) {
if (vp_1 == 0 &&
x != vector_of_sqrt_primes[i]) { // verifying if value is prime
cout << x << " is not a prime number. \n";
return 0;
}
else {
cout << x << " is a prime number. \n";
return 1;
}
}
}
int list_of_prime_sqrt(int y) {
// using this vector as reference for all values less than the sqrt of max
vector<int> vector_of_primes_sqrt = {2};
int vps = 0;
for (int i = 2; i < round(sqrt(y)); i = i + 1) {
for (int j = 0; j < vector_of_primes_sqrt.size(); j = j + 1) {
vps = i % vector_of_primes_sqrt[j];
if (vps == 0 && i != vector_of_primes_sqrt[j]) {
cout << i << " is not a prime number. \n";
} else {
cout << i << " is a prime number. \n";
vector_of_primes_sqrt.push_back(i);
}
}
}
}
int main() {
int max = 0;
vector<int> primes_list = {};
cout << "Please enter the number of integers you would like to inspect "
"whether they are prime.\n";
cin >> max;
list_of_prime_sqrt(max);
for (int i = 1; i < max + 1; i = i + 1) {
int p = determine_prime(i);
if (p == 1) {
primes_list.push_back(i);
}
}
for (int j = 0; j < primes_list.size(); j = j + 1) {
cout << primes_list[j] << "\n";
}
}
So what I was hoping was that I would be able to use vector_of_sqrt_primes in the determine_prime() function and then work out which values are primes a return them to my main(). But I am hitting a wall. So all of this to askif there is a way for me to be able to do this? I haven't gotten to the point to be able to use pointers or anything advanced. I'm working through Stroustroup Programming Principles and Practices and this is Chapter 4.
Below are two different ways in solving your problem. One returns a vector and the other uses pass by reference to be able to modify the vector passed into the parameter
#include <iostream>
#include <vector>
#include <string>
bool is_prime(int number){
//exceptions
if(number == 1) return false;
if(number == 2 || number == 5) return true;
std::string str = std::to_string(number);
if(str.back() == '1' || str.back() == '3' || str.back() == '7' || str.back() == '9'){
for(int i = 3; i * i <= number; i++){
if(number % i == 0){
return false;
}
}
return true;
}
return false;
}
//adds the value to the vector passed in and the values will 'save'
void find_primes(std::vector<int>& primes, int max){
for(int i = 0; i < max; i++){
if(is_prime(i)) primes.push_back(i);
}
}
//adds the results to a vector and returns that vector
std::vector<int> return_vec_primes(int max){
std::vector<int> results;
for(int i = 0; i < max; i++){
if(is_prime(i)) results.push_back(i);
}
return results;
}
int main(){
std::vector<int> reference_vec;
//pass the vector into the function
find_primes(reference_vec, 100);
//the function will return the vector into 'returned_vec'
std::vector<int> returned_vec = return_vec_primes(100);
//same results
for(int i : reference_vec) std::cout << "prime: " << i << "\n";
for(int i : returned_vec) std::cout << "prime: " << i << "\n";
return 0;
}

C++, sort through array of numbers to find uniqueness

Say I have an array of 4 different numbers.
int numbers[4] = {50234, 50356, 50454, 50934};
How do you make a nested for loop in C++ to sort through these numbers from back to front in order to identify the required amount of digits needed for uniqueness?
From the example you can tell that you'll need 3 digits from the back to make sure no numbers contain similar tails of numbers. 50234, 50934 = 3 digits to have them unique = 502 and 509 respectively.
What would the for loop look like to go through each of these numbers one by one, number by number, and sort out identical numbers to reach an output of 3?
It would go like this:
4
6 - discard this number, it's not identical
4
4
Then:
3
5 - discard this number
3
Then:
2
9 Hurray! No similar numbers anymore, print out 3 being the answer.
I'm stumped and can't figure it out.
Any help would be greatly appreciated, thank you.
Say you start with
#include <unordered_set>
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
const std::vector<int> numbers{50234, 50356, 50454, 50934};
You can transform it into a vector of strings:
std::vector<std::string> string_numbers;
std::for_each(std::begin(numbers), std::end(numbers), [&](int n){ string_numbers.push_back(std::to_string(n)); });
Now we'll check the number of digits required, starting at 1:
size_t digits = 1;
while(true) {
At each iteration, we'll create an unordered_set
std::unordered_set<std::string> partials;
For each number, we'll attempt to place digits digits of it into the set:
for(const auto &s: string_numbers) {
if(s.size() <= digits) {
std::cout << "not unique" << std::endl;
return 0;
}
partials.insert(s.substr(0, digits));
}
If the size of the set is the size of the vector, we're done:
if(partials.size() == numbers.size()) {
std::cout << digits << " required" << std::endl;
return 0;
}
Otherwise, we need to increase the number of digits:
++digits;
}
}
Full code:
#include <unordered_set>
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
const std::vector<int> numbers{50234, 50356, 50454, 50934};
std::vector<std::string> string_numbers;
std::for_each(std::begin(numbers), std::end(numbers), [&](int n){ string_numbers.push_back(std::to_string(n)); });
size_t digits = 1;
while(true) {
std::unordered_set<std::string> partials;
for(const auto &s: string_numbers) {
if(s.size() <= digits) {
std::cout << "not unique" << std::endl;
return 0;
}
partials.insert(s.substr(0, digits));
}
if(partials.size() == numbers.size()) {
std::cout << digits << " required" << std::endl;
return 0;
}
++digits;
}
}
if you want to sort numbers so use one of sort algorithms let's say bubble sort. then check for uniqueness and store the unique values in a new array then print them:
we make our code for understanding and practice but in a real program we use libraries they are too much powerful and quick:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int numbers[4] = {50234, 50356, 50454, 50934};
// int numbers[4] = {50234, 50356, 50454, 50356};
for(int i(0); i < 4; i++)
{
for(int j(i + 1); j < 4; j++)
{
if(numbers[i] > numbers[j])
{
numbers[i] ^= numbers[j];
numbers[j] ^= numbers[i];
numbers[i] ^= numbers[j];
}
}
}
for(int i = 0; i < 4; i++)
cout << numbers[i] << ", ";
int nUniq = 0;
bool isUniq = true;
for(int i = 0; i < 4; i++)
{
isUniq = true;
for(int j(i + 1); j < 4; j++)
{
if(numbers[i] == numbers[j])
{
isUniq = false;
break;
}
}
if(isUniq)
nUniq++;
}
cout << nUniq << endl;
int* ptrUniq = new int[nUniq];
int k = 0;
for(int i = 0; i < 4; i++)
{
isUniq = true;
for(int j(i + 1); j < 4; j++)
{
if(numbers[i] == numbers[j])
{
isUniq = false;
break;
}
}
if(isUniq)
{
ptrUniq[k] = numbers[i];
k++;
}
}
cout << "\nhere are uniq values:\n\n";
for(int i = 0; i < nUniq; i++)
cout << ptrUniq[i] << ", ";
delete[] ptrUniq;
ptrUniq = NULL;
cout << endl << endl;
return 0;
}

Create randomly sized (within a certain range) arrays over many iterations

I am trying to create many arrays consisting of random numbers and of random size between the range of, say, 1 and 20 elements. My code works SOMETIMES.
I am using a random number between my desired range to determine the array size. If the first iteration produces an array size of value 10, say, then for some reason my code does not want to create any arrays of size larger than 10. Various arrays will be created (and the list of those arrays will be outputed) until a certain iteration produces a random number larger than 10. Then I get this error:
Array index out of range numbers->[11] valid upto numbers[9]
"numbers" is the name of the array. Here is the relevant portion of my code:
srand(time(0));
int j, flag = 0;
int temp;
int rand=1;
for(int t=0; t<50; t++)
{
int length = rand()% 20 + 1;
cout<<"length is " << length << endl;
int numbers[length];
for(int i = 0; i < length; i++)
{
numbers[i]=rand();
cout << numbers[i] << endl;
}
for(j=0; (j<=length); j++)
{
for (int i=0; i<(length-1); i++)
{
if(numbers[i+1]<numbers[i])
{
temp=numbers[i];
numbers[i]=numbers[i+1];
numbers[i+1]=temp;
flag++;
}
}
}
cout << "Number of Swaps : " << flag << endl;
}
As #Bob__ wrote, allocating variable length arrays is not C++ standard. It might work sometimes on specific compilers, but it may break on others.
But there are good alternatives. You can allocate dynamic memory with new. For instance:
int *array = new int[size];
array[0] = 3;
array[1] = 5;
cout << array[1];
delete [] array;
Don't forget to delete the memory with delete afterwards.
Or you could use vector<int>. It's a STL-container, that was made for exactly this purpose.
#include <vector>
using namespace std;
...
vector<int> vec(size);
vec[0] = 3;
vec[1] = 5;
cout << vec[1];
Variable Length Arrays are not in C++ standard, but only offered as extension by some compilers. I wouldn't trust them and you don't really need anything like that in your code.
You can declare your array outside the outer for loop as you know its max lenght:
#define MAXL 20
int numbers[MAXL];
for ...
int length = rand() % MAXL + 1;
...
Besides, if you are implementing a bubble sort I think that the condition of the inner i loop should be i < length - j
rand is used in two contexts. As a variable int rand and as a function std::rand(). I'd suggest to delete any using namespace std; but since the variable is not needed anyways you could as well delete int rand = 1;. Note that rand() is a C function. You may use it but IMHO std::rand() is more pure C++.
int numbers[length]; will not compile because length is a non-constant. Use std::vector<int> numbers(length); instead.
And that's about it.
#include <iostream>
#include <vector>
#include <time.h>
int main()
{
std::srand(time(0));
int j, flag = 0;
int temp;
for (int t = 0; t < 50; t++)
{
int length = std::rand() % 20 + 1;
std::cout << "length is " << length << std::endl;
std::vector<int> numbers(length);;
for (int i = 0; i < length; i++)
{
numbers[i] = std::rand();
std::cout << numbers[i] << std::endl;
}
for (j = 0; (j <= length); j++)
{
for (int i = 0; i < (length - 1); i++)
{
if (numbers[i + 1] < numbers[i])
{
temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
flag++;
}
}
}
std::cout << "Number of Swaps : " << flag << std::endl;
}
}

C++ Array copying/shift

We had a project that asked us to Write a program that allows a user to enter a series of numbers "read numbers into an array for further processing, user signals that they are finished by entering a negative number (negative not used in calculations), after all numbers have been read in do the following, sum up the #'s entered, count the #'s entered, find min/max # entered, compute average, then output them on the screen. So the working version of this that I made looks like so
/* Reads data into array.
paramater a = the array to fill
paramater a_capacity = maximum size
paramater a_size = filled with size of a after reading input. */
void read_data(double a[], int a_capacity, int& a_size)
{
a_size = 0;
bool computation = true;
while (computation)
{
double x;
cin >> x;
if (x < 0)
computation = false;
else if (a_size == a_capacity)
{
cout << "Extra data ignored\n";
computation = false;
}
else
{
a[a_size] = x;
a_size++;
}
}
}
/* computes the maximum value in array
paramater a = the array
Paramater a_size = the number of values in a */
double largest_value(const double a[], int a_size)
{
if(a_size < 0)
return 0;
double maximum = a[0];
for(int i = 1; i < a_size; i++)
if (a[i] > maximum)
maximum = a[i];
return maximum;
}
/* computes the minimum value in array */
double smallest_value(const double a[], int a_size)
{
if(a_size < 0)
return 0;
double minimum = a[0];
for(int i = 1; i < a_size; i++)
if (a[i] < minimum)
minimum = a[i];
return minimum;
}
//computes the sum of the numbers entered
double sum_value(const double a [], int a_size)
{
if (a_size < 0)
return 0;
double sum = 0;
for(int i = 0; i < a_size; i++)
sum = sum + a[i];
return sum;
}
//keeps running count of numbers entered
double count_value(const double a[], int a_size)
{
if (a_size < 0)
return 0;
int count = 0;
for(int i = 1; i <= a_size; i++)
count = i;
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int INPUT_CAPACITY = 100;
double user_input[INPUT_CAPACITY];
int input_size = 0;
double average = 0;
cout << "Enter numbers. Input negative to quit.:\n";
read_data(user_input, INPUT_CAPACITY, input_size);
double max_output = largest_value(user_input, input_size);
cout << "The maximum value entered was " << max_output << "\n";
double min_output = smallest_value(user_input, input_size);
cout << "The lowest value entered was " << min_output << "\n";
double sum_output = sum_value(user_input, input_size);
cout << "The sum of the value's entered is " << sum_output << "\n";
double count_output = count_value(user_input, input_size);
cout << "You entered " << count_output << " numbers." << "\n";
cout << "The average of your numbers is " << sum_output / count_output << "\n";
string str;
getline(cin,str);
getline(cin,str);
return 0;
}
That went fine, the problem I am having now is part 2. Where we are to "copy the array to another and shift an array by N elements". I'm not sure where to begin on either of these. I've looked up a few resources on copying array's but I was not sure how to implement them in the current code I have finished, especially when it comes to shifting. If anyone has any thoughts, ideas, or resources that can help me on the right path it would be greatly appreciated. I should point out as well, that I am a beginner (and this is a beginners class) so this assignment might not be the 'optimal' way things could be done, but instead incorporates what we have learned if that makes sense.
for(int i = 0; i < n; ++i){
int j = (i - k)%n;
b[i] = a[j];
}
Check it. I'm not sure
If this works you could improve it to
for(int i = 0; i < n; ++i)
b[i] = a[(i - k)%n];//here can be (i +/- k) it depends which direction u would shift
If you only want to copy the array into another array and shift them
ex : input = 1, 2, 3, 4, 5; output = 3, 4, 5, 1, 2
The cumbersome solution is
//no template or unsafe void* since you are a beginner
int* copy_to(int *begin, int *end, int *result)
{
while(begin != end){
*result = *begin;
++result; ++begin;
}
return result;
}
int main()
{
int input[] = {1, 2, 3, 4, 5};
size_t const size = sizeof(input) / sizeof(int);
size_t const begin = 2;
int output[size] = {0}; //0, 0, 0, 0, 0
int *result = copy_to(input + begin, input + size - begin, output); //3, 4, 5, 0, 0
copy_to(input, input + begin, result); //3, 4, 5, 1, 2
return 0;
}
How could the stl algorithms set help us?
read_data remain as the same one you provided
#include <algorithm> //std::minmax_element, std::rotate_copy
#include <iostream>
#include <iterator> //for std::begin()
#include <numeric> //for std::accumulate()
#include <string>
#include <vector>
int main(int argc, char *argv[]) //don't use _tmain, they are unportable
{
const int INPUT_CAPACITY = 100;
double user_input[INPUT_CAPACITY];
int input_size = 0;
double average = 0;
cout << "Enter numbers. Input negative to quit.:\n";
read_data(user_input, INPUT_CAPACITY, input_size);
auto const min_max = std::minmax_element (user_input, user_input + input_size); //only valid for c++11
std::cout << "The maximum value entered was " << min_max.second << "\n";
std::cout << "The lowest value entered was " << min_max.first << "\n";
double sum_output = std::accumulate(user_input, user_input + input_size, 0);
cout << "The sum of the value's entered is " << sum_output << "\n";
//I don't know the meaning of you count_value, why don't just output input_size?
double count_output = count_value(user_input, input_size);
cout << "You entered " << count_output << " numbers." << "\n";
cout << "The average of your numbers is " << sum_output / count_output << "\n";
int shift;
std::cout<<"How many positions do you want to shift?"<<std::endl;
std::cin>>shift;
std::vector<int> shift_array(input_size);
std::rotate_copy(user_input, user_input + shift, user_input + input_size, std::begin(shift_array));
//don't know what are they for?
std::string str;
std::getline(std::cin,str);
std::getline(std::cin,str);
return 0;
}
if your compiler do not support c++11 features yet
std::minmax_element could replace by
std::min_element and std::max_element
std::begin() can replace by shift_array.begin()
I don't know what is the teaching style of your class, in my humble opinion, beginners should
start with those higher level components provided by c++ like vector, string, algorithms
and so on.I suppose your teachers are teaching you that way and you are allowed to use the
algorithms and containers come with c++(Let us beg that your class are not teaching you "c with classes" and say something like "OOP is the best thing in the world").
ps : You could use vector to replace the raw array if you like