Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you only want to copy the array into another array and shift them</p> <p>ex : input = 1, 2, 3, 4, 5; output = 3, 4, 5, 1, 2</p> <p>The cumbersome solution is</p> <pre><code>//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; } </code></pre> <p>How could the stl algorithms set help us?</p> <p>read_data remain as the same one you provided</p> <pre><code>#include &lt;algorithm&gt; //std::minmax_element, std::rotate_copy #include &lt;iostream&gt; #include &lt;iterator&gt; //for std::begin() #include &lt;numeric&gt; //for std::accumulate() #include &lt;string&gt; #include &lt;vector&gt; 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 &lt;&lt; "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 &lt;&lt; "The maximum value entered was " &lt;&lt; min_max.second &lt;&lt; "\n"; std::cout &lt;&lt; "The lowest value entered was " &lt;&lt; min_max.first &lt;&lt; "\n"; double sum_output = std::accumulate(user_input, user_input + input_size, 0); cout &lt;&lt; "The sum of the value's entered is " &lt;&lt; sum_output &lt;&lt; "\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 &lt;&lt; "You entered " &lt;&lt; count_output &lt;&lt; " numbers." &lt;&lt; "\n"; cout &lt;&lt; "The average of your numbers is " &lt;&lt; sum_output / count_output &lt;&lt; "\n"; int shift; std::cout&lt;&lt;"How many positions do you want to shift?"&lt;&lt;std::endl; std::cin&gt;&gt;shift; std::vector&lt;int&gt; 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; } </code></pre> <p>if your compiler do not support c++11 features yet</p> <p>std::minmax_element could replace by std::min_element and std::max_element std::begin() can replace by shift_array.begin()</p> <p>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").</p> <p>ps : You could use vector to replace the raw array if you like</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload