c++数组复制/移位

C++ Array copying/shift

本文关键字:移位 复制 数组 c++      更新时间:2023-10-16

我们有一个项目,要求我们编写一个程序,允许用户输入一系列数字,将数字读入数组进行进一步处理,用户通过输入一个负数(计算中不使用负数)来表示完成,在所有数字读入后执行以下操作,将输入的#加起来,计算输入的#,找到输入的最小/最大#,计算平均值,然后在屏幕上输出它们。因此,我所创建的工作版本看起来像这样

/* 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 ignoredn";
        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;
}

一切顺利,我现在遇到的问题是第二部分。我们要"将数组复制到另一个数组并将数组移动N个元素"。我不知道从哪里开始。我已经查找了复制数组的一些资源,但我不确定如何在我已经完成的当前代码中实现它们,特别是当涉及到转移时。如果任何人有任何想法,想法,或资源,可以帮助我在正确的道路上,这将是非常感激的。我也应该指出,我是一个初学者(这是一个初学者课程),所以这个作业可能不是"最佳"的方式,而是结合了我们所学到的东西,如果有意义的话。

for(int i = 0; i < n; ++i){
    int j = (i - k)%n;
    b[i] = a[j];
}

检查它。我不确定如果这个有效,您可以将其改进为

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

如果您只想将数组复制到另一个数组并移动它们

ex: input = 1,2,3,4,5;输出= 3、4、5、1、2

麻烦的解是

//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;
}

stl算法集如何帮助我们?

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;
}

如果你的编译器还不支持c++11的特性

std::minmax_element可以替换为Std::min_element和Std::max_elementStd::begin()可以替换为shift_array.begin()

我不知道你们班的教学风格是什么,依我拙见,初学者应该从c++提供的那些高级组件开始,比如向量、字符串、算法等等......我想你们的老师就是这么教你的,你们可以用算法和容器是c++自带的(让我们乞求你的类不会教你"c with classes",并说"OOP是世界上最好的东西")。

ps:您可以使用vector来替换原始数组,如果您喜欢