急!!!编写函数实现删除一维整型数组中指定元素,在主函数输入数组和输出...
发布网友
发布时间:2024-10-24 01:23
我来回答
共2个回答
热心网友
时间:2024-11-07 12:53
#! /bin/bash
read -p "input the length of array: " length
for i in `seq $length`
do
read -p "input date: " array[$i]
i=$(($i+1))
done
for i in "${array[@]}"
do
echo -ne "$i "
done
read -p "input the number you want to delete: " num
for i in "${array[@]}"
do
if [ "$i" -ne "$num" ];then
echo -ne "${array[$i]} "
fi
done
echo ""
这是脚本的写法,c语言忘干净了,思路都一样换成c语言里的函数和用法就行了
热心网友
时间:2024-11-07 12:53
void main()
{
int count, del;
scanf("%d", &count);
int *pArr = (int *)malloc(sizeof(int) * count);
memset(pArr, 0, sizeof(int) * count);
for (int i = 0; i < count; i ++)
{
scanf("%d", &pArr[i]);
}
scanf("%d", &del);
for (int i = 0; i < count;)
{
if (pArr[i] == del)
{
int j;
for (j = i; j < count-1; j ++)
{
pArr[j] = pArr[j+1];
}
pArr[j] = 0;
}
else
{
printf(" %d ", pArr[i]);
i++;
}
}
free(pArr);
return;
}