1

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

/* 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 个元素”。我不确定从哪里开始。我查找了一些关于复制数组的资源,但我不确定如何在我完成的当前代码中实现它们,尤其是在转移时。如果有人有任何想法、想法或资源可以帮助我走上正确的道路,我们将不胜感激。我还应该指出,我是一个初学者(这是一个初学者课程),所以这个作业可能不是完成事情的“最佳”方式,而是在有意义的情况下结合我们所学到的知识。

4

2 回答 2

2
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
于 2013-04-29T12:04:22.857 回答
0

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

例如:输入 = 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_element std::begin() 可以替换为 shift_array.begin()

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

ps:如果你愿意,你可以使用向量来替换原始数组

于 2013-04-29T13:48:04.940 回答