我试图使这个算法在C ++中工作,但我没有得到任何适当的解决方案。 [英] I tried to make this algorithm to work in C++ but I'm not getting any proper solution.

查看:68
本文介绍了我试图使这个算法在C ++中工作,但我没有得到任何适当的解决方案。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

插入数组列表的伪代码。回想一下,我们假设数组中的所有元素必须是连续的。另请注意,伪代码使用基于0的索引。 

insert(element,index)://将元素插入数组并在成功时返回True(如果失败则返回False)
//在插入
之前执行必要的安全检查指数< 0或索引> n://无效索引
返回False

如果n == array.length://如果数组已满
newArray =空数组长度为2 * array.length
for i从0到n-1://将旧数组的所有元素复制到新数组
newArray [i] = array [i]
array = newArray //用new替换旧数组array

//执行插入算法
如果index == n://在数组末尾插入
array [index] = element //执行插入

else://一般插入
for i从n-1到index://为新元素创建空间(如果插入不在末尾)
array [i + 1] = array [i]
array [index] = element //执行插入

n = n + 1 //增加元素数
返回True

< br $> b $ b

我尝试了什么:



我是试用按照算法逐步进行,但在错误后得到错误。

解决方案

我们不做你的作业:它是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!

如果你用你编写的代码得到错误后的错误,那么请准确地告诉我们你尝试了什么,告诉我们错误是什么,复制和粘贴任何消息,并解释你为了解决它们而尝试的内容。



如果遇到具体问题,请询问相关问题,我们将尽力救命。但是我们不打算为你做这一切!


当你想用C ++编写代码时,你需要学习语法。主要的很多大括号都缺失了,而且:的用法完全错了。



你需要向一些 C ++教程使用这种语言。安装Visual Studio并深入研究一些示例代码。 ; - )

 the pseudocode of insertion into an Array List. Recall that we assume that all elements in the array must be contiguous. Also, note that the pseudocode uses 0-based indexing.

insert(element, index): // inserts element into array and returns True on success (or False on failure)
    // perform the required safety checks before insertion
    if index < 0 or index > n:   // invalid indices
        return False

    if n == array.length:        // if array is full
        newArray = empty array of length 2*array.length
        for i from 0 to n-1:     // copy all elements of old array to new array
            newArray[i] = array[i]
        array = newArray         // replace old array with new array

    // perform the insertion algorithm
    if index == n:               // insertion at end of array
        array[index] = element   // perform insertion

    else:                        // general insertion
        for i from n-1 to index: // make space for the new element (if insertion not at the end)
            array[i+1] = array[i]
        array[index] = element   // perform insertion

    n = n+1                      // increment number of elements
    return True



What I have tried:

I am trying to do this by going step by step as per algorithm but getting errors after errors.

解决方案

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
If you are getting "errors after errors" with code you have written to do this, then show us exactly what you tried, tell us what the errors are, copy and paste any messages, and explain what you have tried in order to fix them.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


When you want to code in C++ you need to learn the syntax. Primary a lot of braces are missing and the usage of ":" is completly wrong.

You need to learn from some C++ tutorial to use this language. Install the Visual Studio and dig into some sample code. ;-)


这篇关于我试图使这个算法在C ++中工作,但我没有得到任何适当的解决方案。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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