已解决 - 使用递归反转数组(特定输出问题) [英] SOLVED - reversing array using recursion (specific output issue)

查看:124
本文介绍了已解决 - 使用递归反转数组(特定输出问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了递归反向数组程序,但它没有从反向数组中输出原始数组中的最后一个数字。以下是输出示例:



原始阵列:18 18 10 8 5 1 13 13 18 19

反向数组:18 13 13 1 5 8 10 18 18



它不输出19(应该首先放在反向数组中)。有人可以指导我在我的代码中出现错误的位置吗?任何帮助将不胜感激。谢谢。以下是我的部分代码:



I completed my recursive reverse array program however, it is not outputting the last number from the original array in the reverse array. Here is a sample output:

Original Array: 18 18 10 8 5 1 13 13 18 19
Reversed Array: 18 13 13 1 5 8 10 18 18

It is not outputting "19" (which should be placed first in the reverse array). May someone guide me on where the error(s) are in my code? Any help will be greatly appreciated. Thank you. Here is a portion of my code:

void print_reverse_array(int a[], int k)
{
  if (k == 0)
    return;
  else
    cout << setw(3) << a[k - 1];
    print_reverse_array(a, k - 1);
}
 
int main ()
{
  int arr[SIZE];
  cout << "Original Array:";
  initialize(arr);
  print_array(arr);
  cout << "Reversed Array:";
  print_reverse_array(arr, SIZE - 1);
  return 0;
}





我的尝试:



我尝试在if-else语句中递增/递减k但是,这也不起作用(它不让我编译)。



*这是一个随机数组(范围是[1-20])





(我意识到我从未初始化k = SIZE在我的主要功能) - 已解决



What I have tried:

I have tried incrementing/decrementing k in the if-else statement however, that isn't working either (it is not letting me compile).

*It is a randomized array (range is [1-20] )


(I realized I never initialized k = SIZE in my main function) -- SOLVED

推荐答案

Quote:

它不是输出19(应该首先放在反向数组中)。

It is not outputting "19" (which should be placed first in the reverse array).



如果你的数组是0 1 2 3.

你想要打印3 2 1 0.

但是你得到2 1 0.



如果你的阵列是0 1 2 3 4 5 6 7 8 9。

你想要打印9 8 7 6 5 4 3 2 1 0.

但你得到8 7 6 5 4 3 2 1 0.

可能是什么问题?想一想,用你的大脑!

你需要做什么改变才能得到你想要的结果?玩价值观,看看会发生什么。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器 。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


If your array is 0 1 2 3.
You want to print 3 2 1 0.
But you get 2 1 0.

If your array is 0 1 2 3 4 5 6 7 8 9.
You want to print 9 8 7 6 5 4 3 2 1 0.
But you get 8 7 6 5 4 3 2 1 0.
What can be the problem ? Think about it and use your brain !
What change do you need to do get the result you want ? Play with values and see what happen.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于已解决 - 使用递归反转数组(特定输出问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆