所以我想要这个输出:
Position you wanted to insert
2
Value You wanted to insert
34
Array after insertion:
10 5 34 46 2 100 97
但是它不能显示输出,我该怎么办请帮助我,并修复它..
#include <stdio.h>
#include <conio.h>
int main(){
int arr[100] = {10, 5, 46, 2, 100, 97};
int n, max, i;
n = 0;
clrscr();
if(i > n){
printf("postion you wanted to insert");
scanf("%d", &n);
}
printf("Value for position: \n");
scanf("%d", &max);
for(i=7; i>n-1; i--){
arr[i+1] = arr[i];
arr[n] = max;
}
printf("Array after insertion: \n");
for(i = 0; i < 7; i++){
printf("%d\t", arr[i]);
}
getch();
}
最佳答案
您没有为 int i 分配任何值,因此编译器将垃圾值分配给变量 i,因此您没有得到输出。
你需要做这样的事情
void main()
{
int n,val,i;
int arr[100] = {10,20,30,40,50};
printf("enter the position where you want to insert the new element \n");
scanf("%d",&n);
print("Enter the value you want to insert");
scanf("%d",&val);
for(i=5,i>=n,i--)
{
arr[i+1]=arr[i];
}
arr[n]=max;
/*then print your array*/
getch();
}
关于c - 在c中的一维数组中的特定位置插入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18148160/