简体   繁体   English

C ++阵列复制/移位

[英]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". 很好,我现在遇到的问题是第2部分。在这里,我们要“将数组复制到另一个数组,并通过N个元素移动一个数组”。 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; 例如:输入= 1,2,3,4,5; output = 3, 4, 5, 1, 2 输出= 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? 设置的stl算法如何为我们提供帮助?

read_data remain as the same one you provided read_data保持与您提供的相同

#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 如果您的编译器尚不支持c ++ 11功能

std::minmax_element could replace by std::min_element and std::max_element std::begin() can replace by shift_array.begin() std :: minmax_element可以替换为std :: min_element和std :: max_element std :: begin()可以替换为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"). 我不知道您的课堂教学风格是什么,以我的拙见,初学者应该从c ++提供的那些更高层次的组件开始,例如向量,字符串,算法等。我想您的老师正在教您这种方式,您可以使用c ++随附的算法和容器(让我们乞求您的班级没有在教您“带类的c”,并说“ OOP是世界上最好的东西”之类的东西)。

ps : You could use vector to replace the raw array if you like ps:如果愿意,可以使用vector替换原始数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM