删除数组中指定位置的值 [英] deleting value at specifed location in an array

查看:137
本文介绍了删除数组中指定位置的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除具有N个元素的数组国家"的位置K处的值的算法


1.输入位置K

2.输入K> N,然后

打印无效的位置
返回
END IF

[将每个元素从指定位置移至步骤开始.指定位置的项目将自动删除]

3.将C = K重复到N-1
国家[C] =国家[C + 1];
[循环结束]

4.退出



我想问一下我的arraysize是否为Country [5],索引是否为0到n-1的形式.如果我想删除最后一个索引4的元素,那么此算法能做什么?

Algorithm to delete the value at location K of an array "Country" having N elements


1. Input location K

2. Input K>N then

Print invalid location
Return
END IF

[move each element from the specified location on step towards the beginning.The item at the specified location is automatically deleted]

3.Repeat For C=K To N-1
Country[C]=Country[C+1];
[End of loop]

4.Exit



i want to ask if my arraysize is Country[5] and index is form 0 to n-1. if i want to delete the element at last index 4 then what this algorithm do? How it works?

推荐答案

假定您的伪代码1)操作了排他测试,并且2)隐含的累加了一个;即C的值在循环中每次增加1,前提是增量后的值小于但不等于(N-1):
Assuming that your pseudo code 1) operates an exclusive test and 2) that there is an implicit increment by one involved; i.e. the values of C increase by one each time round the loop provided that the value after increment is less than but not equal to (N-1):
N = 5.
K = 0
C = {0, 1, 2, 3}

它什么也没做.

如果这些条件中的任何一个都不成立,则它将失败,或者是因为它将超出数组限制,或者将永远不会退出循环.

It would do nothing.

If either of these condition is not true, then it will fail, either because it will exceed the array limits, or it will never exit the loop.


(基本上)这是相同的问题按照您在此处的要求 [
This is (basically) the same issue as you asked here[^]. Why not take the advice given and use a proper dynamic container class and forget about using static arrays, which will only cause you unnecessary pain.


这篇关于删除数组中指定位置的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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