简体   繁体   English

输出中的特定递归逆向程序问题

[英]Specific Recursive Reverse Program Issue in Outputting

I completed my recursive reverse (array) C++ program however, it is outputting partially correctly, with an extra "10" which I believe is because my const int SIZE = 10. I am unsure about where I made the error(s) in my program and would like some advice on where the error has happened.我完成了我的递归反向(数组)C++ 程序,但是,它部分输出正确,还有一个额外的“10”,我相信这是因为我的 const int SIZE = 10。我不确定我在哪里犯了错误程序,并想就错误发生的位置提供一些建议。 Thank you.谢谢你。 Any help will be greatly appreciated.任何帮助将不胜感激。 This is the output I am receiving:这是我收到的输出:

Original Array:   9   2   6  11   0  18   4  13   2   7
Reversed Array:   7   2  13   4  18   0327053270532705  10

The following is my code:以下是我的代码:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

const int SIZE = 10;
unsigned int seed = int (time(0));

using namespace std;

double random(unsigned int &seed);
void initialize(int a[]);
void print_array(int a[]);
void recursive_reverse_array(int a[], int[], int[]);

double random(unsigned int &seed)
{
  const int MODULUS = 15749;
  const int MULTIPLIER = 69069;
  const int INCREMENT = 1;
  seed  = ((MULTIPLIER *seed) + INCREMENT) % MODULUS;
  return double (seed)/double(MODULUS);
}

void initialize(int a[])
{
  for (int i = 0; i < SIZE; ++i)
     a[i] = (20 * (random(seed)));
}

void print_array (int a[])
{
  for (int i = 0; i < SIZE; ++i)
     cout << setw(4) << a[i];
  cout << endl;
}

void recursive_reverse_array(int a[],int num1, int num2)
{
  int k;
  if(num1 < num2)
  {
    a[num1] = a[num2];
    a[num2] = k;
    recursive_reverse_array(a, num1 + 1, num2 - 1);
  }
}

int main ()
{
  int arr[SIZE];
  cout << "Original Array:";
  initialize(arr);
  print_array(arr);
  recursive_reverse_array(arr, 0, SIZE - 1);
  cout << "Reversed Array:";
  print_array(arr);
  return 0;
}

Set int k = a[num1];设置int k = a[num1]; in your recursive_reverse_array function, and that should work.在您的recursive_reverse_array函数中,这应该可以工作。 Currently, you have it uninitialized, which I believe gives you your partially correct output.目前,您未初始化它,我相信这可以为您提供部分正确的输出。

you are doing a silly mistake in your code.你在你的代码中犯了一个愚蠢的错误。 please see below code and replace your code with it.请参阅下面的代码并用它替换您的代码。

   #include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

const int SIZE = 10;
unsigned int seed = int (time(0));

using namespace std;

double random(unsigned int &seed);
void initialize(int a[]);
void print_array(int a[]);
void recursive_reverse_array(int a[], int[], int[]);

double random(unsigned int &seed)
{
  const int MODULUS = 15749;
  const int MULTIPLIER = 69069;
  const int INCREMENT = 1;
  seed  = ((MULTIPLIER *seed) + INCREMENT) % MODULUS;
  return double (seed)/double(MODULUS);
}

void initialize(int a[])
{
  for (int i = 0; i < SIZE; ++i)
     a[i] = (20 * (random(seed)));
}

void print_array (int a[])
{
  for (int i = 0; i < SIZE; ++i)
     cout << setw(4) << a[i];
  cout << endl;
}

void recursive_reverse_array(int a[],int num1, int num2)
{
  int k;
  if(num1 < num2)
  {
    k=a[num1];
    a[num1] = a[num2];
    a[num2] = k;

    recursive_reverse_array(a, num1 + 1, num2 - 1);
  }
}

int main ()
{
  int arr[SIZE];
  cout << "Original Array:";
  initialize(arr);
  print_array(arr);
  recursive_reverse_array(arr, 0, SIZE - 1);
  cout << "Reversed Array:";
  print_array(arr);
  return 0;
}

you have to use k = a[num1];你必须使用k = a[num1]; in your code.在你的代码中。 hope it will work.希望它会起作用。

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

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