C ++程序从数组中删除元素

This is a C++ program to delete an element from array.

这是一个C ++程序,用于从数组中删除元素。

The user input an element to delete, the element is then searched in the array, if it is found it is deleted and new array is displayed. If the element is not present in the array then a not found message is displayed.

用户输入要删除的元素,然后在数组中搜索该元素,如果找到该元素将被删除并显示新的数组。 如果数组中不存在该元素,则会显示一条未找到消息。

#include<iostream>
#include<process.h>
 
using namespace std;
 
int main()
{
	int a[50],x,n,i,j,b[50];
	cout<<"How many elements of Array you want to create?";
	cin>>n;
	cout<<"\nEnter elements of Array\n";
	
	for(i=0;i<n;++i)
		cin>>a[i];
		
	cout<<"\nEnter element to delete:";
	cin>>x;
	
	for(i=0,j=0;i<n;++i)
	{
		if(a[i]!=x)
			b[j++]=a[i];
	}
 
	if(j==n)
	{
		cout<<"\nSoory!!!Element is not in the Array";
		exit(0);
	}
	else
	{
		cout<<"\nNew Array is ";
		for(i=0;i<j;i++)
			cout<<b[i]<<" ";
	}
 
	return 0;
}

Output

输出量

How many elements of Array you want to create?5

您要创建多少个数组元素?5

Enter elements of Array 14 8 3 6 9

输入数组 14 8 3 6 9的 元素

Enter element to delete:6

输入要删除的元素:6

New Array is 14 8 3 9

新数组是14 8 3 9

翻译自: https://www.thecrazyprogrammer.com/2011/11/c-program-to-delete-element-from-array.html

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值