gpt4 book ai didi

C++ 数组复制/移位

转载 作者:行者123 更新时间:2023-11-30 04:18:40 39 4
gpt4 key购买 nike

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

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

一切顺利,我现在遇到的问题是第 2 部分。我们要“将数组复制到另一个数组并将数组移动 N 个元素”。我不确定从哪里开始这些。我查找了一些关于复制数组的资源,但我不确定如何在我已完成的当前代码中实现它们,尤其是在涉及到移位时。如果有人有任何想法、想法或资源可以帮助我走上正确的道路,将不胜感激。我还应该指出,我是一个初学者(这是一个初学者类(class)),所以这个作业可能不是完成事情的“最佳”方式,而是在有意义的情况下结合我们所学的知识。

最佳答案

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

关于C++ 数组复制/移位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16278063/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com